"Go" to localize app text using genstring and nslocalizedstring

Source: Internet
Author: User

Original address: http://www.cnblogs.com/U-tansuo/p/IOS_NSLocalizedString.html

iOS provides an easy way to localize, with most of the nslocalizedstring. First look at what the next nslocalizedstring is: #define Nslocalizedstring (Key,comment) [[NSBundle Mainbundle]localizedstringforkey: (Key ) value:@ "" table:nil]; This is a macro that essentially calls the function Localizedstringforkey: So, the macro is actually looking for the resource file name in the current bundle. Localizable.strings the string that the key value key points to, so it is not difficult to understand that there are macros such as: Nslocalizedstringfromtable.

Without considering localization, if you define a title in your code for a button, you will typically write this:

Btn.titleLabel.text = @ "Example button"; When the problem of localization is considered, the following changes are made to the string to be displayed to the User: (for internally used strings it is not necessary to localize) Btn.titileLabel.text = Nslocalizedstring (@ "Btn_title", nil);nslocalizedstring is a macro defined in NSBundle.h that is used to find the value of a key in the Localizable.strings file that corresponds 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", which is automatically added when generating the Loclizable.strings file with the Genstrings tool. So far, the outside has not generated the Localizable.strings file, this is a reverse process, that is, first write the call process, and then generate the strings resource file. When all the. m files are modified outside, it's time to genstring the tool. 1. Start the terminal and enter the directory where the project is located 2. Create a new two directory, it is recommended that the directory name in the resource directory will affect the language of the Localizable.strings file, cannot be written incorrectly. Here Zh-hans refers to Simplified Chinese, note can not be expressed in zh.lproj. mkdir zh-hans.lprojmkdir en.lproj3. Generate localizable.strings file Genstrings-o zh-hans.lproj *.mgenstrings-o en.lproj *.m-o < folder, specify the directory where the generated localizable.strings files are placed *.m, scan all the. m files, and the files supported here also include. H,.java etc. 4. Right click on the resources directory of the project, select "New Group", Add two directories Zh-hans.lproj and en.lproj5. Add the newly generated loclizable.strings file in the new Group 6. Finally in the Localizable.strings file, modify each key corresponding to the content, it is possible.Summary:1. Get the string to localize in the code with nslocalizedstring2. Scan the code file with Genstrings, generate the localizable.strings, and add it to the project.

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.