Localization of ios languages

  • 2020-05-06 11:42:58
  • OfStack

There are two general approaches to multilingualism in an application:
I. the program provides the user with the opportunity to choose;
2. Automatically switch our app to the corresponding language according to the current user's current mobile device language.

The first approach is relatively simple and completely depends on your own play, here is mainly the second approach, mainly divided into several points:

1. Localized application name
2. Localize the
string 3. Localized images
4. Localize other files

1. Localized application name

(1) click "new file" and select the resource item of IOS on the left of the popup window. The icon "String File" can be seen on the right. Create this file, name it "InfoPlist" (must be this file name) to generate an InfoPlist.strings file;

(2) select InfoPlist.strings click XCode-> View- > Utilities - > File Inspector, in Localization middle point "+", add chinese (zh-Hans) type for simplified Chinese, english should be added automatically. Then there will be an extra triangle on the left side of InfoPlish.strings. Click to expand and you will see two versions of InfoPlish.strings (english) and InfoPlish.strings (chinese).

(3) add:

to the InfoPlish.strings (english) file


CFBundleDisplayName ="Program"; 

Where "Program" is the English application name, the same is added to the InfoPlish.strings (chinese) file:


CFBundleDisplayName =" The application ";

  where "application" is the Chinese name, note: CFBundleDisplayName with or without double quotes

(4) edit es1064en.plist, add a new attribute Application has localized display name, set its type to boolean, and set its value to YES to

  2. Localization string

(1) similar to the first step of "localized application name", click "new file" and then select the resource item of IOS in the left side of the pop-up window. On the right side, the icon of "String File" can be seen. Create this file, name it "Localizable" (must be this file name or the call will make some difference later) to generate an Localizable.strings file;

(2) similar to the second and third step of "localized application name", add:
in the Localizable.strings (english) file "welcome"="Click on the screen to continue...";
  also adds:
to Localizable.strings (chinese) "welcome"=" click the screen to continue..." ;
(3) use NSLocalizedString(<) in your code #key# > , < #comment# > ) to read the localized string 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 custom, such as wang.strings, then you will have to use NSLocalizedStringFromTable() to read the localized string:


NSLocalizedStringFromTable(@"welcome",@"wang", nil)

3. Localized images

Here there are two methods, the first method is similar to the localization string method, the name of the Chinese and English images into the Chinese and English corresponding strings file, and then through NSLocalizedString) to get the image name, such as:

Add:

to Localizable.strings (english) file


"BtnCancel"="BtnCancelEn.png";

Localizable.strings(chinese) add:


"BtnCancel"="BtnCancelCn.png";

Then use NSLocalizedString) in the code to get the image name:


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 formal: click on the image you want to localize, such as "icon.png", then XCode-> View- > Utilities - > File Inspector, add chinese (zh-Hans) to Localization midpoint "+"; An inverted triangle will appear on the left side of the picture. If you click on it, two images (english) and (chinese) will appear, and en.lproj and zh-Hans.lproj will appear in the project folder. Es269en.lproj files are stored in English, zh-Hans.lproj files are stored in Chinese, and the names of Chinese and English pictures are the same.

4. Localize other files

The second method of localizing other files is similar to that of localizing images. Add the language to Localization and then copy the corresponding version to the en.lproj and zh-Hans.lproj folders.

The above is all the content of this article, I hope you can enjoy it.


Related articles: