0
我的应用程序应该支持在运行时更改语言。我正在使用SwiftGen 5.0。 ViewControllers订阅语言更改通知,我已经检查了本地化功能正确触发。我重写tr
功能如下:使用SwiftGen在运行时更改语言
fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
guard let bundle = LanguageManager.shared.bundle else {
fatalError("Cannot find bundle!")
}
let format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
let locale = Locale(identifier: LanguageManager.shared.currentLanguageKey!)
return String(format: format, locale: locale, arguments: args)
}
的bundle
设置像这样:
if let path = Bundle.main.path(forResource: currentLanguageKey, ofType: "lproj") {
bundle = Bundle(path: path)
}
然而,tr
函数返回以前大多语言字符串。内存中当前所有标签中只有一个刷新。在函数内部设置断点并打印bundle
返回
NSBundle </var/containers/Bundle/Application/ED5A6C7D-1807-4319-8817-45E693BC45E2/MyApp.app/en_US.lproj> (not yet loaded)
这是正确的新语言。应用程序重新启动后,语言设置正确。难道我做错了什么?