gpt4 book ai didi

ios - 使用 SwiftGen 在运行时更改语言

转载 作者:行者123 更新时间:2023-11-28 06:13:45 25 4
gpt4 key购买 nike

我的应用程序应该支持在运行时更改语言。我正在使用 SwiftGen 5.0。 ViewControllers 订阅了语言更改通知,我已经检查了本地化功能是否正确触发。我重写的 tr 函数如下所示:

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)

这是正确的新语言。应用程序重新启动后,语言设置正确。我做错了什么吗?

最佳答案

好的,我找到问题了。模板正在生成静态变量:

static let label = L10n.tr("Localizable", "registration_verify.pin_code.label")

更改模板以生成计算属性修复了行为:

static var label: String { 
return L10n.tr("Localizable", "registration_verify.pin_code.label")
}

关于ios - 使用 SwiftGen 在运行时更改语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45731079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com