iOS program to implement a variety of language localization methods, recently to a game for multi-language localization, on the Internet to find some solutions, together with their own a little idea to organize a set of programs and share!
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:
The code is as follows:
Cfbundledisplayname = "program";
where "program" for the English application name, the same in the Infoplish.strings (Chinese) file to add:
The code is as follows:
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:
The code is 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:
The code is as follows:
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:
The code is as follows:
"Btncancel" = "btncancelen.png";
Localizable.strings (Chinese) file by adding:
The code is as follows:
"Btncancel" = "btncancelcn.png";
Then use nslocalizedstring in your code to get the name of the picture:
The code is as follows:
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.
The above mentioned is the entire content of this article, I hope you can enjoy.