IOS6 and IOS7 adaptation Code (6)--nslocalizedstring

Source: Internet
Author: User

Our applications all need internationalization, an important part of the string. Generally speaking. We do this through a string resource file, and we need to support multiple languages and how many times we localize this document. The code that needs to be used nslocalizedstring this macro. Example. I want to set the title of a TableView:

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

What if the customer chooses a language that we don't have localized? Example. We chose Traditional Chinese, so what does this title show? We generally want to show English.

This is true in IOS7, but in iOS7, you will find that the language chooses the language you used the previous time.

In other words, on the iOS7, I choose first. English, then change to Chinese Simplified, the last change to traditional Chinese, this time will show Chinese Simplified!


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.

Control the multi-language Settings page of the setting page:

We can see very clearly, iOS6 on. English is always in the second. The rest is arranged in the reverse order of your settings, and iOS7 is not, it is completely reversed in the order in which you set the language.

When we call nslocalizedstring this macro, the system will follow this order to find localized language files. Such When we chose traditional Chinese, the system found that the language was not localized. So according to the order to detect each, iOS6 will use English, and iOS7 will use the previous language.


The reason has been determined, then how do we change 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 ensure that 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 is important to note that this code must be placed before the face Uiapplicationmain () in the main.m file. Otherwise. Applying this time is not working and can only work the next time.


2. Specify the resource file within the bundle

The first method, though once and for all, feels very rude to change. Not elegant enough!

Another way to highlight our program Ape's 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];}
Each of these methods is to obtain the current language, set the language--assuming that no resource files are used in the default language. The last Get:alter: The method is actually called the method, of course, this is not nslocalizedstring so convenient, so we then use the macro packaging:

#define Mylocalizedstring (key, comment) [Self get: (key) Alter: (comment)]
When used for example the following:

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


Both of these methods are available, and the individual certainly recommends another approach, assuming another approach is to expand. Be able to write a class on your own so that we can ensure that all projects are used.





Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

IOS6 and IOS7 adaptation Code (6)--nslocalizedstring

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.