IOS language switching, localization, internationalization, and ios Localization

Source: Internet
Author: User

IOS language switching, localization, internationalization, and ios Localization

What is localization?

Localization processing means that our applications may be released to many countries in the world, because the application languages in each country are different, so we need to localize the language of our application.

What files need to be processed during localization?

(1): localized application name (my project name)

(2): localized string processing (strings involved in the Project)

(3): localized Images

(4): XIB file Localization

(5): Other files

Perform localization as follows:

1. Set our project to support multiple languages

Why: because it is set to support multiple languages, we can switch the language in the display project name, project string, and so on.

(1): first create a project and use "apple" as the project name for testing.

(2): Add a supported language for the project. Only when the project has the language we want to use can we switch the project to this language.

:


 

2: set the project name for localization

(1): The InfoPlist. strings file manages the names of our applications.

(2): Setting InfoPlist. strings is actually very simple. You can add the language we want to support to the InfoPlist. strings file by using the language environment we set in the previous step.

(3): Add process:

 


 

 


 

(4) Next we only need to write the CFBundleDisplayName attribute and the value corresponding to it in the file we created in InfoPlist.

For example:

In InfoPlist. strings (English), we write: CFBundleDisplayName = "apple ";

In InfoPlist. strings (Chinese (Simplified), we write: CFBundleDisplayName = "apple ";

In InfoPlist. strings (English), we write: CFBundleDisplayName = "zookeeper ";

Note: No @ is added before @

(5): Add an attribute Application has localized display name to the Info. Plist file and set it to Yes Of The Boolean type.

 


 

(6): You can change the language of the simulator in the simulator to see the following three effects:

 


 

 


 

 


 

3: localized string processing (strings involved in the Project)

Tip: localization string processing is basically the same as the localization processing of the project name mentioned in 2, except that we need to create a file like Info. Plist to manage our localization string.

Create a Localizable. strings file, and then create multiple Supported languages in this folder.

 

Create a file in the desired location in the Xcode Project

Select the current file and click "Localiztion"-> "Localize..."-> select a language.

 

 

 

(1): The method for creating the Localizable. strings file is the same as that for creating Info. Plist above, but the file name is different.

(2): add the Supported languages to the Localizable. strings file.

:

 


 

(3): Add the following content to each file in Localizable. strings:

For example:

"Login" = "Login" in Localizable. strings (English ";

"Login" = "Login" in Localizable. strings (Chinese (Simplified ";

"Login" = "Login" in Localizable. strings (Japan ";

Then someone may ask how the "Login" before each equals sign is determined? : Look down

How can we use it?

That is, we don't need NSString every time we use the set string. We need to use a function to replace it: NSLocalizedString (key, comment)

For example, we need to set the topic on another Button: button. title = NSLocalizedString (@ "Login", nil );

The @ "Login" in this will automatically retrieve the Localizable in the same language of the device. get the folder under strings, find the same Key value as @ "Login" in it, and then output it.

In fact, we use Localizable. the content set in the strings folder is like a dictionary. The first is the Key value followed by the corresponding value, and then through NSLocalizedString (@ "Login", nil ); function to return the value corresponding to the key.

Someone will explain what is the comment in NSLocalizedString (key, comment). It is obvious that it is the meaning of comments according to the meaning of the meaning.

:

 


 

Summary:

In fact, it is very simple, that is, we put Localizable. set the language we need under strings, and then sort the strings we use in the project into the corresponding files in the form of "Three" = "3, then, when we use the required string, we can use the NSLocalizedString (key, comment) function to find the value in the key value, then we will display the value corresponding to the language we set. This is the localization process.



Text/Shen Shan Lao linfei (author of jianshu)
Link: http://www.jianshu.com/p/b053bbd8c339

[Your Own supplement]

  

The implementation logic is as follows:

 

1. configuration language

#define lang_en @"en"#define lang_zh_hans @"zh-Hans"#define lang_zh_hant @"zh-Hant"

In the following method, the languageCode is the value, which is not described in other languages.

2. Call the corresponding language string using the selected language

/*** Get the INTERNATIONAL string value ** @ param key <# key description #> */-(NSString *) localeStringWithKey :( NSString *) key {// Get the language code. NSString * languageCode = self. language; // Get the relevant language bundle. NSString * bundlePath = [[NSBundle mainBundle] pathForResource: languageCode ofType: @ "lproj"]; NSBundle * languageBundle = [NSBundle bundleWithPath: bundlePath]; // Get the translated string using the language bundle. NSString * translatedString = [languageBundle localizedStringForKey: key value: @ "table: nil]; if (translatedString. length <1) {// There is no localizable strings file for the selected language. translatedString = NSLocalizedStringWithDefaultValue (key, nil, [NSBundle mainBundle], key, key);} return translatedString ;}

  

Swift:

// Obtain the internationalized enum AppLanguage: String {case en = "en" case zh_Hans = "zh-Hans" case zh_Hant = "zh-Hant"} func localString (key: String) -> String {// Get the language code. let languageCode = XPApplication. specified instance. language // Get the relevant language bundle. let bundlePath = NSBundle. mainBundle (). pathForResource (languageCode, ofType: "lproj") let Your agebundle = NSBundle. init (path: bundlePath !) // Get the translated string using the language bundle. var translatedString = languageBundle ?. LocalizedStringForKey (key, value: "", table: nil) if translatedString ?. Length <1 {// There is no localizable strings file for the selected language. translatedString = NSLocalizedString (key, tableName: nil, bundle: NSBundle. mainBundle (), value: key, comment: key)} return translatedString !;}

  

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.