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)
這是正確的新語言。應用程序重新啓動後,語言設置正確。難道我做錯了什麼?