一個能夠跨國際使用的App,需要多國語言化(Localization),之前介紹的是系統運行時自動轉換字串的語言,然而我們想要知道目前使用者的偏好語言 (Prefer Language),來做更進一步的運用,比如將語言碼當作參數傳遞。
/**
Theme: Language
IDE: Xcode 6
Language: Objective C
Date: 103/10/14
Author: HappyMan
Blog: https://cg2010studio.wordpress.com/
*/
// 記錄語言碼
NSString *languageStr = [[NSLocale preferredLanguages] objectAtIndex:0];
// NSString *languageStr = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
if ([languageStr isEqualToString:@"zh-Hant"]) {// 繁中
self.languageCode = @"2";
}
if ([languageStr isEqualToString:@"zh-Hans"]) {// 簡中
self.languageCode = @"1";
}
if ([languageStr isEqualToString:@"en"]) {// 英語
self.languageCode = @"0";
}
有兩種用法可行,注意事項請看文件說明!
使用說明
+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0);
note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information@property (readonly, copy) NSArray *preferredLocalizations;
a subset of this bundle’s localizations, re-ordered into the preferred order for this process’s current execution environment; the main bundle’s preferred localizations indicate the language (of text) the user is most likely seeing in the UI
隨意留個言吧:)~