iOS Development-application internationalization, in-app switching language

Source: Internet
Author: User

1. Preface

Their own projects need to be internationalized and require the application of internal switching languages. This can be done, it is not difficult, you can directly poke GitHub to see

Https://github.com/leo90821/LocaliztionStringExchange

2. Principle of implementation

The principle is relatively simple, using nsbundle to switch language packs, mainly using NSBundle method

-localizedStringForKey:value:table: To switch local strings

3. Implementing the Code

1) Business logic

Header file

#import<Foundation/Foundation.h>#defineuser_default_string @ "Userlanguage"#defineApple_languages @ "Applelanguages"/** * App BLL **/@interfaceBll:nsobject/** * BLL Single **/+(instancetype) currentlogic;/** * App Bundle **/+ (NSBundle *) bundle;/** * Initialize language **/- (void) Inituserlanguage;/** * get current language **/-(NSString *) Getcurrentlanguage;/** * Set Language **/- (void) Setuserlanguagewithlanguage: (NSString *) language;@end

Implementation file

#import "BLL.h"@implementationBLLStaticNSBundle *bundle =Nil;-(instancetype) init { self=[Super Init]; returnSelf ;}+(instancetype) currentlogic {StaticBLL *logic =Nil; Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Logic=[[BLL alloc] init];    }); returnlogic;}+ (NSBundle *) Bundle {returnbundle;}/** * Initialize language **/- (void) inituserlanguage {nsuserdefaults*userdefault =[Nsuserdefaults Standarduserdefaults]; NSString*string=[Userdefault valueforkey:user_default_string]; if(string. length = =0) {Nsarray*languages =[Userdefault objectforkey:apple_languages]; NSString*current = [Languages Objectatindex:0]; string=Current ; [Userdefault SetValue:stringforkey:user_default_string];    [Userdefault Synchronize]; } nsstring*path = [[NSBundle mainbundle] Pathforresource:stringOfType:@"lproj"]; Bundle=[NSBundle Bundlewithpath:path];}/** * get current language **/-(NSString *) getcurrentlanguage {nsuserdefaults*userdefault =[Nsuserdefaults Standarduserdefaults]; NSString*language =[Userdefault valueforkey:user_default_string]; returnlanguage;}/** * Set Language **/- (void) Setuserlanguagewithlanguage: (NSString *) Language {nsuserdefaults*userdefault =[Nsuserdefaults Standarduserdefaults]; NSString*path = [[NSBundle mainbundle] pathforresource:language ofType:@"lproj"]; Bundle=[NSBundle Bundlewithpath:path];    [Userdefault setvalue:language forkey:user_default_string]; [Userdefault synchronize];}@end

2) View Controller

#import <UIKit/UIKit.h>#define notification_change_language @ "Changelanguage"@ Interface  viewcontroller:uiviewcontroller@end
#import "ViewController.h"#import "BLL.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*tip;-(Ibaction) Changelanguage: (IDsender; @property (weak, nonatomic) Iboutlet UIButton*button;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.[Self.button settitle:nslocalizedstringfromtable (@" Change",@"MyString", nil) forstate:uicontrolstatenormal]; Self.tip.text= Nslocalizedstringfromtable (@"Hello",@"MyString", nil);}- (void) Viewwillappear: (BOOL) animated {[Super viewwillappear:animated]; [Self addobservers];}- (void) Viewwilldisappear: (BOOL) animated {[Super viewwilldisappear:animated]; [Self removeobservers];}- (void) addobservers {[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (Refreshui) Name: Notification_change_languageObject: nil];}- (void) removeobservers {[[Nsnotificationcenter Defaultcenter] removeobserver:self name:notification_change_language /c4>Object: nil];}- (void) Refreshui {[Self.button settitle:[[bll bundle] Localizedstringforkey:@" Change"Value:nil table:@"MyString"] Forstate:uicontrolstatenormal]; Self.tip.text= [[BLL bundle] Localizedstringforkey:@"Hello"Value:nil table:@"MyString"];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(Ibaction) Changelanguage: (ID) Sender {NSString*language =[[BLL currentlogic] getcurrentlanguage]; if([Language rangeofstring:@"en"].location! =nsnotfound) {[[BLL currentlogic] Setuserlanguagewithlanguage:@"Zh-hans"]; } Else{[[BLL currentlogic] Setuserlanguagewithlanguage:@"en"]; } [[Nsnotificationcenter Defaultcenter] Postnotificationname:notification_change_languageObject: nil];}@end

4. Summary

The principle is relatively simple, using nsbundle to switch language packs, mainly using NSBundle method

-localizedStringForKey:value:table: To switch the local string , the most troublesome thing is to refresh each page needs to set the message observer, this is compared to the egg pain. There is no good way, welcome message exchange.

iOS Development-application internationalization, in-app switching language

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.