[iOS] How to switch display language in iOS development for internationalization

Source: Internet
Author: User

1. In project settings, join Chinese and English in two languages:




2. Create a new localizable.strings file, as a multilingual dictionary, store multiple languages, click on the right side of the localization, check the English:



3. Add a field,

In 中文版, add: "submit_btn_title" = "Go";

In Chinese, add: "submit_btn_title" = " start ";

4. A tool class Gdlocalizablecontroller. To switch the local language:
gdlocalizablecontroller.h//guide-book////Created on 7/16/14.//Copyright (c). All rights reserved.//#import <Foundation/Foundation.h> @interface gdlocalizablecontroller:nsobject+ (nsbundle *) bundle;//get current resource file + (void) inituserlanguage;//initialization language file + (NSString *) userlanguage;//get app current language + (void) setuserlanguage :(NSString *) language;//set the current language @end////gdlocalizablecontroller.m//guide-book////Created by what on 7/16/14.//Copyrig HT (c). All rights reserved.//#import "GDLocalizableController.h" @implementation gdlocalizablecontrollerstatic nsbundle * Bundle = nil;+ (NSBundle *) bundle{return bundle;}    + (void) inituserlanguage{nsuserdefaults *def = [Nsuserdefaults standarduserdefaults];    NSString *string = [def valueforkey:@ "Userlanguage"];        if (String.Length = = 0) {//Get the System current language version number nsarray* languages = [def objectforkey:@ "Applelanguages"];        NSString *current = [languages objectatindex:0];        string = current; [Def SetvAlue:current forkey:@ "Userlanguage"]; [Def synchronize];//persistent.    No words will not be saved}//Get file path nsstring *path = [[NSBundle mainbundle] pathforresource:string oftype:@ "Lproj"];  bundle = [NSBundle bundlewithpath:path];//generate bundle}+ (NSString *) userlanguage{nsuserdefaults *def = [NSUserDefaults    Standarduserdefaults];    NSString *language = [def valueforkey:@ "Userlanguage"]; return language;}        + (void) Setuserlanguage: (NSString *) language{nsuserdefaults *def = [Nsuserdefaults standarduserdefaults];    1. The first step changes the value of the bundle nsstring *path = [[NSBundle mainbundle] pathforresource:language oftype:@ "Lproj"];        bundle = [NSBundle Bundlewithpath:path];    2. Persistence [def setvalue:language forkey:@ "Userlanguage"]; [Def synchronize];} @end




5. Define a macro yourself for easy handling:

-----Multi-language Set # define Chinese @ "Zh-hans" #define English @ "en" #define gdlocalizedstring (key) [[Gdlocalizablecontroller BUNDLE] Localizedstringforkey: (key) value:@ "" Table:nil]

6. Use:

    [Gdlocalizablecontroller Setuserlanguage:chinese];    NSLog (Gdlocalizedstring (@ "Submit_btn_title"));    [Gdlocalizablecontroller Setuserlanguage:english];    NSLog (Gdlocalizedstring (@ "Submit_btn_title"));




Reference: Internationalization within IOS apps, no trailing system language

[iOS] How to switch display language in iOS development for internationalization

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.