iOS Development: Application Multi-language localization solution

Source: Internet
Author: User
Tags addchild string methods

Multiple languages typically have two approaches in an application:

One, the procedure provides to the user oneself choice opportunity;

Second, according to the current user mobile device language automatically switch our app to the corresponding language.

The first approach is relatively simple and completely rely on their own play, here is mainly about the second approach, mainly divided into a number of points:

1. Localized Application name

2. Localized strings

3. Localized pictures

4. Localize other Documents

1. Localized Application name

(1) Click "New File" and then select the iOS resource item on the left side of the pop-up window, and you can see the "String File" icon on the right. Create this file, named "Infoplist" (must be this file name) to generate a infoplist.strings file;

(2) Select Infoplist.strings Click xcode-> view-> Utilities-> File Inspector, in the localization midpoint "+", add Chinese (zh-hans) The type is simplified Chinese and 中文版 should be added automatically. Then on the left side of the infoplish.strings will be a triangle, click on the expansion can see Infoplish.strings (Chinese) and infoplish.strings (Chinese) Two versions of the file;

(3) in the Infoplish.strings (Chinese) file, add:

Cfbundledisplayname = "program";

where "program" for the English application name, the same in the Infoplish.strings (Chinese) file to add:

Cfbundledisplayname = "Application";

Where "Application" is the Chinese name, note: cfbundledisplayname add no double quotes

(4) Edit Info.plist, add a new property application has localized display name, set its type to Boolean, and set its value to Yes

2. Localized strings

(1) Similar to the "Localized application name" First step, click "New File" and then select the iOS resource item on the left side of the pop-up window and see the "String File" icon on the right. Create this file, named "Localizable" (must be the name of this file or the subsequent call will have some differences) to generate a localizable.strings file;

(2) Similar to the "Localized application name" Step two, in the Localizable.strings (Chinese) file, add:

"Welcome" = "Click on the screen to continue ...";

In the same vein, add in the Localizable.strings (Chinese) file:

"Welcome" = "Click on the screen to continue ...";

(3) using nslocalizedstring (< #key #>, < #comment #>) in your code to read the localized string, the code reads as follows:

Cclabelttf *label = [Cclabelttf labelwithstring:nslocalizedstring (@ "Welcome", Nil) fontname:@ "Marker felt" fontsize:18 ];

Cgsize size = [[Ccdirector shareddirector] winsize];

Label.position = CCP (SIZE.WIDTH/2, size.height/2+30);

[Self Addchild:label];

Note: If your strings file name is not localizable but customized, such as wang.strings, then you will have to use nslocalizedstringfromtable () to read the localized string:

Nslocalizedstringfromtable (@ "Welcome", @ "Wang", nil)

3. Localized pictures

Here are two ways, the first and localized string methods similar to the Chinese and English pictures in the name of the corresponding strings file, and then through the nslocalizedstring to get the name of the picture, such as:

Localizable.strings (Chinese) file, add:

"Btncancel" = "btncancelen.png";

Localizable.strings (Chinese) file by adding:

"Btncancel" = "btncancelcn.png";

Then use nslocalizedstring in your code to get the name of the picture:

Ccsprite *btncancel = [Ccsprite spritewithspriteframename:nslocalizedstring (@ "Btncancel", nil)];

BTNCANCEL.POSITION=CCP (S.WIDTH/2,S.HEIGHT/2-40);

[Self addchild:btncancel z:2 tag:104];

The second is more formalized: the point where you want to localize the picture, such as "Icon.png", then xcode-> view-> Utilities-> File Inspector, in the localization midpoint "+" add Chinese (Zh-hans); In the left side of the picture there will be an inverted triangle, the point will appear (Chinese) and (Chinese) of the 2 map, and in the project folder will appear en.lproj files and zh-hans.lproj files; En.lproj file is stored in English version of the picture, zh-hans.lproj stored in Chinese version of the picture, the same as the English picture name, we in the folder directly replace the picture on it, the last use of direct use of the normal name on the line, such as: "Icon.png."

4. Localize other Documents

Localizing other files and localizing pictures The second method is similar to adding a language to the localization and then copying the corresponding version to the En.lproj and Zh-hans.lproj folders, and then to the last reference.

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.