您好我有一个imageView来显示一些图片。我想为每种语言展示不同的图片。因此,当用户本地化是德语时,他获得的图片与具有本地化英语的用户不同。
这是我的代码。如何为每个本地化制作几个imageArrays?
感谢
show_term_doc_count_error
答案 0 :(得分:1)
你可以使用NSLocalizedString
这样做,所以你声明这样的数组:
let help0 = UIImage(named: NSLocalizedString("help0", comment: ""))
let help1 = UIImage(named: NSLocalizedString("help1", comment: ""))
let help2 = UIImage(named: NSLocalizedString("help2", comment: ""))
let help3 = UIImage(named: NSLocalizedString("help3", comment: ""))
let help4 = UIImage(named: NSLocalizedString("help4", comment: ""))
let imageArray = [help0, help1, help2, help3, help4]
然后,您在示例ENG
和GER
中添加了本地化语言,并在这些文件中添加了您要使用的图片,如下所示(a guide如何添加本地化) :
Localization for English:
"help0" = "help0Eng";
"help1" = "help1Eng";
etc...
Localization for German:
"help0" = "help0Ger";
"help1" = "help1Ger";
etc...
您的help0Eng
,help0Ger
等是Assets.xcassets
中图片的名称。所以会发生以下情况:
答案 1 :(得分:0)
Locale
有你想要的东西。在开发过程中,您可以使用Locale.isoLanguageCodes
获取所有可能语言代码的数组(每个代码都是一个两个或三个字母的字符串)。在运行时,使用Locale.current.languageCode
返回用户当前语言的语言代码。
要实现这一点,您可以使用以下行的辅助函数:
func localizedImageArray() -> [UIImage] {
let currentLanguage = Locale.current.languageCode
let imageArray: [UIImage]
if currentLanguage == "ja" {
[#imageLiteral(resourceName: "help0_ja"), #imageLiteral(resourceName: "help1_ja"), #imageLiteral(resourceName: "help2_ja"), #imageLiteral(resourceName: "help3_ja"), #imageLiteral(resourceName: "help4_ja")]
} else if currentLanguage == "kr" {
[#imageLiteral(resourceName: "help0_kr"), #imageLiteral(resourceName: "help1_kr"), #imageLiteral(resourceName: "help2_kr"), #imageLiteral(resourceName: "help3_kr"), #imageLiteral(resourceName: "help4_kr")]
} else /* Default implementation */ {
[#imageLiteral(resourceName: "help0"), #imageLiteral(resourceName: "help1"), #imageLiteral(resourceName: "help2"), #imageLiteral(resourceName: "help3"), #imageLiteral(resourceName: "help4")]
}
return imageArray
}
在viewDidLoad()
,而不是声明imageArray = /* the part where you set the array with literals */
,您会声明imageArray = localizedImageArray()
请注意,字符串不使用此方法进行本地化。对于本地化字符串,请使用NSLocalizedString
。