今天項(xiàng)目中添加了一個需求想许,App設(shè)置中有個語言切換伶授,需要實(shí)現(xiàn)的效果是中英繁切換,針對APP國際化進(jìn)行操作流纹,使APP內(nèi)部控件顯示語言可以不隨系統(tǒng)語言做改變糜烹,自由切換:效果簡約如下:
核心方法:
NSLocalizedStringFromTable(<#key#>,<#tbl#>,<#comment#>)
原理即在工程內(nèi)部建立 .String,在內(nèi)部采用鍵值對方式對所需要切換語言的文字進(jìn)行賦值
1.工程內(nèi)部創(chuàng)建.String文件漱凝,命名為Language.String
2.右側(cè)菜單欄點(diǎn)擊Localize疮蹦,彈出菜單選擇English,確定
3.第二步結(jié)束后選擇工程茸炒,選擇PROJECT愕乎,下方Localizations菜單中點(diǎn)擊+號選擇添加中文
4.此時Language.String中會顯示兩個文件阵苇,Language.String(Chinese) 和 Language.String(English),這一步像這兩個文件中添加鍵值對
英文文件中:
"denglu" = "Login";
"account" = "Phone number/email address";
"password" = "Password";
"verification" = "Verification code";
"message" = "SMS verification code";
"forget" = "Forget?";
"newregist" = "Registration";
"question" = "Problems";
中文文件中:
"denglu" = "登錄";
"account" = "請輸入手機(jī)號/郵箱";
"password" = "請輸入登錄密碼";
"verification" = "請輸入驗(yàn)證碼";
"message" = "通過短信驗(yàn)證碼登錄";
"forget" = "忘記密碼感论?";
"newregist" = "新用戶注冊";
"question" = "常見問題";
5.新建一個單例類绅项,ChangeLanguage,.h中添加屬性方法
#import <Foundation/Foundation.h>
@interface ChangeLanguage:NSObject
#define FGGetStringWithKeyFromTable(key, tbl)? [[ChangeLanguage sharedInstance] getStringForKey:key withTable:tbl]
@property(nonatomic,strong)NSString * language;
+(id)sharedInstance;
/**
*? 返回table中指定的key的值
*
*? @param key? key
*? @param table table
*
*? @return 返回table中指定的key的值
*/
-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table;
/**
*? 改變當(dāng)前語言
*/
-(void)changeNowLanguage;
/**
*? 設(shè)置新的語言
*
*? @param language 新語言
*/
-(void)setNewLanguage:(NSString *)language;
@end
6.在.m中實(shí)現(xiàn)方法:
#import"ChangeLanguage.h"
#import"AppDelegate.h"
#define CNS @"zh-Hans"
#define EN @"en"
#define LANGUAGE_SET @"langeuageset"
@interface ChangeLanguage()
@property(nonatomic,strong)NSBundle * bundle;
@end
@implementation ChangeLanguage
static ChangeLanguage * sharedModel;
+(id)sharedInstance{
? ? if(!sharedModel){
? ? ? ? sharedModel=[[ChangeLanguage 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;
? ? //默認(rèn)是中文?
? ? if([tmp isEqualToString:CNS]||!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;NSLog(@"%@",language);
? [[NSUserDefaults standardUserDefaults]setObject:language forKey:LANGUAGE_SET];
? [[NSUserDefaults standardUserDefaults]synchronize];
? [self resetRootViewController];
}
//重新設(shè)置
-(void)resetRootViewController{
? ? ? ?AppDelegate * appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
? ? ? ?UIStoryboard * SB=[UIStoryboard storyboardWithName:@"Main"bundle:nil];
? ? ? ?UITabBarController * tabBarController=[SB
instantiateViewControllerWithIdentifier:@"tabBarController"];
? ? ? ?tabBarController=(UITabBarController *)appDelegate.window.rootViewController;
}
@end
7.具體使用:在具體方法中先調(diào)用changeNowLanguage方法比肄,然后用FGGetStringWithKeyFromTable賦值即可快耿;