Swift 本地化:字符串字符串插值

发布于 2025-01-18 20:28:34 字数 781 浏览 2 评论 0原文

我正在尝试为我的应用添加多语言支持。我正在通过用本地化的字符串替换所有用户可见字符串来做到这一点。对于“取消”之类的字符串,这很容易。但是我有很多与信息插值的字符串。困难的部分是,每种语言都会以不同的方式构建句子。

例如:我将如何本地化以下字符串: “请在德语中输入(user.phonenumber)发送给您的4位代码”

,我希望字符串看起来像这样: “ bitte gebe den 4-ziffer代码EIN,den wir dir an(user.phonenumber)gesendet haben”

或法语: “ Entrez le codeà4chiffres qui vous eétédestéEnvoyéà(user.phoneNumber)”

我正在尝试找到一种使用这种本地化文件内部的变量的方法(我知道这不起作用):

内部我的ViewController:

title.text = "Please enter the 4-digit Code sent to you at %phoneNumber%".localized

在我的本地化内部。String

"Please enter the 4-digit Code sent to you at %phoneNumber%" = "Bitte gebe den 4-Ziffer Code ein, den wir dir an %phoneNumber% gesendet haben"

做到这一点的最佳实践是什么?这一定是人们正在遇到的一些常见问题。语言是如此不同!

I am trying to add multilanguage support to my app. I am doing this by replacing all my User visible Strings with localized strings. For strings like "Cancel", this is easy. But I have just as many strings that are interpolated with information. And the hard part is that each language would build the sentence differently.

For example: How would I localize the following String:
"Please enter the 4-digit Code sent to you at (user.PhoneNumber)"

In German, I want the string to look like this:
"Bitte gebe den 4-Ziffer Code ein, den wir dir an (user.PhoneNumber) gesendet haben"

Or in French:
"Entrez le code à 4 chiffres qui vous a été envoyé à (user.PhoneNumber)"

I am trying to find a way to use some kind of variable inside of my localization file like this (I know that this does not work):

Inside my ViewController:

title.text = "Please enter the 4-digit Code sent to you at %phoneNumber%".localized

Inside my Localizable.string

"Please enter the 4-digit Code sent to you at %phoneNumber%" = "Bitte gebe den 4-Ziffer Code ein, den wir dir an %phoneNumber% gesendet haben"

What's the best practice to do this? This must be some common issue people are having. Languages are so different!

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

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

发布评论

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

评论(1

灵芸 2025-01-25 20:28:34

我的本地化文件示例:

“email_error_message”=“链接已发送至%@,请验证”

实际代码实现:

let email = [电子邮件受保护]

让消息 = NSLocalizedString("email_error_message", comment: "错误,错误的电子邮件")

let str = String.localizedStringWithFormat(message, email) //我们想用电子邮件替换 %@。

Print(str)

** 请注意替换的字符串/字符,在上述情况下(电子邮件)将不会被本地化。

my localized file example:

"email_error_message" = "a link is sent to %@ please verify"

Actual code implementation:

let email = [email protected]

let message = NSLocalizedString("email_error_message", comment: "error, wrong email")

let str = String.localizedStringWithFormat(message, email) //we want to replace %@ with email.

Print(str)

** Please note the replaced string/char, in the above case (email) won't be localized.

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