標籤:
? 添加關注 作者 FarmGuo 2016.01.27 15:36* 寫了6453字,被44人關注,獲得了56個喜歡 iOS應用內語言切換功能 字數1372 閱讀681 評論7 喜歡7
當我們的應用僅僅面向國內使用者群,一般僅支援一種語言--中文就可以了。當面向國外使用者時就需要進行國際化了,不僅僅是語言的轉變,也可能包括設計風格,頁面配置、互動效果的轉變,如,微博,QQ這類應用都有著切換語言的功能。
iOS常用的國際化流程
1.建立strings檔案。2.在Localization勾選支援的語言,在不同的尾碼的同檔案名稱的strings中設定標題。3.使用NSLocalizedStringFromTable(key, tbl, comment) 這個宏取出key對應的value。
這樣的做法方便快捷,但有一些缺點。1.完全是根據手機系統設定的語言來進行國際化的。2.手機系統語言更改後,需將App Kill後,重新進入才有改變。可以看出給使用者帶來很大的不便,一些使用者根本不知道如何設定語言,而且還去跳出應用,再Kill應用。我們需要的是應用內切換語言,所見即所得 (WYSIWYG)。
應用內切換語言
先看下的做法
111.gif
可以看出選擇新語言,儲存後就直接切換了。這樣的使用者體驗就比較好了。
關鍵1.NSBundle
An NSBundle object represents a location in the file system that groups code and resources that can be used in a program.NSBundle objects locate program resources, dynamically load and unload executable code, and assist in localization. You build a bundle in Xcode using one of these project types: Application, Framework, plug-ins.
2.宏 NSLocalizedStringFromTableInBundle
它的定義
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
Returns a localized version of a string.The key for a string in the specified table.從指定的bundle裡的table中返回對應key的值。
上面兩點就是應用內切換的關鍵
先瀏覽下Demo架構
structure.png
使用storyboard方式,常見的TabBar+Navigation形式,第一個頁面的按鈕來切換語言,第二個來顯示當前語言。
建立FGLanguageTool類來管理語言的切換,因為許多地方都要用到,將其設計為單例。並定義一個宏來載入設定的語言。
#define FGGetStringWithKeyFromTable(key, tbl) [[FGLanguageTool sharedInstance] getStringForKey:key withTable:tbl]#import <Foundation/Foundation.h>@interface FGLanguageTool : NSObject+(id)sharedInstance;/** * 返回table中指定的key的值 * * @param key key * @param table table * * @return 返回table中指定的key的值 */-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table;/** * 改變當前語言 */-(void)changeNowLanguage;/** * 設定新的語言 * * @param language 新語言 */-(void)setNewLanguage:(NSString*)language;@end
對應的.m檔案
#define CNS @"zh-Hans"#define EN @"en"#define LANGUAGE_SET @"langeuageset"#import "AppDelegate.h"#import "FGLanguageTool.h"static FGLanguageTool *sharedModel;@interface FGLanguageTool()@property(nonatomic,strong)NSBundle *bundle;@property(nonatomic,copy)NSString *language;@end@implementation FGLanguageTool+(id)sharedInstance{ if (!sharedModel) { sharedModel = [[FGLanguageTool alloc]init]; } return sharedModel;}-(instancetype)init{ self = [super init]; if (self) { [self initLanguage]; } return self;}-(void)initLanguage{ NSString *tmp = [[NSUserDefaults standardUserDefaults]objectForKey:LANGUAGE_SET]; NSString *path; //預設是中文 if (!tmp) { tmp = CNS; } else { tmp = EN; } self.language = tmp; path = [[NSBundle mainBundle]pathForResource:self.language ofType:@"lproj"]; self.bundle = [NSBundle bundleWithPath:path];}-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table{ if (self.bundle) { return NSLocalizedStringFromTableInBundle(key, table, self.bundle, @""); } return NSLocalizedStringFromTable(key, table, @"");}-(void)changeNowLanguage{ if ([self.language isEqualToString:EN]) { [self setNewLanguage:CNS]; } else { [self setNewLanguage:EN]; }}-(void)setNewLanguage:(NSString *)language{ if ([language isEqualToString:self.language]) { return; } if ([language isEqualToString:EN] || [language isEqualToString:CNS]) { NSString *path = [[NSBundle mainBundle]pathForResource:language ofType:@"lproj"]; self.bundle = [NSBundle bundleWithPath:path]; } self.language = language; [[NSUserDefaults standardUserDefaults]setObject:language forKey:LANGUAGE_SET]; [[NSUserDefaults standardUserDefaults]synchronize]; [self resetRootViewController];}//重新設定-(void)resetRootViewController{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UINavigationController *rootNav = [storyBoard instantiateViewControllerWithIdentifier:@"rootnav"]; UINavigationController *personNav = [storyBoard instantiateViewControllerWithIdentifier:@"personnav"]; UITabBarController *tabVC = (UITabBarController*)appDelegate.window.rootViewController; tabVC.viewControllers = @[rootNav,personNav];}@end
在ViewController裡使用下面類似的方式進行賦值
self.navigationItem.title = FGGetStringWithKeyFromTable(@"RootTitle", @"Main");self.languageLabel.text = FGGetStringWithKeyFromTable(@"NowLanguage", @"Person");
"Main","Person"就是strings檔案的名字。"RootTitle"和"NowLanguage"就是key。
setting.png
這些就是我們的代碼部分,還有一些資源檔要進行配置,就是我們的多語言檔案。
1,添加支援的語言,選擇工程。
add.png
點擊Localizations地區的+號,添加新的語言,我們這裡樣本就選擇簡體中文。
2,建立strings檔案
方法1.選擇一個storyboard,這裡我們使用工程預設的Main.storyboard,在File Inspecter。在Localization欄中勾選支援的語言。系統就會產生對應的檔案。方法2.我們直接建立strings資源檔。在該檔案的File Inspecter的Localization欄中勾選支援的語言。
strings.png
圖中的Main是用方法1建立的,Person是用方法2建立的。這些同名的檔案僅僅依靠尾碼來區分,對應相應的資源。在這些檔案中添加對應的key和value,如
Person.strings(English)檔案中內容
"NowLanguage"="English";
"PersonTitle"="Person";
Person.strings(Chinese(Simplified))檔案中內容
"NowLanguage"="中文";
"PersonTitle"="個人";
即在不同的相同檔案名稱不同尾碼名檔案中設定不同的value。
幾個關鍵點
1,當我們設定支援多語言後建立多語言檔案後,工程檔案結構中多了一些檔案夾,Base是工程預設的,而後面的兩個就是我們設定多語言後自動產生的,對應相應的英文和簡體中文。分別以en和zh-Hans開頭,所以FGLanguageTool中將中文和英文定義為en和zh-Hans來載入對應的bundle。
directory.png
裡面存放的就是對應的strings檔案
stings.png
2.NSLocalizedStringFromTableInBundle,從指定的bundle中去指定table的key的值。
3.更改語言後,重新設定rootViewController,使得可以重新載入語言。
最終效果
change.gif
? 添加關注 作者 FarmGuo 2016.01.27 15:36* 寫了6453字,被44人關注,獲得了56個喜歡 iOS應用內語言切換功能 字數1372 閱讀681 評論7 喜歡7
當我們的應用僅僅面向國內使用者群,一般僅支援一種語言--中文就可以了。當面向國外使用者時就需要進行國際化了,不僅僅是語言的轉變,也可能包括設計風格,頁面配置、互動效果的轉變,如,微博,QQ這類應用都有著切換語言的功能。
iOS常用的國際化流程
1.建立strings檔案。2.在Localization勾選支援的語言,在不同的尾碼的同檔案名稱的strings中設定標題。3.使用NSLocalizedStringFromTable(key, tbl, comment) 這個宏取出key對應的value。
這樣的做法方便快捷,但有一些缺點。1.完全是根據手機系統設定的語言來進行國際化的。2.手機系統語言更改後,需將App Kill後,重新進入才有改變。可以看出給使用者帶來很大的不便,一些使用者根本不知道如何設定語言,而且還去跳出應用,再Kill應用。我們需要的是應用內切換語言,所見即所得 (WYSIWYG)。
應用內切換語言
先看下的做法
111.gif
可以看出選擇新語言,儲存後就直接切換了。這樣的使用者體驗就比較好了。
關鍵1.NSBundle
An NSBundle object represents a location in the file system that groups code and resources that can be used in a program.NSBundle objects locate program resources, dynamically load and unload executable code, and assist in localization. You build a bundle in Xcode using one of these project types: Application, Framework, plug-ins.
2.宏 NSLocalizedStringFromTableInBundle
它的定義
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
Returns a localized version of a string.The key for a string in the specified table.從指定的bundle裡的table中返回對應key的值。
上面兩點就是應用內切換的關鍵
先瀏覽下Demo架構
structure.png
使用storyboard方式,常見的TabBar+Navigation形式,第一個頁面的按鈕來切換語言,第二個來顯示當前語言。
建立FGLanguageTool類來管理語言的切換,因為許多地方都要用到,將其設計為單例。並定義一個宏來載入設定的語言。
#define FGGetStringWithKeyFromTable(key, tbl) [[FGLanguageTool sharedInstance] getStringForKey:key withTable:tbl]#import <Foundation/Foundation.h>@interface FGLanguageTool : NSObject+(id)sharedInstance;/** * 返回table中指定的key的值 * * @param key key * @param table table * * @return 返回table中指定的key的值 */-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table;/** * 改變當前語言 */-(void)changeNowLanguage;/** * 設定新的語言 * * @param language 新語言 */-(void)setNewLanguage:(NSString*)language;@end
對應的.m檔案
#define CNS @"zh-Hans"#define EN @"en"#define LANGUAGE_SET @"langeuageset"#import "AppDelegate.h"#import "FGLanguageTool.h"static FGLanguageTool *sharedModel;@interface FGLanguageTool()@property(nonatomic,strong)NSBundle *bundle;@property(nonatomic,copy)NSString *language;@end@implementation FGLanguageTool+(id)sharedInstance{ if (!sharedModel) { sharedModel = [[FGLanguageTool alloc]init]; } return sharedModel;}-(instancetype)init{ self = [super init]; if (self) { [self initLanguage]; } return self;}-(void)initLanguage{ NSString *tmp = [[NSUserDefaults standardUserDefaults]objectForKey:LANGUAGE_SET]; NSString *path; //預設是中文 if (!tmp) { tmp = CNS; } else { tmp = EN; } self.language = tmp; path = [[NSBundle mainBundle]pathForResource:self.language ofType:@"lproj"]; self.bundle = [NSBundle bundleWithPath:path];}-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table{ if (self.bundle) { return NSLocalizedStringFromTableInBundle(key, table, self.bundle, @""); } return NSLocalizedStringFromTable(key, table, @"");}-(void)changeNowLanguage{ if ([self.language isEqualToString:EN]) { [self setNewLanguage:CNS]; } else { [self setNewLanguage:EN]; }}-(void)setNewLanguage:(NSString *)language{ if ([language isEqualToString:self.language]) { return; } if ([language isEqualToString:EN] || [language isEqualToString:CNS]) { NSString *path = [[NSBundle mainBundle]pathForResource:language ofType:@"lproj"]; self.bundle = [NSBundle bundleWithPath:path]; } self.language = language; [[NSUserDefaults standardUserDefaults]setObject:language forKey:LANGUAGE_SET]; [[NSUserDefaults standardUserDefaults]synchronize]; [self resetRootViewController];}//重新設定-(void)resetRootViewController{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UINavigationController *rootNav = [storyBoard instantiateViewControllerWithIdentifier:@"rootnav"]; UINavigationController *personNav = [storyBoard instantiateViewControllerWithIdentifier:@"personnav"]; UITabBarController *tabVC = (UITabBarController*)appDelegate.window.rootViewController; tabVC.viewControllers = @[rootNav,personNav];}@end
在ViewController裡使用下面類似的方式進行賦值
self.navigationItem.title = FGGetStringWithKeyFromTable(@"RootTitle", @"Main");self.languageLabel.text = FGGetStringWithKeyFromTable(@"NowLanguage", @"Person");
"Main","Person"就是strings檔案的名字。"RootTitle"和"NowLanguage"就是key。
setting.png
這些就是我們的代碼部分,還有一些資源檔要進行配置,就是我們的多語言檔案。
1,添加支援的語言,選擇工程。
add.png
點擊Localizations地區的+號,添加新的語言,我們這裡樣本就選擇簡體中文。
2,建立strings檔案
方法1.選擇一個storyboard,這裡我們使用工程預設的Main.storyboard,在File Inspecter。在Localization欄中勾選支援的語言。系統就會產生對應的檔案。方法2.我們直接建立strings資源檔。在該檔案的File Inspecter的Localization欄中勾選支援的語言。
strings.png
圖中的Main是用方法1建立的,Person是用方法2建立的。這些同名的檔案僅僅依靠尾碼來區分,對應相應的資源。在這些檔案中添加對應的key和value,如
Person.strings(English)檔案中內容
"NowLanguage"="English";
"PersonTitle"="Person";
Person.strings(Chinese(Simplified))檔案中內容
"NowLanguage"="中文";
"PersonTitle"="個人";
即在不同的相同檔案名稱不同尾碼名檔案中設定不同的value。
幾個關鍵點
1,當我們設定支援多語言後建立多語言檔案後,工程檔案結構中多了一些檔案夾,Base是工程預設的,而後面的兩個就是我們設定多語言後自動產生的,對應相應的英文和簡體中文。分別以en和zh-Hans開頭,所以FGLanguageTool中將中文和英文定義為en和zh-Hans來載入對應的bundle。
directory.png
裡面存放的就是對應的strings檔案
stings.png
2.NSLocalizedStringFromTableInBundle,從指定的bundle中去指定table的key的值。
3.更改語言後,重新設定rootViewController,使得可以重新載入語言。
最終效果
change.gif
iOS應用內語言切換功能