use*_*662 4 localization ios swift
假设我的项目中有这 2 个字符串文件:
\n\nLocalizable.strings (Base)\n"hello_key" = "Hello";\n"bye_key" = "Goodbye";\nRun Code Online (Sandbox Code Playgroud)\n\n和
\n\nLocalizable.strings (Chinese Traditional)\n"hello_key" = "\xe6\x82\xa8\xe5\xa5\xbd";\nRun Code Online (Sandbox Code Playgroud)\n\n我使用以下代码来本地化“hello”字符串
\n\nNSLocalizedString("hello_key", comment: "")\nRun Code Online (Sandbox Code Playgroud)\n\n它适用于“hello_key”,但如果我在 iPhone 中使用“bye_key”并将手机语言设置为繁体中文,我会得到“bye_key”作为本地化字符串。
\n\n换句话说,没有使用 Base 本地化。在这种情况下可以显示基本英语“GoodBye”吗?
\n\n谢谢。
\n这是不受支持的短语的情况,因为您没有完全翻译为繁体中文。
苹果不会回退到基本语言,而只是返回密钥。这就是它的行为方式。我在我的博客中写了更多相关内容。
您必须编写NSLocalizedString这样的自定义(该示例用作en后备语言):
public func LS(_ key: String) -> String {
let value = NSLocalizedString(key, comment: "")
if value != key || NSLocale.preferredLanguages.first == "en" {
return value
}
// Fall back to en
guard
let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
let bundle = Bundle(path: path)
else { return value }
return NSLocalizedString(key, bundle: bundle, comment: "")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1980 次 |
| 最近记录: |