iOS Development-Advanced: Language internationalization and Code switching language

Source: Internet
Author: User

Language internationalization and the use of code switching language is a lot of apps have features, the implementation is also very simple, no technical problems, there are a lot of relevant information on the Internet, after the realization of the function, the whole process is intended to write here. (examples are both Chinese and English, the same principle of replacing other languages, just record the value of the key to save the language)

Demand:

1. Application name following system switch

2. App content follows system switch

3. Use the code to switch the language of the app content ( with background return data switch language )


Realize:

1. Application name following system switch

First select Info under Project, and then add the language that the program needs to use in localizations, Chinese.

Then create a infoplist.strings file, Xcode to the left to select the file, click Localize in Xcode, then add Chinese and 中文版, So the infoplist.string file will be expanded down two files infoplist.strings (English), Infoplist.strings (Chinese ( Simplified));

Enter The code in the infoplist.strings (English) file: cfbundledisplayname="中文版"; In Infoplist.strings (Chinese), enter: cfbundledisplayname="Chinese";

Complete and then run your program, and when you switch languages, you will notice changes to the app name on the desktop.


2. App content follows system switch

Note: If you implement demand 3, which is the language in which code switches the application content, it overrides the method of following the system switchover.

Create a new Localizable.strings file, and click Localize to generate both English and Chinese strings files;

Enter the following in English:

"Hello World" = "Hello world!";
Enter in Chinese:

"Hello World" = "Hey, Worlds!";
Finally write a test function and call him in Viewdidload

-(void) languagewithsystem{    UILabel *testlabel = [[UILabel alloc] Initwithframe:cgrectmake (+, +, +)];    Testlabel.text = nslocalizedstring (@ "Hello World", @ "This String would change with system language!");    [Self.view Addsubview:testlabel];}

Running the program, you will find that the content of this label will be switched based on the language of the system.


3. Use the code to switch the language of the app content (with background return data switch language)

Write a macro definition on the basis of 2 to read the language file and the value returned.

#define Localized (key)  [[NSBundle bundlewithpath:[[nsbundle Mainbundle] pathforresource:[nsstring stringwithformat:@ "%@", [[Nsuserdefaults Standarduserdefaults] objectforkey:@ "Applanguage"]] ofType:@ "Lproj"] Localizedstringforkey: (key) Value:nil table:@ "Localizable"]
Because you need to refresh the page after switching languages, declare a Tomian method in AppDelegate.h to refresh the interface

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {        if (![ [Nsuserdefaults standarduserdefaults]objectforkey:@ "Applanguage"]) {        Nsarray *languages = [NSLocale Preferredlanguages];        NSString *language = [languages objectatindex:0];        if ([Language hasprefix:@ "Zh-hans"]) {//Start match            [[Nsuserdefaults standarduserdefaults] setobject:@ "Zh-hans" Forkey: @ "Applanguage"];        } else{            [[nsuserdefaults standarduserdefaults] setobject:@ "en" forkey:@ "applanguage"];        }    }        [Self tomain];        return YES;} -(void) tomain{    self.window = [[UIWindow alloc] Initwithframe:[uiscreen mainscreen].bounds];    Viewcontroller *VC = [[Viewcontroller alloc] init];    Self.window.rootViewController = VC;        [Self.window makekeyandvisible];}
Write a test function in Viewcontroller

-(void) languagewithbutton{    UILabel *testlabel = [[UILabel alloc] Initwithframe:cgrectmake (+ (+), []];    Testlabel.text = localized (@ "Hello world");    [Self.view Addsubview:testlabel];}
To create a UIButton, click Yes to switch languages

-(void)        viewdidload {[Super viewdidload];        Self.view.backgroundColor = [Uicolor Whitecolor];        [Self languagewithsystem];        [Self Languagewithbutton];    UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];    Btn.frame = CGRectMake (100, 250, 150, 50);    Btn.backgroundcolor = [Uicolor Redcolor];    [Btn settitle:localized (@ "Change button") Forstate:uicontrolstatenormal];    [Btn addtarget:self Action: @selector (changelanguage:) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];} 
-(void) Changelanguage: (ID) sender{    nsstring *language = [[Nsuserdefaults standarduserdefaults]objectforkey:@] Applanguage "];    if ([Language isequaltostring: @ "en"]) {        [[nsuserdefaults standarduserdefaults] setobject:@ "Zh-hans" forkey:@ " Applanguage "];    } else {[        [nsuserdefaults standarduserdefaults] setobject:@ "en" forkey:@ "applanguage"];    }    [[Nsuserdefaults standarduserdefaults] synchronize];    Appdelegate *delegate = (appdelegate *) [uiapplication sharedapplication].delegate;    [Delegate Tomain];}
Then, run the program, and when you click the button, the language is successfully switched through the code.


Finally, the background of the two languages to return data, using a relatively stupid method, the implementation is relatively simple, that is, in the network request, the current app's language sent to the background, the background according to this parameter to return the corresponding language data.


Demo Address: Https://github.com/Nikolilol/LXLocalizationsLanguage/tree/master











iOS Development-Advanced: Language internationalization and Code switching language

Related Article

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.