问题描述
我可以使用其他语言Localizable.strings文件中的未翻译键使用默认语言(例如英语)吗?
Can i use default language (e.g. English) for untranslated keys in my other language Localizable.strings files ?
推荐答案
要实现此目的,您可以使用英语单词作为Localizable.strings文件中的键.
To achieve this, you could use the English words as the keys in the Localizable.strings file.
另一种方法是检查NSLocalizedString的结果,并在结果与键相同的情况下返回默认的英语版本(使用强制"捆绑包).
Another approach would be to check the outcome of NSLocalizedString and return the default english version (using a 'forced' bundle) in case the result is the same as the key.
它可能看起来像这样
extension NSString {
class func NSLocalizedStringWithDefault (key:String, comment:String)->String {
let message = NSLocalizedString(key, comment: comment)
if message != key {
return message
}
let language = "en"
let path = NSBundle.mainBundle().pathForResource(language, ofType: "lproj")
let bundle = NSBundle(path: path!)
if let forcedString = bundle?.localizedStringForKey(key, value: nil, table: nil){
return forcedString
}else {
return key
}
}
}
Localized.string(eng)
Localized.string (eng)
"test-key-1" = "Test 1";
"test-key-2" = "Test 2";
Localized.string(esp)
Localized.string (esp)
"test-key-1" = "El Test 1";
然后您可以像这样使用它(假设语言环境设置为'es'):
then you could use it like this (assuming locale set to 'es'):
println(NSString.NSLocalizedStringWithDefault("test-key-1", comment: "")) // El Test 1
println(NSString.NSLocalizedStringWithDefault("test-key-2", comment: "")) // Test 2 (from eng file)
这不是最干净的实现方式,但您知道了.
Not the cleanest way to implement, but you get the idea.
这篇关于密钥未翻译时使用默认语言回退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!