iOS 語言切換、本地化,國際化

什麼是本地化處理?swift

本地化處理就是咱們的應用程序有可能發佈到世界的不少國家去,由於每一個國家應用的語言是不同的,因此咱們要把咱們的應用程序的語言要進行本地化處理一下。app

本地化處理須要處理那些文件?函數

(1):本地化應用程序名稱(個人的項目名稱)測試

(2):本地化字符串處理(項目中所涉及的字符串)spa

(3):本地化圖片3d

(4):XIB文件本地化處理code

(5):其餘文件blog

下面進行本地化處理:圖片

1 . 設置咱們的工程支持多語言ip

爲何:由於只有設置成支持多語言了,咱們才能在展現項目名稱和項目中的字符串等將語言進行切換

(1): 首先建立一個項目,以「蘋果」爲項目名作測試

(2):爲工程添加可支持的語言,只有項目具備咱們想要用的語言,咱們才能講項目切換成這種語言

如圖所示:


 

2:設置項目名稱本地化處理

(1):InfoPlist.strings文件是對咱們應用程序的名字進行管理的

(2):設置InfoPlist.strings其實很簡單,就是藉助咱們上一步設置的語言環境,將咱們想要支持的語言添加到InfoPlist.strings文件之下就能夠了

(3):如圖所示添加過程:

 


 

 


 

(4)接下來咱們只須要將CFBundleDisplayName屬性和它所對應的值分別寫在咱們在InfoPlist下建立的文件中

例如:

在InfoPlist.strings(English)中咱們寫入:CFBundleDisplayName = "apple";

在InfoPlist.strings(Chinese(Simplified))中咱們寫入:CFBundleDisplayName = "蘋果";

在InfoPlist.strings(English)中咱們寫入:CFBundleDisplayName = "リンゴ";

注意:後面的「」前面不加@

(5):在Info.Plist文件中添加一個屬性Application has localized display name而後設置成Boolean類型的Yes就能夠了

 


 

(6):在模擬器中更改模擬器的語言就會看到如下三種效果:

 


 

 


 

 


 

3:本地化字符串處理(項目中所涉及的字符串)

提示:本地化字符串處理和2中講的項目名本地化處理基本上是同樣的,只不過咱們須要在建立一個像Info.Plist文件同樣的文件來管理咱們的本地化字符串

就是建立一個Localizable.strings文件,而後一樣是在這個文件夾下建立多種支持語言

 

在Xcode項目中,在你想要的位置新建文件

 而後選中當前文件,屬性中 點擊 "Localiztion"->"Localize..." ->勾選語言

 

 

 

(1):建立Localizable.strings文件的方式和上面建立Info.Plist的方式是同樣的,只是文件名不同

(2):在Localizable.strings文件下添加支持的語言

如圖所示:

 


 

(3):而後咱們在Localizable.strings下得各個文件添加相似於以下的內容:

例如:

Localizable.strings(English)中       「Login」=「Login」;

Localizable.strings(Chinese(Simplified)) 中   「Login」 = 「登錄」;

Localizable.strings(Japanese) 中  「Login」 = 「登録」;

而後有人可能問了咱們每一個等號前的"Login"是怎麼肯定的? :往下看

而後咱們怎麼用呢?

就是每次咱們在用到咱們設定的字符串的時候不用NSString了,咱們須要用到一個函數代替它:NSLocalizedString(key, comment) 

例如咱們要再一個Button上設定主題:button.title = NSLocalizedString(@「Login」, nil);

這個裏面的@「Login」就會自動檢索於設備相同語言的Localizable.strings下得文件夾,而後在裏面找到與@「Login」相同的Key值,而後就輸出出來

其實咱們在Localizable.strings下得文件夾中設置的內容就像一個字典同樣,前面是Key值後面是對應的值,而後就是經過NSLocalizedString(@「Login」, nil);函數進行檢索,將key對應的值返回過來。

有人會文NSLocalizedString(key, comment)中的comment是什麼,很明顯根據字義判斷是註釋的意思

如圖所示:

 


 

總結:

其實很簡單,就是咱們把Localizable.strings下面設置好咱們所須要的語言,而後咱們在將咱們在項目中用到的字符串都用"Three"="3";的形式整理到各個對應的文件裏面,而後咱們要是用到咱們須要的字符串的時候咱們就經過NSLocalizedString(key, comment)函數以key值的形式找裏面的值,而後將咱們本身設置的語言對應的值展示出來,這就是本地化處理



文/深山老林飛(簡書做者)
原文連接:http://www.jianshu.com/p/b053bbd8c339

【本身的補充】

  

若是是本身的應用本地須要作語言切換的話,實現邏輯以下

 

1.配置的語言

#define lang_en @"en"
#define lang_zh_hans @"zh-Hans"
#define lang_zh_hant @"zh-Hant"

  下面的方法中languageCode: 就是這幾個值, 其餘語言的不作闡述

2.經過當前選擇的語言調用相應語言字符串

/**
 *  獲取國際化字符串值
 *
 *  @param key <#key description#>
 */
- (NSString*)localeStringWithKey:(NSString*)key{
    // Get the language code.
    NSString *languageCode = self.language;
    
    // Get the relevant language bundle.
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:languageCode ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:bundlePath];
    
    // Get the translated string using the language bundle.
    NSString *translatedString = [languageBundle localizedStringForKey:key value:@"" table:nil];
    
    if (translatedString.length < 1) {
        
        // There is no localizable strings file for the selected language.
        translatedString = NSLocalizedStringWithDefaultValue(key, nil, [NSBundle mainBundle], key, key);
    }
    
    return translatedString;
}

  

Swift:

//獲取國際化
enum AppLanguage:String{
    case en = "en"
    case zh_Hans = "zh-Hans"
    case zh_Hant = "zh-Hant"
}
func localString(key:String) -> String{
    // Get the language code.
    let languageCode = XPApplication.shareInstance.language
    
    // Get the relevant language bundle.
    let bundlePath = NSBundle.mainBundle().pathForResource(languageCode, ofType: "lproj")
    let languageBundle = NSBundle.init(path: bundlePath!)
    
    // Get the translated string using the language bundle.
    var translatedString = languageBundle?.localizedStringForKey(key, value: "", table: nil)
    
    if translatedString?.length < 1 {
        
        // There is no localizable strings file for the selected language.
        translatedString = NSLocalizedString(key, tableName: nil, bundle: NSBundle.mainBundle(), value: key, comment: key)
    }
    
    return translatedString!;
}
相關文章
相關標籤/搜索