英文:
SwiftUI: How can I interpolate multiple localized string keys
问题
I'm trying to create a single String
out of multiple LocalizedStringKey()
objects.
我试图将多个 LocalizedStringKey()
对象合并成一个单一的 String
。
I'm using Enum
s with String
s to give the user options and then interpolating them into a single Text
View like this: Text("\(LocalizedStringKey(enumOne.rawValue)) \(LocalizedStringKey(enumTwo.rawValue))?") as String
我正在使用带有 String
的 Enum
来为用户提供选项,然后将它们插入到一个单独的 Text
视图中,就像这样:Text("\(LocalizedStringKey(enumOne.rawValue)) \(LocalizedStringKey(enumTwo.rawValue))?") as String
The as String
is there to prevent this error: "Instance method 'appendInterpolation' requires that 'LocalizedStringKey' conform to '_FormatSpecifiable'"
添加 as String
是为了防止出现这个错误:"实例方法 'appendInterpolation' 要求 'LocalizedStringKey' 遵循 '_FormatSpecifiable'"
Any help would be awesome. I'm struggling to figure out a good solution.
任何帮助都将不胜感激。我正在努力寻找一个好的解决方案。
Edit due to confusion:
the Enum: String {}
s each have multiple cases. They are used for confirmation dialogues throughout the app.
由于混淆而进行编辑:
Enum: String {}
中每个都有多个情况。它们用于应用程序中的确认对话框。
I'm using Localizable.strings
in order to future proof the app, in case it may get translated some day.
我正在使用 Localizable.strings
来确保应用程序具备未来的可翻译性,以防将来可能需要翻译。
Based on what the user is trying to do, a View
is called that takes parameters for both Enum
s. This ultimately creates a confirmation dialogue out of LocalizedStringkey
s from the rawValues
of the Enum
properties.
根据用户所要做的事情,将调用一个 View
,该视图接受了两个 Enum
的参数。这最终会从 Enum
属性的 rawValues
中创建一个确认对话框的 LocalizedStringkey
。
It does not have to be accomplished the way I'm showing here in this question. But I am trying to create a Text()
View out of multiple LocalizedStringKey
s.
这不一定要按照我在这个问题中展示的方式来完成。但我正在尝试从多个 LocalizedStringKey
中创建一个 Text()
视图。
Perhaps this would be best accomplished using an HStack()
and two separate Text()
Views. But that seems like a subpar solution to me.
也许使用 HStack()
和两个单独的 Text()
视图会是最好的方法。但对我来说,这似乎是一个次优的解决方案。
英文:
I'm trying to create a single String
out of multiple LocalizedStringKey()
objects.
I'm using Enum
s with String
s to give the user options and then interpolating them into a single Text
View like this: Text("\(LocalizedStringKey(enumOne.rawValue)) \(LocalizedStringKey(enumTwo.rawValue))?" as String)
The as String
is there to prevent this error: "Instance method 'appendInterpolation' requires that 'LocalizedStringKey' conform to '_FormatSpecifiable'"
Any help would be awesome. I'm struggling to figure out a good solution.
Edit due to confusion:
the Enum: String {}
s each have multiple cases. They are used for confirmation dialogues throughout the app.
I'm using Localizable.strings
in order to future proof the app, in case it may get translated some day.
Based on what the user is trying to do, a View
is called that takes parameters for both Enum
s. This ultimately creates a confirmation dialogue out of LocalizedStringkey
s from the rawValues
of the Enum
properties.
It does not have to be accomplished the way I'm showing here in this question. But I am trying to create a Text()
View out of multiple LocalizedStringKey
s.
Perhaps this would be best accomplished using an HStack()
and two separate Text()
Views. But that seems like a subpar solution to me.
答案1
得分: 1
> 或许最好的方法是使用HStack()
和两个独立的Text()
视图。但这对我来说似乎不是最佳解决方案。
不需要HStack
。您可以使用+
运算符将多个Text
连接在一起,它们将成为一个单独的Text
视图。
Text(LocalizedStringKey(enumOne.rawValue)) +
Text(" ") +
Text(LocalizedStringKey(enumTwo.rawValue)) +
Text("?")
与HStack
不同,使用+
创建的新Text
会自然处理换行,就好像所有组件都在单个Text
中一样。
<hr>
请注意,如果您还希望本地化显示这两个字符串的格式,例如,不是%@ %@?
,而是希望在另一种语言中显示类似选择一个:%@ 或 %@?
的内容。
// 在您的.strings文件中...
"%@ %@?" = "Choose one: %@ or %@?";
您可以使用LocalizedStringResource
进行插值:
Text("\(LocalizedStringResource(.init(enumOne.rawValue))) \(LocalizedStringResource(.init(enumOne.rawValue)))?")
此时,将在枚举中添加一个返回LocalizedStringResource
的属性可能会更好。
英文:
> Perhaps this would be best accomplished using an HStack()
and two separate Text()
Views. But that seems like a subpar solution to me.
You don't need an HStack
. You can join multiple Text
s together with the +
operator and they will become one single Text
view.
Text(LocalizedStringKey(enumOne.rawValue)) +
Text(" ") +
Text(LocalizedStringKey(enumTwo.rawValue)) +
Text("?")
Unlike an HStack
which will always keep the Text
s laid out horizontally, the new Text
created with +
handles line-wrapping "naturally", as if all the components are in a single Text
.
<hr>
Note that if you want to also localise the format in which these two strings are displayed in, e.g. instead of %@ %@?
, you want to display something like Choose one: %@ or %@?
in another language.
// in your .strings file...
"%@ %@?" = "Choose one: %@ or %@?";
You can interpolate with LocalizedStringResource
s:
Text("\(LocalizedStringResource(.init(enumOne.rawValue))) \(LocalizedStringResource(.init(enumOne.rawValue)))?")
At this point it might look better to just have a property in your enum that returns a LocalizedStringResource
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论