[IOS]app内切换语言国际化

阅读更多

OC:

Util:

#import "LanguageTool.h"
#import "SessionManager.h"

#define CNS @"zh"
#define EN @"en"
#define tap @"change_language"

static NSBundle *localeBundle = nil;
static NSString *currentLanguage = nil;

@interface LanguageTool()
    
    
@end

@implementation LanguageTool
    
+(instancetype)getInstance{
    static LanguageTool *manager = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        manager = [[LanguageTool alloc] init];
    });
    return manager;
}
    
-(void)getLastTimeLanguage{
    
}
    
    
-(void)changeLanguage:(NSString*)language{
    
    currentLanguage = language;
    
    NSString *path = [[NSBundle mainBundle]pathForResource:language ofType:@"lproj"];
    
    if (path && ![@"en" isEqualToString:language]) {
        localeBundle = [NSBundle bundleWithPath:path];
    }else{
        localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]];
    }
}
    
-(void)judgeLanguage{
    
    //Get last change language
    NSString *lastChangeLanguage = nil;
    lastChangeLanguage = [SessionManager getSession:[SessionManager getLastLanguageTap]];
    //    NSLog(@"last chage language: %@",lastChangeLanguage);
    
    //语言优先级:currentLanguage > lastChangeLanguage > system default language
    //user 没有设定语言时:获取系统默认语言
    if (!lastChangeLanguage) {
        if (!currentLanguage) {
            //获取系统默认语言:
            //截取:zh-Hant-HK 去掉区域保留:zh-Hant
            NSLog(@"-------system language:%@",[NSLocale preferredLanguages][0]);
            currentLanguage = [[NSLocale preferredLanguages][0] substringToIndex:2];
            if ([currentLanguage containsString:@"zh"]) {
                
                currentLanguage = [NSString stringWithFormat:@"%@%@",currentLanguage,@"-Hant"];
            }
        }
    }else if(!currentLanguage){
        //当前没有切换语言,app内有设定语言,优先app内语言
        currentLanguage = lastChangeLanguage;
    }
    
}
    
- (NSBundle *)getLocaleBundle{
    
    [self judgeLanguage];
    
    //    NSLog(@"------current language :%@",currentLanguage);
    //    NSLog(@"-------system language:%@",[NSLocale preferredLanguages][0]);
    
    NSString *path = [[NSBundle mainBundle]pathForResource:currentLanguage ofType:@"lproj"];
    
    if (path) {
        localeBundle = [NSBundle bundleWithPath:path];
        
    }else{
        localeBundle= [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]];
    }
    return localeBundle;
}
    
-(NSString *)getTap{
    return tap;
}
    
-(NSString *)getCurrentLanguage{
    //    if (!currentLanguage) {
    //        //截取:zh-Hant-HK 去掉区域保留:zh-Hant
    //        currentLanguage = [[NSLocale preferredLanguages][0] substringToIndex:2];
    //    }
    
    [self judgeLanguage];
    
    NSLog(@"current language:%@",currentLanguage);
    return currentLanguage;
}
    
    @end

 

NSString *account = NSLocalizedStringFromTableInBundle(Account_Lb_Title, nil, [[LanguageTool getInstance] getLocaleBundle] , @"");

 这样就能显示实时切换语言,不用退出app

 

这里是tableview的点击事件,点击切换语言

if(row == 0){
            [self changeLanguage:@"en" BeforeCell:temp AfterCell:indexPath];
//            [self changeLanguage:@"en"];
//            [self changeLanguage:@"en" BeforeCell:_selectRow AfterCell:row]
        }else if(row == 1){
            [self changeLanguage:@"zh-Hant" BeforeCell:temp AfterCell:indexPath];

//            [self changeLanguage:@"zh-Hant"];
        }

 

swift:

原理一样,可以引用上面的Util

调用:

let menuPingTest = NSLocalizedString(Config.advanced.menu_ping_test, tableName: nil, bundle: (LanguageTool.getInstance()?.getLocaleBundle())! ,value: "", comment: "")

 还有其他针对swift的切换办法,参考如下:

https://www.jianshu.com/p/f4a317d83c59

你可能感兴趣的:(国际化)