后备语言iOS(具有不完整的Localizable.strings文件)

时间:2021-08-21 12:42:34

I have an iOS project that is localized into 16 languages. Only some words are not localized (Mainly those that go into an update and the localization office did not deliver in time). For my keys I do not use the english wording, as this can also change if a translator wishes. So now if I just don't have a translation for a language, if falls back to the key that I used. But as this key is not 'human readable' or at least not 'human enjoyable to read' this is a problem.

我有一个本地化为16种语言的iOS项目。只有一些单词没有本地化(主要是那些进入更新并且本地化办公室没有及时交付的单词)。对于我的钥匙,我不使用英语措辞,因为如果翻译人员愿意,这也会改变。所以现在如果我没有语言的翻译,如果我回到我使用的密钥。但由于这个关键不是“人类可读的”或至少不是“人类阅读的乐趣”,这是一个问题。

I did some research but couldn't find a solution to my exact problem. I have fe.:

我做了一些研究,但无法找到解决我确切问题的方法。我有:

Localizable.strings in en.lproj
@"Key1" = @"Value 1"
@"Key2" = @"Value 2"

Localizable.strings in de.lproj
@"Key1" = @"Wert 1"
// Note that @"Key2" is missing here in my de.lproj

I would expect that if I make NSLocalizedString(@"Key2", ...)
and am running on a german phone, it falls back to the english
translation for this key as it exists...

So for now i just copied the english translation into the missing Localizable.strings files. But this is a big hack! But also using the english words as keys seems to be a hack to me!

所以现在我只是将英文翻译复制到缺少的Localizable.strings文件中。但这是一个大黑客!但是使用英语单词作为键似乎对我来说是一个黑客!

Is there any way to tell my app, that it should use f.e. english as the fallback if there is no value for a given key? I tried adding a base localization but this doesn't help...

有没有办法告诉我的应用程序,它应该使用f.e.如果给定密钥没有值,那么英语作为后备?我尝试添加基本本地化但这没有帮助...

Thanks a lot

非常感谢

3 个解决方案

#1


14  

As far as I know, there's no "official" way to do it, but I have implemented functions such as this before:

据我所知,没有“官方”方法可以做到这一点,但我之前已经实现了这样的功能:

NSString * L(NSString * translation_key) {
    NSString * s = NSLocalizedString(translation_key, nil);
    if (![[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"] && [s isEqualToString:translation_key]) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        NSBundle * languageBundle = [NSBundle bundleWithPath:path];
        s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil];
    }
    return s;
}

borrowed from: https://*.com/a/8784451/1403046

借鉴自:https://*.com/a/8784451/1403046

Basically, instead of NSLocalizedString(), which will return the input string, this version will fallback to English if necessary.

基本上,不是NSLocalizedString(),它将返回输入字符串,如果需要,此版本将回退到英语。

#2


0  

Inspired by this and this, my Swift code version:

灵感来自于此,我的Swift代码版本:

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: "")
}

Many developers expect an incomplete translation to fallback on the development language.. but that's not the way Apple choose to behave. I have a pseudocode to help better understand how Apple choose to fallback.

许多开发人员期望不完整的翻译能够回避开发语言......但这并不是Apple选择表现的方式。我有一个伪代码,以帮助更好地了解Apple如何选择回退。

#3


-4  

You can use a Base localization and all unlocalized strings will be taken from this.

您可以使用Base本地化,并且将从中获取所有未本地化的字符串。

#1


14  

As far as I know, there's no "official" way to do it, but I have implemented functions such as this before:

据我所知,没有“官方”方法可以做到这一点,但我之前已经实现了这样的功能:

NSString * L(NSString * translation_key) {
    NSString * s = NSLocalizedString(translation_key, nil);
    if (![[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"] && [s isEqualToString:translation_key]) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        NSBundle * languageBundle = [NSBundle bundleWithPath:path];
        s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil];
    }
    return s;
}

borrowed from: https://*.com/a/8784451/1403046

借鉴自:https://*.com/a/8784451/1403046

Basically, instead of NSLocalizedString(), which will return the input string, this version will fallback to English if necessary.

基本上,不是NSLocalizedString(),它将返回输入字符串,如果需要,此版本将回退到英语。

#2


0  

Inspired by this and this, my Swift code version:

灵感来自于此,我的Swift代码版本:

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: "")
}

Many developers expect an incomplete translation to fallback on the development language.. but that's not the way Apple choose to behave. I have a pseudocode to help better understand how Apple choose to fallback.

许多开发人员期望不完整的翻译能够回避开发语言......但这并不是Apple选择表现的方式。我有一个伪代码,以帮助更好地了解Apple如何选择回退。

#3


-4  

You can use a Base localization and all unlocalized strings will be taken from this.

您可以使用Base本地化,并且将从中获取所有未本地化的字符串。