Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用SwiftGen在运行时更改语言_Ios_Swift_Localization_Internationalization - Fatal编程技术网

Ios 使用SwiftGen在运行时更改语言

Ios 使用SwiftGen在运行时更改语言,ios,swift,localization,internationalization,Ios,Swift,Localization,Internationalization,我的应用程序应该在运行时支持语言更改。我用的是迅捷5.0。ViewController订阅语言更改通知,我已检查本地化功能是否正确启动。我的重写tr函数如下所示: fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { guard let bundle = LanguageManager.shared.bundle else { fatalE

我的应用程序应该在运行时支持语言更改。我用的是迅捷5.0。ViewController订阅语言更改通知,我已检查本地化功能是否正确启动。我的重写
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)
NSBundle(尚未加载)

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

好的,我发现了问题。模具正在生成静态变量:

static let label = L10n.tr("Localizable", "registration_verify.pin_code.label")
更改模具以生成计算属性修复了以下行为:

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

您的应用程序如何支持语言更改,而不会导致应用程序停止/重新启动?更改设备区域设置将终止您的应用程序,因此我不知道您打算如何通过本机设备区域设置,而不是应用程序中的某些自定义设置来执行此操作。因此,记忆SwiftGen的区域设置并不重要。当语言更改时,我发布了一个通知,在所有运行的适用ViewController上调用一个函数,名为
localize()
,其中包含所有可本地化的视图设置(标签、图像、表格等)。这样应用程序可以在运行时更改其语言。请更新答案并共享您的
.stencil
?谢谢复制整个模具,并将第29行替换为:{{accessModifier}静态变量{{string.name}swiftIdentifier:“pretty”{{{enumName}tr({table},{string.key}}}}}