[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值