我创建了3个翻译文件:
我有这个方法来按键获取字符串:
static func localized(key: String) -> String? {
if let path = Bundle.main.path(forResource: currentLanguage, ofType: "lproj") {
if let bundle = Bundle(path: path) {
return NSLocalizedString(key, tableName: nil, bundle: bundle, value: "", comment: "")
}
}
return nil;
}
但我得到了“NSBundle< /var/containers/Bundle/Application/213B1469-9751-4459-ABED-80879B80EFBE/Dicken.app/en.lproj>(尚未加载)”
为什么不加载,问题出在哪里?
答案 0 :(得分:1)
制作扩展名为
extension String {
func localized(lang:String) -> String {
let path = Bundle.main.path(forResource: lang, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
let label: UILabel
var language: String?
label.text = “Hello”.localized(lang: self.language! )