IOS learning: iOS program name and content internationalization (localization)

Source: Internet
Author: User

1. iOS program name Internationalization

1.1 create a Single View app template project named Localization.

1.2 after the creation, you can see the following working directory structure file. Click InfoPlist. strings to view the properties on the right and add a language in the Localization column.

1.3 open the corresponding language file after adding the language, such as the addition of English

CFBundleDisplayName = "China ";

Add a Chinese File

CFBundleDisplayName = "China ";

Run. If your simulator is in Chinese at this time, you will see that your program name has changed to China:

Set the language to English in settings. The program name is changed to: China

Then, the program name is internationalized. What about the content?

2. International content

2.1 create a Localization. strings File

2.2 Add the same language as the 1.1 link.

2.3 add language content

Add "Key" = "value"; then use NSLocalizedString (@ "key", @ ""); to read the content.

Add Localization. strings English File

"Key" = "english value ";

Add Localization. strings English File

"Key" = "Chinese content ";

Add a UILabel experiment in ViewController. m.

- (void)viewDidLoad{    [super viewDidLoad];    UILabel *label = [[UILabel alloc] init];    label.frame = CGRectMake(20, 40, 250, 60);    label.text = NSLocalizedString(@"key", @"");    [self.view addSubview:label];}

Run

Switch to English in settings

3. How can I determine the language of the current running environment during running?

3.1 obtain Supported languages

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; // set NSArray * languages = [defaults objectForKey: @ "AppleLanguages"]; NSLog (@ "% @", ages );

Run and print the result:

(

En,

"Zh-Hant ",

"Zh-Hans ",

Fr,

De,

Ja,

Nl,

It,

Es,

Pt,

"Pt-PT ",

Da,

Fi,

Nb,

Sv,

Ko,

Ru,

Pl,

Tr,

Uk,

Ar,

Hr,

Cs,

El,

He,

Ro,

Sk,

Th,

Id,

"En-GB ",

Ca,

Hu,

Vi

)

"Zh-Hant" Traditional Chinese

"Zh-Hans", simplified Chinese

This code gets the languages supported by the current system.

3.2 obtain the current language

    NSArray *languages = [NSLocale preferredLanguages];    NSString *currentLanguage = [languages objectAtIndex:0];    NSLog ( @"%@" , currentLanguage);

Print result:

En

You can use the obtained current language to compare it with the supported language to understand the language.

    if([currentLanguage isEqualToString:@"en"])    {        NSLog(@"current Language:en");    } 

  

 

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.