Internationalization in iOS (i)

Source: Internet
Author: User

Preface: In iOS, if the language or region of the system changes, how do we make the format of the language, date, number, and currency units displayed in the app change? Here's an introduction to the simple internationalization approach in iOS:

    • Download a project on GitHub that requires internationalization: https://github.com/MikeFighting/Bilingual
    • To open this project you can see one of the following interfaces:

Then open the storyboard and you will find the controls inside are very simple. For international purposes, we need to add another language to the project. The way to add languages is that Project--->info--->localizations click "+" to add the appropriate language, here we choose Chinese (Simplified) Simplified Chinese. The Launchscreen.strings and Main.storyboard in the popup dialog box are then checked. So our basic work is done, and the following is officially started.

  1. Create the. strings file, click Command + N new file, select Strings file in Resouce, name localizable, This way the system will be loaded with different. strings files in different locales, displaying different languages.
  2. Click on the localizable.strings file, click the Localization button on the right, then select the language you want to localize, select Chinese, and then click Localizable.strings again, and you'll find a few more options for choosing a language below the localizattion on the right. , Base,english,chinese (Simplified) Select English, and you will find another. strings file.
  3. configure different value values for different. String files, the format configured in. String is "KEY" = "value", and note the last semicolon. We add in Localizable.strings (中文版)
    1 " I write%@ lines every day. " " I write%@ lines every day. " ; 2 " Do you like coding? " " Do you like coding? ";

    Add in Localizable.strings (Chinese Simplified)

    " I write%@ lines every day. " " I write%@ line code every day. "; " Do you like coding? " " Do you like programming? ";

  4. Rewrite the appropriate loading method in VIEWCONTROLLER.M to change the original
    1  @" I write 1000000000 lines every day. " ; 2     [_viewcontrollerlikebutotn settitle:@ "do I likecoding? [ Forstate:uicontrolstatenormal];

    To be replaced by:

    1     _viewcontrollernumlabel.text = [NSString stringwithformat:nslocalizedstring (@ "I write%@ lines every day.  ", nil), @1000000000]; 2     [_viewcontrollerlikebutotn settitle:nslocalizedstring (@ "do I likecoding? ", nil) forstate:uicontrolstatenormal];

    At this time, set the language of the system to Simplified Chinese, general---International, Language, Chinese, and then rerun the app, you will find the change of one of the label and a button on the text changed.

  5. Modify the different pictures. At this point you will find, sometimes, we have text on the picture, these words can not be changed by the code, so to load a different picture, using the method in step 4, we give the image name with a key to express, The different picture names are then written as value in different. strings files, which we added in 中文版:
    1 " ImageName " " 中文版 ";

    We add in the Chinese:

    1 " ImageName " " Chinese ";

    Where Chinese and Chinese are pictures of the text, and then add the following code in the VIEWCONTROLELR

    1     _viewcontrollerimageview.image = [UIImage imagenamed:nslocalizedstring (@ "imageName" , nil)];

    In this case, different images will appear when you modify the appropriate language.

  6. Modifies the display of controls that are not drawn. Click Main.storyboard, you will find a main.strings (Chinese Simplified) file below, this will be inside about lable code:
    1 /*  */2"zki-n6-dit.text""Hello I am a lable ";

    Modified to:

    /** * *"zki-n6-dit.text" " Hello, I am a label" ;

    Run the code again, and you'll see that the content that's not changed by code at this time is lable.

  7. There is a detail problem, the format of the number, run the app when you will find that the original display format is 1000000000, but China's use of the digital representation should be 1,000,000,000, the number of Spanish used to represent the way is: 1.000.000.000, this how internationalization? This time you need to add the following code to the Viewcontroller:
    1     New ]; 2     Numberformatter.numberstyle =Nsnumberformatterdecimalstyle; 3     NSString *numberstring = [numberformatter stringfromnumber:@1000000000]; 4     5     _viewcontrollernumlabel.text = [NSString stringwithformat:nslocalizedstring (@ "I write%@ lines every Day. ", nil), numberstring];

    Here we use Nsnumberformatter to convert the digital format, it is important to note that when you run the app, you need to set the corresponding locale to the corresponding region format--International- > China

  8. How do I make the app display a name that's also internationalized? At this point, you need to add a plist file infoplist.strings, and then localizable the file, add the name of the app that needs to be displayed in the corresponding Chinese and Chinese generated, such as: Add in Chinese file: " Cfbundledisplayname "=" bilingual ", rerun the app and you'll see that the app's name also changes with the language. Finally, the internationalization result of the app is:

Internationalization in iOS (i)

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.