[Ios development] xcode4.5 application Localization

Source: Internet
Author: User

When developing an APP, we always involve the internationalization of the APP. In ios, the terminology is called localization, which is actually a meaning. In short, it is a different system language, display different application names, string names, image names, and so on. In addition to code, ios can localize almost all types of files.

 


Then I will record how ios can quickly localize the project. [remember not to write the instructions directly in character form. m files or static strings are compiled in a unified manner. These are not conducive to localization. It will be very troublesome when the project requires different language versions for future maintenance ].

 

 

 

1. localized application name

First, I created a simple Application. The default language of the Application is english. The default name of the APP is the name of the created Project. Open the application-info. plist configuration file: Right-click the blank area and select Show Raw Keys/Values. The application name is actually CFBundleDisplayName, as shown in. [Note: CFbundleName is not an attribute of the APP name, instead, CFbundleDisplayName]

 
 


Next, Add a chinese language support for the APP, and select Project-Localization-Add, for example:

 


The pop-up interface allows us to select the file to be localized, infoPlist. strings is essential. You can use this to set different APP names. ViewController should be selected for the moment and used for localization of xib, for example:

 



Expand infoPlist. add CFBundleDisplayName = "Localized" to the strings file (Engilsh). This name is displayed in the English environment. Add CFBundleDisplayName = "Localized" to chinese ", this is the name displayed in the Chinese environment, for example:

 
 


Save it, run the APP again, switch to the system language, and you will find that the APP displays the name we set (if it is found that it has not changed because of the cache, try-Project-clean, delete our demo from the device, and then build)

 


2. localized xib files

Select the xib file to be localized. In the attribute checker view on the right, you will find an additional localization Option. If you select the language to be localized, you will find that the xib file is changed to infoPlist. the list of strings is the same as that of xib (English) and xib (chinese). We can create interfaces for different languages in our respective xib. When the APP needs to display this xib, the system first looks for the xib file in the current language, as shown in:

 

 

 
 

 


Iii. localized strings

A Localize. settings file is required for a localized string. In the code, the NSLocalizeString macro is used to define the string.

Principle: add the added string to the NSLocalizeString macro in the Code. When the program is started, the system loads the corresponding file according to the language to obtain the corresponding string file, the macro in NSLocalizedString can be named "Localizable. strings file, of course, you can create this file yourself, and then manually add the corresponding string.

 


Create this file manually: select project-new file-Resource-Stringfile

 
 


The name must be Localizable.

Then select the created Localizable. strings file and click-Localizae in the localization item of the property checker. We will first add the localization of English and then add the localization of chinese.

 



Now we can see that this file has been changed to both English and Chinese, so we can write it into the respective files:

 
 


To continue in the viewdidload of the code, you need to set the string:

 
 


The first parameter of NSlocalizeString is the content, and the second parameter is the comment generated when the system generates the Localize. strings file. If this parameter is not required, it can be set to nil.

Let's take a look at the effect:

 
 

 

 

 


Iv. Localization of image resources

We can store the Chinese and English image names in the strings file corresponding to the Chinese and English strings, and then obtain the image name through NSLocalizedStringFromTable.


Select the image to be localized, click localize under Localization in Inspector on the right...

 
 


After the previous operation, we added both English and chinese. We found that the image resources were also divided into english and chinese versions, for example:

 
 


To continue, set (replace with) the image resources in the chinese language, as shown below:

 

 


Run the command after saving-clean:

 
 


The same avator.png image resources show different image resources in different language Environments. Is it good?

 

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.