【问题标题】:App widget description and name localization应用小部件描述和名称本地化
【发布时间】:2020-10-21 10:17:09
【问题描述】:

我想知道是否可以在本地化时使用应用语言,而不是手机语言?

我说的是:

.configurationDisplayName("widget.name".localized())

.description("widget.description".localized())

【问题讨论】:

标签: ios swift swiftui ios14 widgetkit


【解决方案1】:

是的,您可以为configurationDisplayNamedescription 提供本地化字符串,因为它们也接受LocalizedStringKey 参数:

/// Sets the localized name shown for a widget when a user adds or edits
/// the widget.
///
/// - Parameter displayName: The key for the localized name to display.
/// - Returns: A widget configuration that includes a descriptive name for
///   the widget.
public func configurationDisplayName(_ displayNameKey: LocalizedStringKey) -> some WidgetConfiguration
/// Sets the localized description shown for a widget when a user adds or
/// edits the widget.
///
/// - Parameters:
///   - descriptionKey: The key for the localized description to display.
/// - Returns: A widget configuration with a description of the widget.
public func description(_ descriptionKey: LocalizedStringKey) -> some WidgetConfiguration

欲了解更多信息,请参阅:

【讨论】:

  • 我说的是描述和名称。使用这些时,正在对设备语言进行本地化,我需要使用应用程序语言。我的意思是它有效 - 本地化有效,但本地化基于设备,而不是应用语言:)
  • 哦,我现在明白了——我认为我们还没有这种可能性。您可以将语言环境注入到视图环境中,但对于小部件是不可能的。类似的线程在这里:Localize iOS 14 dynamic widget configuration
【解决方案2】:
  1. 初始化语言设置后,即可获得所需的正确翻译。
var disName: String {
    LoadLocalizeSettings()
    return "disName.key".localize()
}
  1. 像这样执行: .configurationDisplayName(disName)

  2. 如果它不起作用?你也可以 pod 'Localize'

func LoadLocalizeSettings() {
        Localize.shared.update(provider: .strings)
        Localize.shared.update(bundle: bundle)
        Localize.shared.update(fileName: filename)
        if let language = youWantThis {
            update(language)
        }
        else {
            update(.english)
        }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2018-09-26
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多