IOS6 and IOS7 code adaptation (6)--nslocalizedstring

Source: Internet
Author: User

Our applications need to be internationalized, and strings are an important link. In general, we do this through a string resource file, and we need to support several languages to localize the file as many times as possible. The code needs to use the Nslocalizedstring macro, for example, I want to set a TableView title:

Self.navigationItem.title = nslocalizedstring (@ "Test", nil);
we have added both English and Simplified Chinese languages here.

What if the customer chooses a language that we don't have localized? For example, if we choose Traditional Chinese, what does this title show? We generally want to show English. This is certainly not a problem before iOS7, but on the iOS7, you will find that the language chooses the language you used the previous time. That is, on the iOS7, I first select, English, and then change to Simplified Chinese, and finally replaced by traditional Chinese, this will show the Simplified Chinese!


I have also encountered this problem in the development of an application, after the examination, found that this is iOS7 adjust the order of the language caused.

Compare Setting page's Multi-language Settings page:

We can clearly see that, iOS6, English is always located in the second, the rest in accordance with your settings in reverse order, and iOS7 is not the case, it is entirely in accordance with the order of the language you set the reverse arrangement.

When we call nslocalizedstring this macro, the system will look for localized language files in this order. In this way, when we chose Traditional Chinese, the system found that there is no localization of the language, so in order to detect each, iOS6 will use English, and iOS7 will use the previous language.


The reason has been determined, then how can we modify it? There are two ways to do this.

1. Place the default language required by the application in the second position of the language table to ensure that the default language is displayed when the current language is not localized

We know that in the applelanguages inside the Userdefault store this list of current languages, we can get this list and then adjust the order so that we can make sure the default language is placed in the second one. The code is as follows:

    Nsmutablearray *langarray = [[Nsuserdefaults standarduserdefaults] arrayforkey:@ "Applelanguages"];    Nsuinteger Def_lang_index = [Langarray indexofobject:@ "en"];    if (def_lang_index! = 1 && Def_lang_index! = 0) {        nsmutablearray *temparray = [Nsmutablearray arraywithobjects : langarray[0], @ "en", nil];        [Langarray Removeobject:langarray[0]];        [Langarray removeobject:@ "en"];        [Temparray Addobjectsfromarray:langarray];                [[Nsuserdefaults Standarduserdefaults] Setobject:temparray forkey:@ "Applelanguages"];        [[Nsuserdefaults standarduserdefaults]synchronize];    }
It should be noted that this code must be placed in the main.m file Uiapplicationmain () before, otherwise, the application of this time is not working, only the next function.


2. Specify the resource file within the bundle

The first method, though once and for all, feels very rude and not elegant enough! The second way to highlight our programmers on the tall, not much to say, on the code!

static NSBundle *bundle = nil;-(nsstring*) getcurrentlanguage{    Nsarray *langarray = [[Nsuserdefaults Standarduserdefaults] arrayforkey:@ "Applelanguages"];    return langarray[0];} -(void) SetLanguage: (NSString *) L {    NSLog (@ "Preferredlang:%@", l);    NSString *path = [[NSBundle mainbundle] pathforresource:l oftype:@ "Lproj"];    if (!path) {        path = [[NSBundle mainbundle] pathforresource:@ "en" oftype:@ "lproj"];        [Self resetlocalization];    }    bundle = [NSBundle Bundlewithpath:path];    } -(NSString *) Get: (NSString *) key alter: (NSString *) Alternate {    return [bundle Localizedstringforkey:key value: Alternate Table:nil];}
These methods are to obtain the current language, set the language-if there is no resource file is the default language, the last Get:alter: The method is actually called method, of course, this is not nslocalizedstring so convenient, so we use the macro packaging:

#define Mylocalizedstring (key, comment) [Self get: (key) Alter: (comment)]
Use the following:

    [Self setlanguage:[self getcurrentlanguage]];    Self.navigationItem.title = mylocalizedstring (@ "Test", nil);
OK, it's over, just try it.


Both of these methods can be used, the individual certainly recommend the second method, if the second method is expanded, you can write a class yourself, so that we can ensure that all projects are used.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.