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 in the same language

* If you want to add localization functionality, you need to create a subdirectory for each supported language, called a "localized folder," usually using the. lproj as an extension

* When a localized application needs to load a resource, like, attribute list, 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

Second, the default

Project directory Structure

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

Now I want to change another set of implementations in the Chinese language environment, including:

* Change the text in the nib file (nib localization)

* 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 localized folder (Zh-hans.lproj) in Chinese to allow the application to support the Chinese language environment

Select the resources that you currently need to localize

After selecting Finish, you will find that there is a Chinese localized folder zh-hans.lproj on the hard disk, and the En.lproj folder is a localized folder in English, which is available by default when creating a project.

The Mjviewcontroller.xib and infoplist.strings files in the project have more than one expandable triangle on the left, and you can find files with 2 versions respectively.

Mjviewcontroller.xib (中文版) for mjviewcontroller.xib files in the En.lproj folder

Infoplist.strings (中文版) for infoplist.strings files in the En.lproj folder

Mjviewcontroller.xib (Chinese) corresponds to mjviewcontroller.xib file in Zh-hans.lproj folder

Infoplist.strings (Chinese) corresponds to infoplist.strings file in Zh-hans.lproj folder

Once you're done, you're ready to start localizing.

Iv. Localization of NIB files

Open Mjviewcontroller.xib (Chinese) 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

2. In the Infoplist.strings (中文版) file, add:

[Java]View PlainCopy
    1. Cfbundledisplayname="Localization";

3. In the Infoplist.strings (Chinese) file, add:

[Java]View PlainCopy
    1. Cfbundledisplayname="localization";

Vi. Localization of images

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

1. Click to select Home.png, and then view the view in the upper right corner

2. Select the Localize representative will add home.png to the English localization folder En.lproj

3. Add image Support Chinese language

4. View the home.png in the hard drive

5. And the home.png on the left side of the project is an expandable triangle that expands to discover 2 versions of the file

Home.png (中文版) for home.png files in the En.lproj folder

Home.png (Chinese) corresponds to home.png file in Zh-hans.lproj folder

6. Replace the home.png in the Zh-hans.lproj folder with another picture that is prepared beforehand

7. View Home.png in the project after replacement

8. Use the picture as usual in your code

[Java]View PlainCopy
    1. [UIImage imagenamed:@"Home.png"];

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

2. The file name is preferably localizable.strings, and if you use a different file name, the call to use the string is somewhat different

3. Add multilanguage support for localizable.strings (similar to the image localization above), check the localizable.strings file to see the view in the upper right corner

4. Select the Localize representative will add localizable.strings to the English localization folder En.lproj

5. Add Support Chinese language

6. View the localizable.strings in the hard drive

7. And the localizable.strings on the left side of the project is an expandable triangle that expands to discover 2 versions of the file

Localizable.strings (中文版) for localizable.strings files in the En.lproj folder

Localizable.strings (Chinese) corresponds to localizable.strings file in Zh-hans.lproj folder

8. In localizable.strings (中文版) to add the file:

[Java]View PlainCopy
    1. tip="Tip";
    2. ok="OK";

9. in localizable.strings (Chinese) file join:

[Java]View PlainCopy
    1. tip="hint";
    2. ok="good";

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

[Java]View PlainCopy
    1. NSString *tip = nslocalizedstring (@"tip", nil);
    2. NSString *ok = nslocalizedstring (@"OK", nil);

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 Mj.strings, you will have to use nslocalizedstringfromtable () to read the localized string:

[Java]View PlainCopy
    1. Nslocalizedstringfromtable (@"Tip", @"MJ", nil);

Add: Another way to generate a string resource file (via terminal commands)

1. First add the code that gets the string, for example, in MJVIEWCONTROLLER.M

[Java]View PlainCopy
    1. NSString *tip = nslocalizedstring (@"Tip" @ "Dialog title");
    2. NSString *ok = nslocalizedstring (@"OK" @ "Dialog button");


2. Open the terminal, navigate to the folder where the MJVIEWCONTROLLER.M is located, enter the Genstrings command, generate a string resource file

3. Open the folder, the string resource file has been generated

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

[Java]View PlainCopy
    1. NSString *tip = nslocalizedstringfromtable (@"Tip", @"MJ", @"dialog title");
    2. NSString *ok = nslocalizedstringfromtable (@"OK" @ "MJ" @"dialog button");

The generated resource file is: mj.strings

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

[Java]View PlainCopy
    1. /* Dialog button */
    2. "OK" = "OK";
    3. /* Dialog Title */
    4. "Tip" = "Tip";

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.