IOS, localized characters

Source: Internet
Author: User

iOS character localization 1. Localization Repository 1.1. Create a new bundle as a localized repository

1.2. Change the localization repository to the following

1.3. Configure local character (localizable.strings) resources for each language

English

"Group" = "group";

"Name" = "Name";

Chinese Simplified

"Group" = "group";

"Name" = "First-line Day";

Chinese Traditional

"Group" = "group";

"Name" = "one line day";

2. Processing local conversion Extensions 2.1. New bundle Extension Class

2.2. Implementing the NSBundle extension method

//

Nsbundle+localizationstring.h

Localizationstringtest

//

Created by Vie on 2017/1/20.

Copyright? 2017 Vie. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface NSBundle (localizationstring)

/**

Returning localized characters based on system language

@param key Character Key

@return Localized characters

*/

+ (NSString *) Localizationstringforkey: (NSString *) key;

@end

//

Nsbundle+localizationstring.m

Localizationstringtest

//

Created by Vie on 2017/1/20.

Copyright? 2017 Vie. All rights reserved.

//

#import "Nsbundle+localizationstring.h"

@implementation NSBundle (localizationstring)

/**

Returning localized characters based on system language

@param key Character Key

@return Localized characters

*/

+ (NSString *) Localizationstringforkey: (NSString *) key{

static NSBundle const *bundle=nil;

if (Bundle==nil) {

(The language strings acquired by iOS are not stable) The current framework handles only three cases of EN, Zh-hans, zh-hant, and others are handled by default

NSString *language = [Nslocale preferredlanguages].firstobject;

if ([Language hasprefix:@ "en"]) {

Language = @ "en";

} else if ([Language hasprefix:@ "zh"]) {

if ([Language rangeofstring:@ "Hans"].location! = Nsnotfound) {

Language = @ "Zh-hans"; Chinese Simplified

} else {//ZH-HANT\ZH-HK\ZH-TW

Language = @ "Zh-hant"; Traditional Chinese

}

} else {

Language = @ "Zh-hans";

}

Initializing resources

Bundle=[nsbundle bundlewithpath:[[nsbundle bundlewithpath:[[nsbundle Mainbundle] pathForResource:@ " Localizationstring "oftype:@" bundle "] pathforresource:language oftype:@" Lproj "];

}

return [bundle Localizedstringforkey:key Value:nil Table:nil];

}

@end

3. Localized character key3.1. Localized character key implementation

//

LocalizationKey.h

Localizationstringtest

Local Session character Key list

Created by Vie on 2017/1/20.

Copyright? 2017 Vie. All rights reserved.

//

#define GROUP @ "group"

#define NAME @ "Name"

4. Use

#import "Nsbundle+localizationstring.h"

#import "LocalizationKey.h"

NSLog (@ "%@|%@", [NSBundle localizationstringforkey:group],[nsbundle localizationstringforkey:name]);

IOS, localized characters

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.