iOS app localization

Source: Internet
Author: User
Tags key string

First, Introduction  * Using localization features, you can easily translate applications into multiple languages and even translate them into multiple dialects of the same language * if you want to add localization functionality, you need to create a subdirectory for each supported language, called a "localized folder," which is commonly used. lproj as Extension * When a localized application needs to load a resource, such as a property list, a nib file, the application examines the user's language and region, and looks for a matching localized folder. If the corresponding folder is found, the resources in this folder will be loaded * In order to save time I would like to Li Mingjie Teacher's blog to modify the instructions (a bit lazy, but programmers do not like lazy how can write good things? , besides, Li Mingjie teacher explained very well, I also for better explanation, at the same time to prevent in combing knowledge when there is omission to do so-.-)   Ii. creation of the projectProject directory Structure

Run (because the localization feature is not used, so the effect is the same regardless of what locale the user chooses)

Now want to in the Chinese language environment, for another set of implementations, including: * Change the text in the storyboard file (nib localization, storyboard is also one of the nib)

* Change the login button below picture (image localization)

* Change the text in the dialog box (Tip and OK) (string localization)

* Change app name (app name localization, i.e. localized info.plist file)

Iii. Pre-localization preparation

The first is to create a Chinese localization folder (zh-hans.lproj), let the application support the Chinese language environment first note the check here, we take a look at the project file at this time Where base.lproj refers to the localization of the tick, which means that our project creation defaults to using base localization. This is why every time you create a project, Main.storyboard will be in a base.lproj folder. At the same time, the mouse refers to the system is created by default in an English localization configuration. Here in order not to affect the explanation we remove this tick, only in Chinese and English localization to explain (the base here indicates what language mode can be modified by changing the language environment of the interested students can do their own research). When I remove the tick will delete Main.storyboard and launchscreen.stroyboard, so we need to add these two files in English change and then remove base, the way is to select Main.storyboard,  Check the Chinese to get to the point: because the English localization system has helped us to create a good, we only need to create localization here to select Finish after our project file results: Zh-hans.lproj folder has been created for us, and the same as the En.lproj folder contains a main.storyboard that the Chinese environment will enable the resources in this. (Some students in this step may be generated in the Zh-hans is not main.storyboard but main.strings, this time needs their own choice: As for the. strings file here, the personal guess should be nib code pattern, there is no further to make up. At the same time, a triangle appears to the left of the Main.storyboard in Xcode, and you can find files with 2 versions respectively. Our program supports both English and Chinese environments, and localization is ready.

Iv. Localization of NIB files

Open Main.storyboard (Simplified) file, modify the text information inside (This does not modify the picture)

V. Application name localization (info.plist localization)

Knowledge Background: A key called Cfbundledisplayname in Info.plist determines the name of the application

1. Add a key-value for Info.plist, let the application support name localization, Info.plist will go infoplist.strings load cfbundledisplayname corresponding string, (It is important to note that you cannot localize info.plist directly, because after doing so, the Info.plist file will exist in its own localized folder (like En.lproj), This system will report the Info.plist file does not exist, although you can specify the location of the Info.plist file, but this will not achieve the purpose of localization, so need to use the Infoplist.strings file).

In this way, add the infoplist.strings file separately for English and Chinese localization and add the application has localized Display Name field in Info.plst, assign Yes

Add the respective application name to the corresponding infoplist.strings file

Infoplist.strings (中文版):

Cfbundledisplayname= "Localization";

In Infoplist.strings (SIMPLIFID):

Cfbundledisplayname= "localization";

Vi. Localization of images

(Here are the pictures of the house under the login button in the localization home.png,nib file)

Add the image localization using the Add Infoplist.strings method above, add Home.png file for English and Chinese environment, respectively

View Home.png in the project file below

Use [UIImage imagenamed:@ "Home.png"] in your code; Can

Note: After localizing the picture file, remember to clean the project, and it is best to remove the application before reinstalling

Vii. Localization of strings

1. Create a string resource file The filename is best localizable.strings, and if you use a different file name, the call to use the string is somewhat different

2. Add multilanguage support for Localizable.strings as per the above method

3. in the localizable.strings(中文版) file, add:

tip= "Tip"; ok= "OK"; " Please input your userName "=" Please input your userName ";

  

4. In localizable.strings (Chinese) file join:

tip= "hint"; ok= "good"; " Please input your userName "=" Enter user name ";

  

localizable.strings The string to the left of the equal sign in the file, comment is purely a comment

If the string is not localized or the key corresponding value is not found, nslocalizedstring will return the key string directly

note: If your string resource file name is not localizable.strings , such as Qy.strings, you will have to use nslocalizedstringfromtable () to read the localized string:

Nslocalizedstringfromtable (@ "Tip", @ "qy", nil);

Add: Another way to generate a string resource file (via Terminal command genstrings, you can add it yourself)

1. First add the code that gets the string, for example in VIEWCONTROLLER.M. (In fact, this is convenient, we do not need to localize the string for each place to go to the above localizable.strings file one by one add, we just need to use nslocalizedstring string and then use the Genstrings command can help us improve localizabl E.strings file Now)

NSString *tip = nslocalizedstring (@ "Tip" @ "dialog title");  NSString *ok = nslocalizedstring (@ "OK" @ "dialog button");  

2. Open the terminal, CD to the project folder (including En.lproj and zh-hans.lproj, otherwise will error couldn ' t connect to output directory xx.lproj), here to illustrate, now Xcode After 6 is not recommended to use genstrings, do not know this use batch conversion, but also have to one to the local file generated. But there's no problem with a single deal.

Genstrings Viewcontroller.m-o En.lproj

If you use Nslocalizedstringfromtable (key, tbl, comment) to get a string, the resource file takes the TBL parameter as the file name, such as

NSString *tip = nslocalizedstringfromtable (@ "tip"@ "qy"@ "Dialog title");   *ok = nslocalizedstringfromtable (@ "OK"@ "qy" @" Dialog Button "

The generated resource file is: qy.strings

4. Import the resource file into the project and open the resource file to discover that the key and comment have been generated

Viii. Localization of other documents

The principle of localization is the same as in six, repeat each of the six steps to

Nine, the final effect demonstration

1. In the English environment

2. The Chinese environment

iOS app localization

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.