iOS 国际化如何指定默认语言
怪我咯
怪我咯 2017-04-18 09:51:57
0
4
379

如题,app支持中文,英文,日文三种语言。希望在其他语言的情况下都使用英文。如何实现啊,网上看到的自己实现Localized,有没有苹果原生的方法来指定啊

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all (4)
巴扎黑

Change the development area in the projectplisttoenand that’s it

CFBundleDevelopmentRegion en

Apple’s multi-language principle

Introduction to nouns
Localizable.stringsMulti-language file, used to localize the strings in the AppLocalizable.strings多语言文件,用于本地化App内的字符串
InfoPlist.strings多语言文件,用于本地化App名称
Base多语言默认文件,被其它多语言 (Chinese, English, ...) 继承和覆盖,不会被iOS直接使用,只有当对应的多语言文件中没有找到相应条目时才会到BaseInfoPlist.stringsMulti-language file, used to localize the App name

Base The multi-language default file is inherited and overridden by other multi-languages (Chinese, English, ...) and will not be used directly by iOS. It will only be used when the corresponding entry is not found in the corresponding multi-language file. It will search in Base, which is similar to the default value of function parameters.


Search principle WhenAppis started,iOSwill traverse the language priority set by the user to match the multi-language files ofApp

If it matches, the corresponding language will be usedIf it does not match, it will useplist

The language corresponding to the development area

zh-Hans
日语ja
英语enSimplified Chinesezh-Hans

Japanese ja

English

More introduction
    刘奇

    Hello, just use theDPLocalizedString方法代替系统的NSLocalizedStringI provided.

    #define CURR_LANG ([[NSLocale preferredLanguages] objectAtIndex:0]) + (NSString *)DPLocalizedString:(NSString *)translation_key { NSString * LocalizableStr = NSLocalizedString(translation_key, nil); if (!([CURR_LANG rangeOfString:@"ja"].length >0) && !([CURR_LANG rangeOfString:@"zh-Hant"].length >0) && !([CURR_LANG rangeOfString:@"zh-Hans"].length >0)) { NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; NSBundle * languageBundle = [NSBundle bundleWithPath:path]; LocalizableStr = [languageBundle localizedStringForKey:translation_key value:@"" table:nil]; } return LocalizableStr; }
      黄舟

      Hello, I will provide you with an idea. The specific implementation may need to be done by yourself according to your needs.

      1. Get the machine default language

      NSArray *languages = [NSLocale preferredLanguages]; //获得首选语言版本 NSString *language = [languages objectAtIndex:0];

      2. Determine the language

      if ([language hasPrefix:@"zh-Hans"]) { //简体中文 //do... } else if ([language hasPrefix:@"zh-TW"] || [language hasPrefix:@"zh-HK"] || [language hasPrefix:@"zh-Hant"]) {//繁体中文 由于繁体又有台湾繁体和香港繁体之分 进行判断然后为繁体 } else if ([language hasPrefix:@"en"]) {//英文 }else if ([language hasPrefix:@"ja"]){//日文 }else{ //设置语言为英文... }
        Ty80

        The internationalization of iOS has always been very troublesome to deal with, and I have always wanted to find a more perfect solution to solve the internationalization problem. I shared an article that was too international before, and now I will analyze it in depth.
        Because the article was written before. The original article has been updated. Will be updated at the end.
        1. Problems
        1. The official has not given a perfect way to deal with internationalization.
        2. You cannot specify other languages to display when this language is not adapted. It will default to the last language selected.
        3. After a major version update, the data update is not compatible with the previous version.
        4. Adding a specific language resource file does not correspond to the language returned by the code.

        2. Internationalization framework idea

        In response to the above problems, I enabled a plist to handle internationalization issues.
        The key and value in the plist correspond to each other. If the current language is obtained and corresponds to the plist, the current language will be displayed, otherwise the specified language will be displayed.

        3. Implementation method
        Add the corresponding language package based on a specific language. Use NSLocalizedString to get the corresponding language.
        Get current language

        NSArray * allLanguages = [defaults objectForKey:@"AppleLanguages"]; NSString * CURR_LANG = [allLanguages objectAtIndex:0];

        Determine whether the current language is the language to be adapted

        if (![CURR_LANG hasPrefix:@"en"] && ![CURR_LANG hasPrefix:@"zh"]) { NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; NSBundle * languageBundle = [NSBundle bundleWithPath:path]; s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil]; }

        s is the current language that is not Chinese or English. Currently, English can be removed, because whether to judge English or not, it must be processed into English. Displayed as follows

        if (![CURR_LANG hasPrefix:@"zh"]) { NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; NSBundle * languageBundle = [NSBundle bundleWithPath:path]; s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil]; }

        Article link

        There are some notes in my notes about summarizing international content.

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!