如何为 iOS 应用设置默认本地化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21460202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:19:14  来源:igfitidea点击:

How to set default localization for iOS app

iosobjective-ciphonexcode

提问by 12pm

I'm developing app for Russian-speaking people. A lot of countries near Russia have the second language - Russian. My app has two localizations: Russian and English. And I need to set Russian localization like default. English should be only for people who use English like device's language.

我正在为讲俄语的人开发应用程序。俄罗斯附近的很多国家都有第二语言——俄语。我的应用有两种本地化版本:俄语和英语。我需要像默认设置一样设置俄语本地化。英语应该只适用于像设备语言一样使用英语的人。

I know that Apple recommends to use localization in priority of preferred languages in settings but I have an important reason to don't follow this recommendation.

我知道 Apple 建议在设置中优先使用首选语言的本地化,但我有一个重要的理由不遵循此建议。

How can I set Russian localization like default?

如何将俄语本地化设置为默认值?

采纳答案by 12pm

I have not found the right solution. Only one way that is suitable for my issue is to use NSLocalizedStringFromTableInBundleinstead NSLocalizedString. This way doesn't allow to localize images or nibs but with strings it works great.

我还没有找到正确的解决方案。只有一个方式,是适合我的问题是使用NSLocalizedStringFromTableInBundle代替NSLocalizedString。这种方式不允许本地化图像或笔尖,但使用字符串效果很好。

Firstly you need to define the current device language:

首先你需要定义当前的设备语言:

NSString *lang = (NSString *)[[NSLocale preferredLanguages] objectAtIndex:0];

Next find the bundle for this language:

接下来找到该语言的包:

NSBundle *myLangBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:lang ofType:@"lproj"]];

If bundle is found then use it in NSLocalizedStringFromTableInBundle:

如果找到捆绑包,则将其用于NSLocalizedStringFromTableInBundle

_label.text = NSLocalizedStringFromTableInBundle(@"LabelText",nil,myLangBundle,nil);

Otherwise find and use default bundle.

否则查找并使用默认包。

回答by Laszlo

I think you need to use the Localization native development regionkey in your info.plistfile.

我认为您需要使用info.plist文件中的Localization native development region密钥。

enter image description here

在此处输入图片说明

回答by eofster

You probably need to set Russian as a value of CFBundleDevelopmentRegionin the Info.plist.

您可能需要将俄语设置为CFBundleDevelopmentRegionInfo.plist 中的值。

CFBundleDevelopmentRegion (String - iOS, OS X) specifies the native region for the bundle. This key contains a string value that usually corresponds to the native language of the person who wrote the bundle. The language specified by this value is used as the default language if a resource cannot be located for the user's preferred region or language.

CFBundleDevelopmentRegion (String - iOS, OS X) 指定包的本地区域。此键包含一个字符串值,该值通常对应于编写包的人的母语。如果无法找到用户首选区域或语言的资源,则使用此值指定的语言作为默认语言。

Update

更新

If you really want and need to override the language priority of the OS, you could use AppleLanguagesuser defaults key (although, as you know, it is not recommended):

如果你真的想要并且需要覆盖操作系统的语言优先级,你可以使用AppleLanguages用户默认键(尽管,如你所知,不推荐):

[[NSUserDefaults standardUserDefaults] setObject:@[@“ru”, @“en”] forKey:@“AppleLanguages”];

回答by Gleb Tarasov

In my case I have only Russian localization with hard-coded strings in my app, but I use libraries with localization bundles. Strings from libraries were in English by default. Because NSBundle.mainBundle.preferredLanguageswere [ "en" ].

就我而言,我的应用程序中只有带有硬编码字符串的俄语本地化,但我使用带有本地化包的库。默认情况下,库中的字符串是英文的。因为NSBundle.mainBundle.preferredLanguages[ "en" ]

They became Russian after I specified app localization language in Info.plist with CFBundleLocalizationskey:

在我使用CFBundleLocalizations密钥在 Info.plist 中指定应用程序本地化语言后,它们变成了俄语:

screenshot

截屏

CFBundleLocalizations(Array - iOS, OS X) identifies the localizations handled manually by your app. If your executable is unbundled or does not use the existing bundle localization mechanism, you can include this key to specify the localizations your app does handle.

Each entry in this property's array is a string identifying the language name or ISO language designator of the supported localization. See “Language and Locale Designations” in Internationalization and Localization Guide in Internationalization Documentation for information on how to specify language designators.

CFBundleLocalizations(Array - iOS, OS X) 标识由您的应用程序手动处理的本地化。如果您的可执行文件未捆绑或不使用现有的捆绑本地化机制,您可以包含此键来指定您的应用程序处理的本地化。

此属性数组中的每个条目都是一个字符串,用于标识支持的本地化的语言名称或 ISO 语言指示符。有关如何指定语言指示符的信息,请参阅国际化文档中的国际化和本地化指南中的“语言和区域设置名称”。

回答by Huralnyk

As eofstersaid you should set AppleLanguageskey in NSUserDefaultsto array of languages with desired order, but in order to work at first lunch you should do it very early, even earlier than applicationWillLunchWithOptions:

正如eofster所说,您应该以所需的顺序将AppleLanguages键设置NSUserDefaults为语言数组,但是为了在第一午餐时工作,您应该很早就这样做,甚至早于applicationWillLunchWithOptions:

As for me this solution works great and has advantage on using NSLocalizedStringFromTableInBundlebecause it works with storyboards and nibs as well.

对我来说,这个解决方案效果很好并且在使用上有优势,NSLocalizedStringFromTableInBundle因为它也适用于故事板和笔尖。

Here is step by step solution on Swift language.

这是 Swift 语言的分步解决方案。

  1. Make subclass of UIApplication as described here: Subclass UIApplication with Swift

  2. Second override initmethod of you UIApplicationsubclass to substitute list of preferred languages on your own.

    override init() {
        if let languages = NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages") as? [String],
            let language = languages.first where !language.hasPrefix("en")
        {
            NSUserDefaults.standardUserDefaults().setObject(["ru", "en"], forKey: "AppleLanguages")
        }
    
        super.init()
    }
    

    Swift 4:

    override init() {
    if let languages = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String],
        let language = languages.first(where:{!
    override init() {
        if let languages = NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages") as? [String],
            let language = languages.first where !language.hasPrefix("en")
        {
            NSUserDefaults.standardUserDefaults().setObject(["ru", "en"], forKey: "AppleLanguages")
        }
    
        super.init()
    }
    
    .hasPrefix("en")}) { UserDefaults.standard.set(["ru", "en"], forKey: "AppleLanguages") } super.init()

    }

  3. That's all. If first preferred language is not English you'll substitute list with Russian as first language.

  1. 如下所述创建UIApplication 的子类Subclass UIApplication with Swift

  2. initUIApplication子类的第二个覆盖方法可以自行替换首选语言列表。

    override init() {
    if let languages = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String],
        let language = languages.first(where:{!
    NSString *result = nil;
    NSString *testLang = NSLocalizedString(@"test.lang.phrase", nil);
    if ([testLang isEqualToString:@"YES"]) {
        result = NSLocalizedString(str, nil);
    }
    if (!result) {
        NSBundle *myLangBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]];
        result = NSLocalizedStringFromTableInBundle(str, nil, myLangBundle,nil);
    }
    
    .hasPrefix("en")}) { UserDefaults.standard.set(["ru", "en"], forKey: "AppleLanguages") } super.init()

    斯威夫特 4:

    ##代码##

    }

  3. 就这样。如果首选语言不是英语,您将用俄语替换列表作为第一语言。

P.S. If you're doing not only localization, but internationalization don't forget to check locale which is used by date pickers, date formatters, number formatters, etc.

PS 如果您不仅要进行本地化,还要进行国际化,请不要忘记检查日期选择器、日期格式化程序、数字格式化程序等使用的语言环境。

回答by Voloda2

preferredLanguages sometimes return "ru_RU" but my lproj is ru.lproj. So I decided to another solutions. I added "test.lang.phrase = "YES"; to all my localizable strings files. I load a default localization if localization isn't found.

优选语言有时会返回“ru_RU”,但我的 lproj 是 ru.lproj。所以我决定另一种解决方案。我添加了 "test.lang.phrase = "YES"; 到我所有的本地化字符串文件中。如果没有找到本地化,我会加载一个默认的本地化。

##代码##