1、demo代碼和iOS應用內語言切換后期補上(2016.12.15)
2、demo代碼已加上,因重新寫的demo所以會跟截圖不一樣(2016.12.19)
程序實現國際化
根據手機使用語言加載對應的語言:
1、創建.strings文件,系統默認加載Localizable.strings:
2、設置多語言,這邊設置了中文、英文:
3、.strings文件添加語言,點擊Localizable.strings右側勾選:
4、在各自的響應文件中,按照"key" = "value"的格式配置:
Localizable.strings( Chinese(Simplified) )中,對中文進行適配
"登錄"="登錄";
Localizable.strings( English )中,對英文進行適配
"登錄"="Login";
5、使用:
系統提供的調用方法,一般只使用到前面2個下面進行說明
#define NSLocalizedString(key, comment) \
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
[bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
[bundle localizedStringForKey:(key) value:(val) table:(tbl)]
//默認調用Localizable.strings
self.title = NSLocalizedString(@"登錄",@“這是注釋無影響可填寫nil”);
//調用自定義myName.strings
self.title = NSLocalizedStringFromTable(@"登錄",@"myName",@“這是注釋無影響可填寫nil”);
設置模擬器語言,Product->Scheme->Edit Scheme...
圖片、xib、storyboard同理設置(不同語言圖片切換可使用NSBundle)
改變手機語言效果展示:
iOS應用內語言切換
重點:通過刷新界面或者重新設置rootViewController
通過更改bundle來獲取不同語言,下面是主要代碼
NSString *tmp = [[NSUserDefaults standardUserDefaults]objectForKey:languageKey];
if (tmp.length == 0 || tmp == nil) {
tmp = @"zh-Hans";//默認中文
}
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:tmp ofType:@"lproj"]];
if (bundle)
{
return NSLocalizedStringFromTableInBundle(key, table, bundle, @"");
}
點擊按鈕切換顯示效果如下: