nslocalizedstring和swiftui的价值

发布于 2025-02-04 08:05:40 字数 734 浏览 1 评论 0原文

我正在尝试本地化具有内部值的文本视图而不会成功!

这是我通常要做的:

// ContentView.swift

Text("Tomato")
/* replaced by */
Text(NSLocalizedString("text-tomato", comment: ""))

// Localizable.strings (en)

"text-tomato" = "Tomato";

但是里面有一个值,我不知道该如何进行:

// ContentView.swift

Text("3 Tomatoes")
/* or */
Text("\(tomatoes.count) Tomatoes")
/* replaced by */
Text(NSLocalizedString("\(tomatoes.count) text-tomatoes", comment: ""))

// Localizable.strings (en)

"%@ text-tomatoes" = "%@ Tomatoes";
/* or maybe */
"(tomatoes.count) text-tomatoes" = "%@ Tomatoes";
/* or maybe */
"%@ text-tomatoes" = "(tomatoes.count) Tomatoes";

我尝试使用%@%lld等无成功。你有什么想法吗?

I'm trying to localize a Text view with a value inside without success!

Here's what I'm usually do:

// ContentView.swift

Text("Tomato")
/* replaced by */
Text(NSLocalizedString("text-tomato", comment: ""))

// Localizable.strings (en)

"text-tomato" = "Tomato";

But with a value inside I don't know how to proceed:

// ContentView.swift

Text("3 Tomatoes")
/* or */
Text("\(tomatoes.count) Tomatoes")
/* replaced by */
Text(NSLocalizedString("\(tomatoes.count) text-tomatoes", comment: ""))

// Localizable.strings (en)

"%@ text-tomatoes" = "%@ Tomatoes";
/* or maybe */
"(tomatoes.count) text-tomatoes" = "%@ Tomatoes";
/* or maybe */
"%@ text-tomatoes" = "(tomatoes.count) Tomatoes";

I have tried to use %@, %lld, value, etc without success. Do you have any idea?

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

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

发布评论

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

评论(3

风和你 2025-02-11 08:05:41

在发布我的问题之后,我终于找到了答案:

// ContentView.swift

Text("3 Tomatoes")
/* or */
Text("\(tomatoes.count) Tomatoes")
/* replaced by */
Text(String(format: NSLocalizedString("%lld text-tomatoes", comment: ""), tomatoes.count))

// Localizable.strings (en)

"%lld text-tomatoes" = "%lld Tomatoes";

它像魅力一样工作!

其他示例: swiftui interpolation in swiftui

I have finally found an answer right after posting my question:

// ContentView.swift

Text("3 Tomatoes")
/* or */
Text("\(tomatoes.count) Tomatoes")
/* replaced by */
Text(String(format: NSLocalizedString("%lld text-tomatoes", comment: ""), tomatoes.count))

// Localizable.strings (en)

"%lld text-tomatoes" = "%lld Tomatoes";

It's working like a charm!

Other example: Localization with String interpolation in SwiftUI

萌辣 2025-02-11 08:05:41

谢谢@alexxnd,

这对我有用:

let notAllowedChar = String(format: NSLocalizedString("TextfieldNotAllowedChar %@", comment: ""), char)

“ TextfieldnotlallowedChar% @” - > “没有许可证角色'%@'”


我遇到了麻烦:

SwiftUI.LocalizedStringKey.FormatArgument(storage: SwiftUI.LocalizedStringKey.FormatArgument.Storage.value( ...

Thank you @Alexxnd

This is what worked for me:

let notAllowedChar = String(format: NSLocalizedString("TextfieldNotAllowedChar %@", comment: ""), char)

"TextfieldNotAllowedChar %@" -> "No permitido el character '%@'"


I was having trouble with:

SwiftUI.LocalizedStringKey.FormatArgument(storage: SwiftUI.LocalizedStringKey.FormatArgument.Storage.value( ...
萌吟 2025-02-11 08:05:40

您不应使用swiftui使用nslocalisterstring,请使用常规text()

Text("Tomatoes", comment: "TODO item")

Text("\(viewModel.tomatoesCount, specifier: "%lld") Tomatoes",
    comment: "TODO item with number")

不要忽略注释,它将在翻译过程中有助于上下文。

准备本地化视图

You should not use NSLocalisedString with SwiftUI, use normal Text():

Text("Tomatoes", comment: "TODO item")

Text("\(viewModel.tomatoesCount, specifier: "%lld") Tomatoes",
    comment: "TODO item with number")

Do not ignore comments, it will help with context during translation.

Read more at Preparing views for localization

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