原文鏈接:http://www.reibang.com/p/66618c845107
想要實(shí)現(xiàn)App內(nèi)的語(yǔ)言切換功能,那么首先要?jiǎng)?chuàng)建國(guó)際化語(yǔ)言文件葛峻,用于存儲(chǔ)對(duì)應(yīng)語(yǔ)言顯示的文本说搅。
1理肺、創(chuàng)建國(guó)際化語(yǔ)言文件
command+N乃摹,選擇Other里面的Empty文件。
我們將其命名為L(zhǎng)ocalizable.strings
選中剛才創(chuàng)建的文件赢乓,在其右側(cè)的的屬性欄中點(diǎn)擊Localize按鈕。
2石窑、設(shè)置項(xiàng)目需要支持的語(yǔ)言
選中project--->info--->Localization牌芋。
我們會(huì)發(fā)現(xiàn)里面默認(rèn)有一個(gè)英語(yǔ),我們可以點(diǎn)擊左下角的?號(hào)添加支持的語(yǔ)言松逊。
點(diǎn)擊+號(hào)躺屁,選擇簡(jiǎn)體中文。
默認(rèn)全選经宏,點(diǎn)擊完成即可:
這時(shí)我們展開Localizable.strings犀暑、Main.storyboard和LaunchScreen.storyboard驯击,會(huì)看到多了一些文件。
3耐亏、文本國(guó)際化
大家看這個(gè)可能會(huì)有疑問徊都,不知道是什么意思。
"首頁(yè)" = "首頁(yè)";
"首頁(yè)" = "Homepage";
其實(shí)這兩個(gè)是一個(gè)文本的不同語(yǔ)言的展示广辰∠窘茫“=”號(hào)左面的相當(dāng)于key值;“=”號(hào)右面的相當(dāng)于value值择吊。不過為了使用方便李根,更容易理解,我這里直接使用了中文的內(nèi)容作為key值几睛。
使用方法的話:NSLocalizedString(@"首頁(yè)", nil);
這樣房轿,在中文語(yǔ)言的時(shí)候返回“首頁(yè)”(中文),在英文語(yǔ)言中返回“ Homepage”(英文)所森。
如果你是用xib或者Storyboard展示文本的話囱持,想要文本國(guó)際化展示的話,需要另外的方法必峰。
首先選中xib或者Storyboard洪唐,選中需要支持的語(yǔ)言。
我們可以看到對(duì)應(yīng)的文件里面有對(duì)應(yīng)的顯示文本吼蚁。如果要想xib隨著設(shè)置的不同語(yǔ)言展示不同的文本的話凭需,可以在這兩個(gè)文件中修改。
4肝匆、本地存儲(chǔ)語(yǔ)言設(shè)置
使用NSUserDefaults存儲(chǔ)本地語(yǔ)言設(shè)置粒蜈。
直接上代碼:這是一位大神分享的代碼,我復(fù)制了一份旗国,效果很不錯(cuò)枯怖,看原文請(qǐng)點(diǎn)這里原文.
存儲(chǔ)、獲取能曾、重置本地語(yǔ)言設(shè)置
#import <Foundation/Foundation.h>
@interface UWConfig : NSObject
/**
用戶自定義使用的語(yǔ)言度硝,當(dāng)傳nil時(shí),等同于resetSystemLanguage
*/
@property (class, nonatomic, strong, nullable) NSString *userLanguage;
/**
重置系統(tǒng)語(yǔ)言
*/
+ (void)resetSystemLanguage;
@end
#import "UWConfig.h"
static NSString *const UWUserLanguageKey = @"UWUserLanguageKey";
#define STANDARD_USER_DEFAULT [NSUserDefaults standardUserDefaults]
@implementation UWConfig
+ (void)setUserLanguage:(NSString *)userLanguage
{
//跟隨手機(jī)系統(tǒng)
if (!userLanguage.length) {
[self resetSystemLanguage];
return;
}
//用戶自定義
[STANDARD_USER_DEFAULT setValue:userLanguage forKey:UWUserLanguageKey];
[STANDARD_USER_DEFAULT setValue:@[userLanguage] forKey:@"AppleLanguages"];
[STANDARD_USER_DEFAULT synchronize];
}
+ (NSString *)userLanguage
{
return [STANDARD_USER_DEFAULT valueForKey:UWUserLanguageKey];
}
/**
重置系統(tǒng)語(yǔ)言
*/
+ (void)resetSystemLanguage
{
[STANDARD_USER_DEFAULT removeObjectForKey:UWUserLanguageKey];
[STANDARD_USER_DEFAULT setValue:nil forKey:@"AppleLanguages"];
[STANDARD_USER_DEFAULT synchronize];
}
@end
切換bundle
#import <Foundation/Foundation.h>
@interface NSBundle (UWUtils)
+ (BOOL)isChineseLanguage;
+ (NSString *)currentLanguage;
@end
#import "NSBundle+UWUtils.h"
#import "UWConfig.h"
#import <objc/runtime.h>
@interface UWBundle : NSBundle
@end
@implementation NSBundle (UWUtils)
+ (BOOL)isChineseLanguage
{
NSString *currentLanguage = [self currentLanguage];
if ([currentLanguage hasPrefix:@"zh-Hans"]) {
return YES;
} else {
return NO;
}
}
/*
* 設(shè)置默認(rèn)語(yǔ)言類型
*/
+ (NSString *)currentLanguage
{
// return [UWConfig userLanguage] ? : [NSLocale preferredLanguages].firstObject;
return [UWConfig userLanguage] ? : @"zh-Hans";
}
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//動(dòng)態(tài)繼承寿冕、交換蕊程,方法類似KVO,通過修改[NSBundle mainBundle]對(duì)象的isa指針驼唱,使其指向它的子類UWBundle藻茂,這樣便可以調(diào)用子類的方法;其實(shí)這里也可以使用method_swizzling來交換mainBundle的實(shí)現(xiàn),來動(dòng)態(tài)判斷辨赐,可以同樣實(shí)現(xiàn)优俘。
object_setClass([NSBundle mainBundle], [UWBundle class]);
});
}
@end
@implementation UWBundle
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
if ([UWBundle uw_mainBundle]) {
return [[UWBundle uw_mainBundle] localizedStringForKey:key value:value table:tableName];
} else {
return [super localizedStringForKey:key value:value table:tableName];
}
}
+ (NSBundle *)uw_mainBundle
{
if ([NSBundle currentLanguage].length) {
NSString *path = [[NSBundle mainBundle] pathForResource:[NSBundle currentLanguage] ofType:@"lproj"];
if (path.length) {
return [NSBundle bundleWithPath:path];
}
}
return nil;
}
@end
5、使用方式
當(dāng)切換app語(yǔ)言的時(shí)候掀序,先存儲(chǔ)本地語(yǔ)言的設(shè)置帆焕;然后刷新界面即可。
- (IBAction)simplifiedChinese:(UIButton *)sender {
[UWConfig setUserLanguage:@"zh-Hans"];
// 刷新界面
[self reloadTabBarViewController];
}
- (IBAction)english:(UIButton *)sender {
[UWConfig setUserLanguage:@"en"];
// 刷新界面
[self reloadTabBarViewController];
}
刷新界面
我這兒是直接刷新了tabBar控制器森枪。
- (void)reloadTabBarViewController {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UITabBarController *tbc = [storyBoard instantiateInitialViewController];
// 跳轉(zhuǎn)到個(gè)人中心頁(yè)面(即設(shè)置語(yǔ)言的那個(gè)tabBarItem)
tbc.selectedIndex = self.tabBarController.selectedIndex;
// 創(chuàng)建設(shè)置頁(yè)面
UIViewController *settingVC = [storyBoard instantiateViewControllerWithIdentifier:@"setting"];
settingVC.hidesBottomBarWhenPushed = YES;
// 創(chuàng)建語(yǔ)言選擇界面
FiFLanguageController *languageVC = [storyBoard instantiateViewControllerWithIdentifier:@"chooseLanguage"];
languageVC.hidesBottomBarWhenPushed = YES;
UINavigationController *nvc = tbc.selectedViewController;
// 備用
NSMutableArray *vcs = nvc.viewControllers.mutableCopy;
[vcs addObjectsFromArray:@[settingVC, languageVC]];
//解決奇怪的動(dòng)畫bug视搏。異步執(zhí)行
dispatch_async(dispatch_get_main_queue(), ^{
//注意刷新rootViewController的時(shí)機(jī),在主線程異步執(zhí)行
//先刷新rootViewController
[UIApplication sharedApplication].keyWindow.rootViewController = tbc;
//然后再給個(gè)人中心的nvc設(shè)置viewControllers
nvc.viewControllers = vcs;
});
}
ok县袱,結(jié)束了浑娜!