使用与 NSLocalizedString(@"Text aka.key",@"Description") 中使用的键不同的后备语言

发布于 2024-09-05 02:45:32 字数 361 浏览 3 评论 0 原文

我到处使用 NSLocalizedString(@"Text in deutsch",@"das Textfeld Text in deutsch") 我有两个 Localized.strings 文件。一份用于德语,一份用于英语。

我现在意识到的是。 如果您有德语 iPhone,您会看到德语文本;如果您的 iPhone 设置为英语,您会看到英语文本。但是如果你有法语,那么你也会得到德语文本,因为我使用德语作为键,对吧? 有没有一种方法可以将英语设置为后备语言,而不是我的代码中到处使用的德语? (我有太多次出现 NSLocalizedString ,现在在任何地方更改键都会很痛苦(在代码中,在 Localized.string.en 和 Localized.string.de 中))

I use everywhere NSLocalizedString(@"Text in deutsch",@"das Textfeld Text in deutsch")
I have two Localizable.strings files. One for german and one for english.

What I realized now is.
If you have a german iPhone, you get the german text, if you have your iPhone set to english you get the english text. But if you have lets say french, then you would get the german text, too because I use german as the keys, right ?
Is there a way to set english as fallback language instead of the german one used everywhere in my code ? (I have so many occurences of NSLocalizedString that it would be a pain to change the keys now everywhere (in code, in Localized.string.en and in Localized.string.de))

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

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

发布评论

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

评论(2

相守太难 2024-09-12 02:45:32

您必须将 Info.plist 中的 CFBundleDevelopmentRegion 设置为英语。

这样做,只要不支持用户的首选语言(在您的情况下不是英语和德语),就会使用开发语言。

我通常使用全局函数来包装 NSLocalizedString,并且当存在未翻译的德语短语并且您希望它回退为英语时还提供回退。

public func LS(_ key: String) -> String {
    let value = NSLocalizedString(key, comment: "")
    if value != key || NSLocale.preferredLanguages.first == "en" {
        return value
    }

    // Fall back to en
    guard
        let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
        let bundle = Bundle(path: path)
        else { return value }
    return NSLocalizedString(key, bundle: bundle, comment: "")
}

还有更多关于本地化的内容,我写了更多关于如何回退的内容作品

You have to set CFBundleDevelopmentRegion in your Info.plist to English.

Doing so, whenever the user's preferred language is unsupported (that is other than English and German in your case), then the development language will be used.

I usually use a global func to wrap NSLocalizedString, and also provide a fallback when there is a un-translated German phrase and you want it to fall back to English.

public func LS(_ key: String) -> String {
    let value = NSLocalizedString(key, comment: "")
    if value != key || NSLocale.preferredLanguages.first == "en" {
        return value
    }

    // Fall back to en
    guard
        let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
        let bundle = Bundle(path: path)
        else { return value }
    return NSLocalizedString(key, bundle: bundle, comment: "")
}

There's more about localization and I wrote more about how fallback works.

各空 2024-09-12 02:45:32

始终选择本地化(something.lproj)。如果 l10n 提供翻译,则不会使用密钥本身;这意味着您可以使用“DISCOMBOBULATE_OR_OK”等键,在适合的语言中翻译为“Discombobulate”,在“Discombobulate”翻译对于可用空间来说太长的语言中翻译为“OK”。

可能性:

  • Info.plist 的 CFBundleDevelopmentRegion 中的语言用于选择默认语言。
  • 手机的后备语言受您购买地点的影响(例如,在我的语言屏幕上,“英语”始终显示在第一或第二位。)

值得询问拥有英国/美国/澳大利亚的人手机进行测试。

A localization (something.lproj) is always picked. The keys themselves aren't used if the l10n provides a translation; this means you can use keys like "DISCOMBOBULATE_OR_OK" which translates to "Discombobulate" in languages where it fits and "OK" in languages where the translation for "Discombobulate" is too long for the space available.

Possibilities:

  • The language in Info.plist's CFBundleDevelopmentRegion is used to pick the default language.
  • The phone's fallback language is influenced by where you buy it (For example, on my languages screen, "English" is always displayed either first or second.)

It's worth asking someone with a UK/US/AU phone to test.

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