iOS应用内多语言切换

在项目里面找到PROJECT-info->Localizations,添加支持的语言


屏幕快照 2018-06-19 上午10.15.31.png

创建.string文件,不要和系统的.string文件重名(Localizable)


屏幕快照 2018-06-19 上午10.35.55.png

选中你创建的.string文件,点击xcode右边栏中的Localization中的按钮,添加需要的语言包


屏幕快照 2018-06-19 上午10.42.01.png

添加完毕后系统会自动帮我们生成对应的语言包文件


屏幕快照 2018-06-19 上午10.44.33.png

在对应的语言包文件中添加需要转换语言的字符串


屏幕快照 2018-06-19 上午10.46.03.png
屏幕快照 2018-06-19 上午10.46.15.png

创建一个工具类来切换语言

#import 

@interface ChangeLanguage : NSObject

+ (NSBundle *)bundle;//获取当前资源文件

+ (void)initUserLanguage;//初始化语言文件

+ (NSString *)userLanguage;//获取应用当前语言

+ (void)setUserLanguage:(NSString *)language;//设置当前语言

@end
#import "ChangeLanguage.h"

@implementation ChangeLanguage

static NSBundle *bundle = nil;
+ (NSBundle *)bundle {
    return bundle;
}

//首次加载时检测语言是否存在
+ (void)initUserLanguage {
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *currLanguage = [def valueForKey:@"LocalLanguageKey"];
    if (!currLanguage) {
        NSArray *preferredLanguages = [NSLocale preferredLanguages];
        currLanguage = preferredLanguages[0];
        if ([currLanguage hasPrefix:@"en"]) {
            currLanguage = @"en";
        }else if ([currLanguage hasPrefix:@"zh-Hans"]) {
            currLanguage = @"zh-Hans";
        }else if ([currLanguage hasPrefix:@"zh-HK"] || [currLanguage hasPrefix:@"zh-Hant"]) {
            currLanguage = @"zh-Hant";
        }else {
            currLanguage = @"zh-Hant";
        }
        [def setValue:currLanguage forKey:@"LocalLanguageKey"];
        [def synchronize];
    }
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:currLanguage ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];//生成bundle
}

//获取当前语言
+ (NSString *)userLanguage {
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *language = [def valueForKey:@"LocalLanguageKey"];
    return language;
}

//设置语言
+ (void)setUserLanguage:(NSString *)language {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *currLanguage = [userDefaults valueForKey:@"LocalLanguageKey"];
    if ([currLanguage isEqualToString:language]) {
        return;
    }
    [userDefaults setValue:language forKey:@"LocalLanguageKey"];
    [userDefaults synchronize];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];
}

@end

现在可以写一个切换语言的按钮,切换后发送通知,刷新所有一级页面的UI

- (void)actionSheet:(FDActionSheet *)sheet clickedButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {//简体
        [ChangeLanguage setUserLanguage:@"zh-Hans"];
    }else if (buttonIndex == 1) {//繁体
        [ChangeLanguage setUserLanguage:@"zh-Hant"];
    }else if (buttonIndex == 2) {//英语
        [ChangeLanguage setUserLanguage:@"en"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage"object:self];
}

项目中所有需要转换语言的字符串都要用系统提供的宏来写

NSLocalizedStringFromTableInBundle(@"Change_language", @"Languages", [ChangeLanguage bundle], nil)

第一个参数是要被转换语言的字符串,第二个参数是你创建的.string文件的名称,第三个参数是语言包所在的bundle(在工具类中生成了bundle文件,所以这里固定为[ChangeLanguage bundle])

在AppDelegate中初始化应用语言,每次启动app都会从本地提取用户上次选定的语种

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //初始化应用语言
    [ChangeLanguage initUserLanguage];
    [ChangeLanguage setUserLanguage:[ChangeLanguage userLanguage]];
}

你可能感兴趣的:(iOS应用内多语言切换)