[IOS] Use genstrings and nslocalizedstring to localize app text

Source: Internet
Author: User

You do not need to talk about app localization. This article will introduce a simple method to implement string localization.

If we define a title for a button in the code without considering localization, we generally write the following statement:

btn.titleLable.text = @"Example Button";

 

Maybe we have already written a long time before we can consider the localization issue, so we don't have to worry about it. At this time, what we need to do is to scan the code from the beginning, then, modify each string that will be displayed to the user as follows:
(Localization is not required for internally used strings)

btn.titleLable.text = NSLocalizedString(@"btn_title", nil);

Nslocalizedstring is a macro defined in nsbundle. H. It is used to find the value of a key in the localizable. Strings file corresponding to the current system language.
The first parameter is the name of the key, and the second parameter is a comment on the "key-Value Pair". It is automatically added when the localizable. Strings file is generated using the genstrings tool.

 

So far, the localizable. Strings file has not been generated. This is a reverse process, that is, write the call process first and then generate the strings resource file.

When we have fixed all the. M files, the genstrings tool will be available.
1. Start the terminal and enter the project directory.
2. Create two directories and place them in the Resource Directory.
The directory name applies to the language corresponding to the localizable. Strings file and cannot be written incorrectly. Zh-Hans indicates Simplified Chinese. Note that Zh. lproj cannot be used.

mkdir zh-Hans.lproj
mkdir en.lproj

3. Generate the localizable. Strings File

genstrings -o zh-Hans.lproj *.m
genstrings -o en.lproj *.m

-O <folder>: Specifies the directory where the generated localizable. Strings file is stored.
*. M: scans all. M files. The supported files include. h and. java.

4. Right-click the Resources Directory of the project and select new Group to add two directories, zh-Hans.lproj and EN. lproj.
5. Add the generated localizable. Strings file to the newly created group.
6. Finally, modify the content of each key in the localizable. Strings file.

 

After talking about a lot of things, the operation is still very simple. In general, there are two:
1. Use nslocalizedstring in the code to obtain the string to be localized
2. Use genstrings to scan the code file, generate localizable. strings, and add it to the project.

Finally, I wish you an early start to the world.

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.