我想知道是否可以在本地化时使用应用程序语言,而不是手机语言?
我说的是:
.configurationDisplayName("widget.name".localized())
和
.description("widget.description".localized())
是的,您可以向configurationDisplayName
和description
提供本地化字符串,因为它们也接受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
有关更多信息,请参阅:
- SwiftUI中的本地化
- 如何在Swift UI中实现本地化
- 初始化语言设置后,您可以获得所需的正确翻译
var disName: String {
LoadLocalizeSettings()
return "disName.key".localize()
}
执行如下:
.configurationDisplayName(disName)
如果它不起作用?你也可以吊舱"本地化">
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)
}
}