IOS Localization Application (nslocalizedstring)

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. In this case, you need to use the terminal. You are not familiar with the terminal operation commands of the Mac machine. You have learned a few simple information.

Switch Directory: a new file needs to be created later. The new file must be under my project. We can use the Finder to find our project. If you do not know the path, the finder interface is not displayed, simple: Enter the following command in the terminal to display the path on the top of the finder:

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

Once you know the path, you can switch to this directory, that is, the CD command:

cd /Users/gpm0-mac-mini/Documents/GAIADemo_1/GAIADemo



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. Zh-HANT indicates traditional Chinese, en indicates English, and Ja indicates Japanese. Zh-cn zh-tw can be used. The name must not be wrong. Otherwise, it cannot be matched.

mkdir zh-Hans.lproj
mkdir en.lproj
mkdir zh-Hant.lproj
mkdir ja.lproj

3. Generate the localizable. Strings File

genstrings -o zh-Hans.lproj *.m
genstrings -o en.lproj *.m
genstrings -o zh-Hant.lproj *.m
genstrings -o ja.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 project directory and select new group to create a folder.

5. Add the generated file suffix. lproj to the newly created group.

6. Finally, modify the content of each key in the corresponding localizable. Strings file.

 

Related Article

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.