Localization/internationalization for iOS languages

Source: Internet
Author: User

Introduction to localization/internationalization of iOS language (graphic)

Language localization, also known as language internationalization. is to automatically set the language of the application to a language consistent with the language of the user's operating system, based on the language settings of the user's operating system. Often some applications need to be available to people in multiple countries, or a country with multiple languages, which requires the application to display the text, pictures and other information, can be spoken in different languages of users to read and understand. In turn, it is internationalized to adapt multiple languages for the same application. Language internationalization is also called language localization, which is to stand in the perspective of each user, refers to the user can be local, native people can read the language information, as the name implies, language localization. In fact, language localization = language Internationalization!
This article will be divided into the following 7 main chapters step by step to explain how to fully localize an app.

    • Configure languages that require internationalization (international readiness)
    • App name localization
    • String Localization in code
    • String localization in the case of multi-person development
    • Picture localization (two ways two ways)
    • View/Toggle Local language
    • Storyboard/xib localization
Configure languages that require internationalization

Configuring a language that requires internationalization is also a preparation for internationalization, whether we are internationalized app names, strings in code, pictures, or Storyboard and xib, all of which need to be prepared for this step (one project needs to be configured only once).

    • Select Project->info->localizations, and then click "+" to add a language that requires internationalization/localization, such as (by default, use Base internationalization is checked):

    • Here is an example of adding French, such as:


    • The following dialog box appears, click Finish directly, such as:


    • Similarly, add Simplified Chinese, Traditional Chinese, Korean, the final result such as:

备注:"Zh-hans" and "zh-hant" are abbreviations for Simplified Chinese and Traditional Chinese. This is the standard abbreviation. H can be uppercase or lowercase. "EN" is an abbreviation for English. Ko is an abbreviation for Korean, and FR is the abbreviation for French. Other languages please Baidu language abbreviations can be queried.

(i) Application name localization/internationalization

App name localization refers to the name of the same app, which displays different names in different locales (that is, the language settings for mobile devices). For example, in the Simplified Chinese environment, the app nameis displayed as "" and is displayed as "weChat" in English environment. The application name localization is started below.

    1. Select Info.plist, press COMMAND + N on the keyboard, choose Strings file (ios->resource->strings file)

    • The file name is Infoplist, and must be the name (due to the difference in computer settings, here I do not display the strings suffix name):

    • When you click Create, a file named Infoplist.strings appears in the Xcode left navigation list, such as:

    • With Infoplist.strings selected, click Localize in the Xcode file inspection (Xcode right-hand checker) to select the language we need to localize, such as:
      注意:Before clicking on localize, make sure that we have added the language that needs to be localized, that is, the one step of configuring the language that requires internationalization (step: project->info->localizations, then click "+", add the need to internationalize/ Localized language).

    • When you click Localize, a dialog box pops up and a list of dialogs is displayed, and the language shown in the drop-down list is the language we configured above to be internationalized, select the language we need to localize, and then click the Localize button in the dialog box, such as:

Snip20160617_10.png


注意:If we do not configure a language in PROJECT that requires internationalization (project->info->localizations and then click "+"), only the "Base" and "Chinese" options will appear in the drop-down list. English language is the default language of the system, and other languages that require internationalization (such as Simplified Chinese, French) must be manually added through the configuration localization language above.

    • We then found that the file inspection on the right side of Xcode became a style:

    • Next, tick French, Chinese (Zh-hans), Chinese (zh-hant), Korean, for example:

    • At this point, the left side of the infoplist.stirings of Xcode has an extra arrow, click the arrow to expand as shown:

As can be seen, the Infoplist.strings file contains English, French, Chinese (Simplified), Chinese (Traditional), Korean, the files in five languages.
Principle: When the program starts, it automatically loads the corresponding language file under the Infoplist.strings file according to the language set by the operating system, and then displays the name of the application.

    • Next, we set the corresponding name for the file under Infoplist.strings in different languages.

(1) Add the following code to Infoplist.strings (中文版):

// Localizable App Name是App在英语环境环境下显示的名称CFBundleDisplayName = "Localizable App Name";

备注:Cfbundledisplayname can use double quotes, or you can not use double quotes!


(2) Add the following code to Infoplist.strings (French):

CFBundleDisplayName = "Le nom de la localisation de l‘App";

(3) Add the following code to Infoplist.strings (Chinese (Simplified)):

CFBundleDisplayName = "国际化App名称";

(4) Add the following code to Infoplist.strings (Chinese (Traditional)):

CFBundleDisplayName = "國際化App名稱";

(5) Add the following code to Infoplist.strings (Korean):

CFBundleDisplayName = "??? ? ??";

Modify the emulator locale to English. App names such as:


Modify the emulator locale to Chinese (Simplified). App names such as:


Modify the emulator locale to Chinese (traditional). App names such as:


Modify the emulator locale to Franch. App names such as:


Modify the emulator locale to Korean. App names such as:


备注:In the past, localizing the app name requires adding a bool type key named "Application has localized display name" in the Info.plist file and setting its value to Yes (for example). The goal is to have the app support localizing app names. But now you can ignore this step.


Now that the localized app name has been demonstrated, the steps are:

    • In Project settings, add the language you want to localize by clicking "+".
    • Then click Localize in the file inspection on the right side of Xcode to select the language in which the app name needs to be localized.
    • Finally, the name of the app is set in the file for each language in the form of key = value (Cfbundledisplayname = "app name";);
(ii) Localization of strings in code

The so-called localization of strings means that strings within the app display different content in different locales. For example, the "Home" string displays "home" in the Chinese language environment and "home" in English. The following is the beginning of string localization.
In fact, the character localization and app name localization process is the same, just create a different file name (along with the suffix, the file name must be localizable.strings), the other steps are identical. In order to be able to get a thorough understanding of this, we will still post step one by one.

    • As with the application name localization, you first need command + N, choose iOS-Resource-Strings File
    • The file name must be named localizable
      备注: Because my Computer unhide the file suffix name, so it will be automatically complete. strings suffix name.

    • The file was created successfully, viewing the Xcode left-hand navigation list, found a file named Localizable.strings, such as:

    • With the Localizable.strings file selected, click Localize in Xcode in file inspection to select the language we need to localize (and the same as the localized app name), such as:
    • Select English->localize, such as:
    • We then found that the file inspection on the right side of Xcode became a style:
    • Then tick French, Chinese (Zh-hans), Chinese (zh-hant), Korean, such as:
    • At this point, the left side of Xcode has an extra arrow on the left Localizable.stirings, as shown in the following:

    • Then we just need to assign a value to each string in the code that needs to be localized in the corresponding file in the localizable.strings, respectively, in the form of key-value, such as:





    • A string in the localized code, such as:

    • We only need to use the nslocalizedstring (key, comment), which comes with the foundation framework, to get the corresponding string from key and assign it to a string in the code.
// nslocalizedstring (key, comment) Essence // nslocalizestring The first parameter is the content, according to the first parameter to the corresponding language of the file to take the corresponding string, the second parameter will be converted into a string file comments, can pass nil, you can also pass the empty string @ "".  #define nslocalizedstring (key, comment) [[NSBundle Mainbundle] Localizedstringforkey: (key) value:@ "" Table:nil]

    • Operating effects in different locales, such as:
    • Under English environment:

    • In the French environment:

    • In the Simplified Chinese environment:

    • In the traditional Chinese environment:

    • Korean Environment:

In this way, we implement the localization of strings in the code.

Skills
    • In fact, we do not need to add Key-value to Localizable.strings (中文版) files. The reasons are as follows: When the system obtains the corresponding string based on a key, it is returned with key as value if it is not found. So if we do not add click = "hit" In the Localizable.strings file, then in the Chinese context btn the final title will be click.
    • The switch language does not need to be set in the emulator, just set the following in Xcode: Edit->scheme->run->arguments Passed on Launch->-applelanguages (language code). For example, although our simulator is Korean at this point, if the above steps are set to Zh-hans, then the locale will become Chinese, such as:


    • Operating effect:

    • As a result, switching languages is easier, eliminating the need for cumbersome language switching in the simulator settings.
(iii) string localization in the case of multi-person development

In the project development, the independent development is still a few. Often there is a situation where multiple people develop, in which case there is a high likelihood of conflict if multiple people are working on localized files at the same time. On the other hand, we do not want our localization files to be contaminated by each other, that is, we do not want the other party to manipulate our localization files. However, in the code described above, the localization of the string is the default filename "localizable", because when the program is started, the system will load the corresponding file according to the language to get its corresponding string file. This string can be used by the system to generate a file named "Localizable.strings" in the Nslocalizedstring macro. So how do you get the system to load our own named localization files instead of the system default Localizable.strings? This is the use of nslocalizedstringfromtable (< #key #>, < #tbl #>, < #comment #>).
That is, if your strings file name is not localizable but custom, such as Vvs.strings, then you have to use the Nslocalizedstringfromtable macro to read the localized string.

// key: The system takes a string based on key // TBL: The name of the custom strings file // Comment: Can not pass    Nslocalizedstringfromtable (< #key #>, < #tbl #>, < #comment #>)


#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UIButton*btn;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; NSString*title = Nslocalizedstringfromtable (@"Click",@"VVS", nil); [Self.btn settitle:title forstate:uicontrolstatenormal];}@end

    • Running effects, such as:

In this way, we can get rid of other people's strings files and system default Localizable.strings files, and then reinvent themselves.

(d) Image localization

There are two ways to localize a picture, in the first way, like a string in a localized code, by Nslocalizedstring (key,comment) to get the corresponding string, and then get the picture based on that string.

Way One
NSString *imagename = nslocalizedstring (@ "icon"*image == image;

Way Two

First you need to add a language that needs to be localized, and refer to the first chapter to configure the languages that need internationalization. Because I have added a language that needs to be internationalized in the demo demo when localizing the app name. So no further setup is required.

    • Drag the picture into the project, such as "Icon.png", then select Icon.png, expand the file inspection to the right of Xcode, and click Localize, such as:

    • Then, right-click on the Icon.png->show in Finder, which we found in en. The Iproj file has one more picture named Icon.png, such as:

    • Of course, there are no pictures under the Zh-hans.iproj folder, such as:

    • All we need to do is add a zh-hans.iproj name to the icon.png image. Such as:

    • Then drag the icon.png in the zh-hans.iproj into Xcode, such as:

    • It was then found that an expandable triangle appeared to the left of Icon.png. Such as:

    • After unfolding, it is found that it contains two pictures, such as:

Then add the following code to the controller:

#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*ImageView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; NSString*imagename = nslocalizedstring (@"icon", nil); UIImage*image =[UIImage Imagenamed:imagename]; Self.imageView.image=image;}@end

    • The effect of English environment is as follows:
    • The effect of Chinese environment change is as follows:

    • Other locales, such as French, Chinese Traditional, and Korean, are all in the same steps as above. are in correspondence. Add a picture with the same name under the Iproj folder, then drag and drop the picture into Xcode and don't repeat it.
(v) View/switch local language

Principle: When the application starts, it will first read the value corresponding to the key applelanguages in Nsuserdefaults, which is a string array, that is, Our visit to this key named Applelanguages returns a string array that stores the list of languages supported by the app, and the first item in the array is the current default language of the app.

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Nsarray*languages = [[Nsuserdefaults standarduserdefaults] Valueforkey:@"applelanguages"]; NSString*currentlanguage =Languages.firstobject; NSLog (@"Simulator Current language:%@", currentlanguage);}@end

-Console Print Results:


Snip20160620_1.png

Similarly, since we can remove the language array from the Nsuserdefaults by applelanguages This key, we can also assign a value to the Applelanguages key to achieve the effect of switching the local language, and thereafter, We do not need to go to the simulator settings---general----language and region to switch languages.

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //before switching languagesNsarray *langarr1 = [[Nsuserdefaults standarduserdefaults] Valueforkey:@"applelanguages"]; NSString*language1 =Langarr1.firstobject; NSLog (@"before the simulator language switch:%@", Language1); //Toggle LanguageNsarray *lans = @[@"en"]; [[Nsuserdefaults Standarduserdefaults] Setobject:lans forkey:@"applelanguages"]; //after switching languagesNsarray *LANGARR2 = [[Nsuserdefaults standarduserdefaults] Valueforkey:@"applelanguages"]; NSString*language2 =Langarr2.firstobject; NSLog (@"after the simulator language switch:%@", Language2);}@end


Snip20160620_2.png
    • Recall that I mentioned in the tips section above: Switching languages does not need to be set in the simulator, just set the following in Xcode: Edit->scheme->run->arguments Passed on Launch->- Applelanguages (language code). In essence, it is assigned to the key named Applelanguages in Nsuserdefaults.

Not to be continued ...

Localization/internationalization for iOS languages

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.