SwiftUI:未为本地化导出变量字符串
SwiftUI: Variable strings not exported for localization
根据 Apple 文档,这应该有效:
If you initialize a text view with a string variable rather than a string literal, the view triggers the init(:) initializer instead, because it assumes that you don’t want localization in that case. If you do want to localize the value stored in a string variable, you can choose to call the init(:tableName:bundle:comment:) initializer by first creating a LocalizedStringKey instance from the string variable:
Text(LocalizedStringKey(someString)) // Localizes the contents of someString
.
https://developer.apple.com/documentation/swiftui/text/init(_:tablename:bundle:comment:)
这里也推荐这个:https://www.ibabbleon.com/swiftui_localization_tutorial.html
然而,至少在我的情况下,它没有。在以下示例中,仅导出“Some content 1”值以进行本地化。
struct ContentView: View {
let text = "Some content 2"
var body: some View {
Text("Some content 1", comment: "This text is exported for localization, as expected.")
.padding()
Text(LocalizedStringKey(text), comment: "This text is not exported for localization, which is not expected behaviour.")
.padding()
}
}
在应用程序设置中,“使用编译器提取 swift 个字符串”设置为是。
我错过了什么?
您已经发现 let content2: LocalizedStringKey = "Some content 2"
可以解决您的问题,但仍然对如何添加评论有疑问。
Apple 在 WWDC 21 的 Localize your SwiftUI App session 中指出,一种方法是让您的视图的参数为 Text
类型,以便您可以提供评论。
struct ContentView: View {
let text = Text("Some content 2", comment: "This gets exported!")
var body: some View {
Text("Some content 1", comment: "This text is exported for localization, as expected.")
.padding()
text
.padding()
}
}
到目前为止,我已经找到了 2 种使用 swiftUI 来本地化字符串的方法,您可以在 WWDC talk about localizables or in this article here 中找到第一种方法。如果你只是想避免用字符串字面量污染视图,但不一定要本地化 ir,你可以这样做:
创建一个 swift 存档并创建一个枚举,如示例:
enum MyLocalizable: LocalizedStringKey {
case mytest
}
现在,在您的项目中创建一个 Localizable.strings 并设置您的字符串。
示例:
"mytest" = "Lorem ipsum dolor sit amet.";
最后,您可以在此表单上访问您的观点值:
Text(MyLocalizable.mytest.rawValue)
好的,所以在对本地化进行更多修改之后,我找到了一种行之有效的方法。
我链接到的教程 (https://www.ibabbleon.com/swiftui_localization_tutorial.html) 没有讨论这个,并且使事情与 LocalizedStringKey 和 Text(String, comment: String) 混淆。其他一些教程也走这条路,但它使代码非常丑陋,因为它将数据移动到文本中。所以如果你想从视图中分离数据,你必须在视图模型中包含文本(一个 UI 元素)。或者,如果您使用 LocalizedStringKey,则不能包含对译者的评论。
幸运的是,有一个更简单的方法!
struct ContentView: View {
let normalString = "This is not exported for localization. Good for previews!"
let localizedString = String(localized: "This is exported for localization.", comment: "You can add a comment for the translator, too!")
var body: some View {
VStack {
Text(normalString)
Text(localizedString)
}
}
}
根据 Apple 文档,这应该有效:
If you initialize a text view with a string variable rather than a string literal, the view triggers the init(:) initializer instead, because it assumes that you don’t want localization in that case. If you do want to localize the value stored in a string variable, you can choose to call the init(:tableName:bundle:comment:) initializer by first creating a LocalizedStringKey instance from the string variable:
Text(LocalizedStringKey(someString)) // Localizes the contents of
someString
.https://developer.apple.com/documentation/swiftui/text/init(_:tablename:bundle:comment:)
这里也推荐这个:https://www.ibabbleon.com/swiftui_localization_tutorial.html
然而,至少在我的情况下,它没有。在以下示例中,仅导出“Some content 1”值以进行本地化。
struct ContentView: View {
let text = "Some content 2"
var body: some View {
Text("Some content 1", comment: "This text is exported for localization, as expected.")
.padding()
Text(LocalizedStringKey(text), comment: "This text is not exported for localization, which is not expected behaviour.")
.padding()
}
}
在应用程序设置中,“使用编译器提取 swift 个字符串”设置为是。
我错过了什么?
您已经发现 let content2: LocalizedStringKey = "Some content 2"
可以解决您的问题,但仍然对如何添加评论有疑问。
Apple 在 WWDC 21 的 Localize your SwiftUI App session 中指出,一种方法是让您的视图的参数为 Text
类型,以便您可以提供评论。
struct ContentView: View {
let text = Text("Some content 2", comment: "This gets exported!")
var body: some View {
Text("Some content 1", comment: "This text is exported for localization, as expected.")
.padding()
text
.padding()
}
}
到目前为止,我已经找到了 2 种使用 swiftUI 来本地化字符串的方法,您可以在 WWDC talk about localizables or in this article here 中找到第一种方法。如果你只是想避免用字符串字面量污染视图,但不一定要本地化 ir,你可以这样做:
创建一个 swift 存档并创建一个枚举,如示例:
enum MyLocalizable: LocalizedStringKey {
case mytest
}
现在,在您的项目中创建一个 Localizable.strings 并设置您的字符串。
示例:
"mytest" = "Lorem ipsum dolor sit amet.";
最后,您可以在此表单上访问您的观点值:
Text(MyLocalizable.mytest.rawValue)
好的,所以在对本地化进行更多修改之后,我找到了一种行之有效的方法。
我链接到的教程 (https://www.ibabbleon.com/swiftui_localization_tutorial.html) 没有讨论这个,并且使事情与 LocalizedStringKey 和 Text(String, comment: String) 混淆。其他一些教程也走这条路,但它使代码非常丑陋,因为它将数据移动到文本中。所以如果你想从视图中分离数据,你必须在视图模型中包含文本(一个 UI 元素)。或者,如果您使用 LocalizedStringKey,则不能包含对译者的评论。
幸运的是,有一个更简单的方法!
struct ContentView: View {
let normalString = "This is not exported for localization. Good for previews!"
let localizedString = String(localized: "This is exported for localization.", comment: "You can add a comment for the translator, too!")
var body: some View {
VStack {
Text(normalString)
Text(localizedString)
}
}
}