应用小部件描述和名称本地化

问题描述

我想知道是否可以在本地化上使用应用程序语言,而不是电话语言?

我正在谈论:

.configurationdisplayName("widget.name".localized())

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

解决方法

是的,您可以为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

有关更多信息,请参见:

,
  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)
        }

}