SwiftUI:如何插值多个本地化字符串键

huangapple go评论61阅读模式
英文:

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 Enums with Strings 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

我正在使用带有 StringEnum 来为用户提供选项,然后将它们插入到一个单独的 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 Enums. This ultimately creates a confirmation dialogue out of LocalizedStringkeys 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 LocalizedStringKeys.

这不一定要按照我在这个问题中展示的方式来完成。但我正在尝试从多个 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 Enums with Strings 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 Enums. This ultimately creates a confirmation dialogue out of LocalizedStringkeys 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 LocalizedStringKeys.

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 Texts together with the + operator and they will become one single Text view.

Text(LocalizedStringKey(enumOne.rawValue)) +
    Text(&quot; &quot;) +
    Text(LocalizedStringKey(enumTwo.rawValue)) +
    Text(&quot;?&quot;)

Unlike an HStack which will always keep the Texts 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...
&quot;%@ %@?&quot; = &quot;Choose one: %@ or %@?&quot;;

You can interpolate with LocalizedStringResources:

Text(&quot;\(LocalizedStringResource(.init(enumOne.rawValue))) \(LocalizedStringResource(.init(enumOne.rawValue)))?&quot;)

At this point it might look better to just have a property in your enum that returns a LocalizedStringResource.

huangapple
  • 本文由 发表于 2023年6月1日 09:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定