Just My Life & My Work

[iOS] 偏好語言 (Prefer Language)

一個能夠跨國際使用的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

參考:Getting current device language in iOS?



WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


%d 位部落客按了讚: