IOS Internationalization Latest Full Tutorial + How to quickly internationalize an off-the-shelf app

Source: Internet
Author: User

Classmate interview encountered a problem, the interviewer asked him, there is a ready-made app to go online, how to not change the original code, also do not change xib, storyboard the case of rapid internationalization. This tutorial should be written by the classmate. Anyway the internationalization of the steps to be done, simply write a detailed, comprehensive tutorial.

First, configure the international language

Click ' + ' under Progect->info->localizations, select the language you want to support, default support in English, such as

Second, the application name internationalization

Create a new file with the strings file type, and the name must be infoplist.

Select the newly created file, click Localize in the file inspector on the right side of Xcode, click Localize, a dialog will pop up, expand the dialog list, select the language we need to localize, such as 中文版, and then click the Localize button in the dialog box.

Then look at the file inspection on the right side of Xcode, and check the Chinese and English that need support, such as

You just new infoplist.stirings left an arrow, click the arrow can expand, you can see Chinese, English two files. Add a code to the English file

" Lfinternationalizationdemo ";

Add a code to the Chinese file

" example of the internationalization of Feng-GE ";

Run your app, check the app name, switch the phone language, run your app, and see the app name.

Third, the text internationalization of the app

As with the application name internationalization, create a new file of type strings, unlike the name must be localizable. Click Localize in the file inspector on the right side of Xcode and follow the steps above until you can expand the two localizable.strings in English and Chinese.

Add code in English file

" Account Number " "  Account " ; " Login " " Login ";

Add code in Chinese file

" Account Number " " Account Number " ; " Login " " Login ";

Then set the text code for the label and Button as follows

Self.lbAccount.text = nslocalizedstring (@ " account ", nil); [ Self.btlogin settitle:nslocalizedstring (@ " login ", nil) Forstate: UIControlStateNormal];
Iv. internationalization of Storyboard or xib

First of all, the Chinese version or the English version of a good, then internationalization, so it will be much easier, I have done a Chinese version of the first.

Check storyboard, as in the previous step, tick the supported languages in the file inspector on the right side of Xcode, as

Expand the storyboard file as

Open Main.strings (中文版), or main.strings (chiness), are found in Chinese. At this time you need to change the Chinese in main.strings to English.

If you feel that the text is dense, you can't tell which text is on which control. You can turn the. strings file into a. storyboard file, like this

At this point you can visually change the language you need to change on the. Storyboard file.

V. Internationalization of pictures and sound files 1. Mode 1

Make two sets of pictures, a set of English name, a set of Chinese name. And then, like the word internationalization, get the current language picture name

NSString *imagename = nslocalizedstring (@ "icon"*image == image;
2. Mode 2

Two sets of pictures of the same name, one set of pictures dragged to the project. Select a picture, as in the previous infoplist.stirings file, click Localize in the file inspector on the right side of Xcode, and tick the language you want to support. Then, right click [email protected]>show in Finder, we found in en. A picture named [email protected] is in the iproj and zh-hans.lproj files. Replace this image with a picture of the corresponding language.

The sound file is the same as the picture and does not say.

Vi. in-app switching language

Switching languages to the phone settings is too much trouble, we can switch languages within the app.

//Automatic follow -up system-(Ibaction) Autolanguage: (ID) Sender {NSString*phonelanguage =[[Nslocale Currentlocale] objectforkey:nslocalelanguagecode]; [Self changelanguage:phonelanguage];}-(Ibaction) Chiness: (IDsender {[Self changelanguage:@"Zh-hans"];}-(Ibaction) 中文版: (IDsender {[Self changelanguage:@"en"];} - (void) Changelanguage: (NSString *) language {[[Nsuserdefaults standarduserdefaults] setobject:@[language] Forkey:@"applelanguages"];    [NSBundle Setmainbundellanguage:language]; NSBundle* Currentmainbundel =[NSBundle Getcurrentmainbundel]; Uistoryboard*storyboard = [Uistoryboard storyboardwithname:@"Main"Bundle:currentmainbundel]; Maintabbarcontroller*tabbar = [Storyboard Instantiateviewcontrollerwithidentifier:@"Maintabbarcontroller"]; [UIApplication Sharedapplication].keywindow.rootviewcontroller=Tabbar; [[UIApplication Sharedapplication].keywindow makekeyandvisible];}

Here Setmainbundellanguage and Getcurrentmainbundel Two methods are encapsulated in the nsbundle category, the code is as follows

#import "nsbundle+lf.h"#import<objc/runtime.h>Static ConstNSString *_bundlekey =@"_bundlekey";@interfaceBundleex:nsbundle@end@implementationBundleex-(NSString *) Localizedstringforkey: (NSString *) key value: (NSString *) value table: (NSString *) tableName {nsbundle*bundle = Objc_getassociatedobject (self, &_bundlekey); returnBundle?[Bundle Localizedstringforkey:key value:value Table:tablename]: [Super Localizedstringforkey:key Value:value Table:tablename];}@end@implementationNSBundle (LF)+ (void) Setmainbundellanguage: (NSString *) Language {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{object_setclass ([NSBundle mainbundle], [Bundleexclass]);        }); Objc_setassociatedobject ([NSBundle mainbundle],&_bundlekey, language? [NSBundle bundlewithpath:[[nsbundle Mainbundle] pathforresource:language ofType:@"lproj"]]: nil, objc_association_retain_nonatomic);}+ (NSBundle *) Getcurrentmainbundel {nsstring* Currentlanguage = [[Nsuserdefaults standarduserdefaults] Objectforkey:@"applelanguages"][0]; NSString*path = [[NSBundle mainbundle] pathforresource:currentlanguage ofType:@"lproj" ]; NSBundle* current =[NSBundle Bundlewithpath:path]; returnCurrent ;}@end

Vii. How to quickly internationalize a ready-made app

Unconsciously written to the dawn, not to be continued

IOS Internationalization Latest Full Tutorial + How to quickly internationalize an off-the-shelf app

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.