iOS implements language localization

Source: Internet
Author: User
Brief Introduction

The localization of the language in iOS is simple, just to make a simple record of the process. A, localized storyboard on the string

1. Create an application (e.g. project name: Localizationdemo)
2. Add localized files


3. Add a label to the Main.storyboard and set the content to setting
At this point, we look at the Main.strings (Chinese (Simplified)) file, which is empty.
4. In order for the file to automatically populate the content, there is a tip: Select Main.storyboard, then select Show the file inspector in the Utilities on the right, then the localization item will localizable Strings switch to Interface Builder Storyboard, and then switch back.


Look at the file again, and then you already have the following

/* Class = "Uilabel"; Text = "Setting"; ObjectID = "CWA-ZV-JKP"; * *
"cwa-zv-jkp.text" = "Setting";

5. Modify the setting to set

/* Class = "Uilabel"; Text = "Setting"; ObjectID = "CWA-ZV-JKP"; * *
"cwa-zv-jkp.text" = "set";

6. Finished, run the program to change the default language in Chinese to see the set typeface two, the string in the localized code familiar with the use of nslocalizedstring: parameter one is key, parameter two is annotation; in code, Any strings that need to be localized are wrapped in nslocalizedstring, such as the output test string: NSLog (nslocalizedstring ("test", "Output Test string"));

There are two ways to export a string that needs to be localized:
A. generate files by genstrings command: Open terminal, enter into project directory with cd command, execute genstrings command genstrings *.swift (if the language you are using is objective-c, change. Swift to. m) 。 Note that the genstrings command does not traverse subfolders, in other words, if the swift files are placed in multiple folders, the files will not be accessed, so the best way is to use the Find command to locate the required files first. Then use the genstrings command to generate the file. Such as:
Find./-name ' *.swift '-print0 | xargs-0 Genstrings-o./zh-hans.lproj/
Description: The role of-print0 and Xargs can refer to the description of the Find command,-O, followed by the parameters specify the target path

B. Select the navigator on the left side of the Xcode, and then select Editor-> Export for Localization on the menu bar ..., and then specify the file directory (temporarily assigned to the desktop bar).

From the desktop path you can see the generated folder, open the folder, which has a suffix named Xliff file, which uses XML format. Open the file and we can see all the strings that need to be localized:

... <body> <trans-unit id= "Cwa-zv-jkp.text" > <source>Setting</source> <ta rget>setting</target> <note>class = "Uilabel"; Text = "Setting"; ObjectID = "CWA-ZV-JKP";</note> </trans-unit> </body> ... <body> <trans-unit ID = "Cfbundlename" > <source>$ (product_name) </source> </trans-unit> <trans-unit ID = "Cfbundleshortversionstring" > <source>1.0</source> </trans-unit> </body>.
. <body> <trans-unit id= "good" > <source>GOOD</source> <note> Output Good string
        ;/note> </trans-unit> <trans-unit id= "Setting" > <source>Setting</source> <note>no comment provided by engineer.</note> </trans-unit> <trans-unit id= "Test" &G
        T <source>Test</source> <note> Test </note> </trans-unit> </body> 

The content under the source label is the string to localize, the taget tag is the localized string, and the note label is the annotation. We found that some of the source did not have the taget tag, we can add it ourselves, and write the localized string, the contents of other Taget tags are also modified to the string we need.
Save after the modification. Then with the same steps above, we choose Import Localization ... Import files.

Then you will see what you have just changed and the contents of the file before the comparison, click Import.

After the success of the import, you can see our localized files, run the program, everything goes well:


This completes the localization.

Reference articles:
[Http://blog.chinaunix.net/uid-20082146-id-83489.html]

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.