SwiftUI - 本地化的辅助功能标签

发布于 2025-01-14 17:56:29 字数 435 浏览 4 评论 0原文

我在 SwiftUI 中有以下代码,我想将辅助功能标签从英语翻译成波兰语:

Text("Example")
.accessibilityLabel("Color: \(color.description)")

我在 Localized.strings 文件中有这样的翻译:

/* Color: color.description */
"Color: %@" = "Kolor: %@";

/* Color */
"blue" = "niebieski";

我已经用 VoiceOver 对此进行了测试,它显示: “颜色:蓝色” 但应该读: “Kolor: niebieski”

这意味着 color.description (在我的例子中为“蓝色”)没有翻译为“niebieski”。这是为什么?我做错了什么?

I have the following code in SwiftUI and I want to translate accessibility label from English to Polish:

Text("Example")
.accessibilityLabel("Color: \(color.description)")

And I have such translations in Localizable.strings file:

/* Color: color.description */
"Color: %@" = "Kolor: %@";

/* Color */
"blue" = "niebieski";

I've tested this with VoiceOver and it reads:
"Kolor: blue"
But should read:
"Kolor: niebieski"

Which means that color.description ("blue" in my case) wasn't translated to "niebieski". Why is that? What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2025-01-21 17:56:29

您应该使用 NSLocalizedString 来本地化您的字符串。

所以在你的情况下你应该使用

Text("Example")
.accessibilityLabel(String(format: NSLocalizedString("Color: %@", comment: ""),
 color.description))

You should use the NSLocalizedString to localize your string.

So In your case you should be using

Text("Example")
.accessibilityLabel(String(format: NSLocalizedString("Color: %@", comment: ""),
 color.description))
祁梦 2025-01-21 17:56:29

我修好了:)这很简单。

这是一个工作代码:

Text("Example")
  .accessibilityLabel("Color")
  .accessibilityValue(LocalizedStringKey(Color.blue.description))

如果颜色值来自变量(如原始问题中所示),则它将是:

Text("Example")
  .accessibilityLabel("Color")
  .accessibilityValue(LocalizedStringKey(color.description))

重要的是使用 localizedString(value:)
我已将accessibilityLabel 与accessibilityLabel 和accessibilityValue 分开。

I fixed it :) It's quite simple.

Here is a working code:

Text("Example")
  .accessibilityLabel("Color")
  .accessibilityValue(LocalizedStringKey(Color.blue.description))

And if color value is from a variable (like in the original question), it will be:

Text("Example")
  .accessibilityLabel("Color")
  .accessibilityValue(LocalizedStringKey(color.description))

What is important is to use localizedString(value:).
And I've separated accessibilityLabel to separate accessibilityLabel and accessibilityValue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文