美文网首页
iOS 国际化

iOS 国际化

作者: 时光浅影 | 来源:发表于2018-11-08 14:48 被阅读8次

    第一步进入项目 设置

    project - info - localizations
    

    创建strings文件

    1.新建文件 (快捷键 command + N)

     ![图一.png](https://img.haomeiwen.com/i6693936/d148bf9b0fbb8ab8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    命名为Localizable 注意此命名的名字在宏定义里用到

    1. 点击文件,到右边侧栏点击localize...


      图二.png

    相关方法

    ChangeLanguage.h

    #import <Foundation/Foundation.h>
    
    #define LocalLanguageKey    @"LocalLanguageKey" //当前语言
    #define LanguageChange      @"LanguageChange"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ChangeLanguage : NSObject
    + (NSBundle *)bundle;//获取当前资源文件
    + (void)initUserLanguage;//初始化语言文件
    + (NSString *)userLanguage;//获取应用当前语言
    + (void)setUserlanguage:(NSString *)language;//设置当前语言
    @end
    
    NS_ASSUME_NONNULL_END
    

    ChangeLanguage.m

    
    #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:@"zh"]) {
                [self putDefaults:LanguageChange Value:@"简体中文"];
                currLanguage = @"zh-Hans";
            }else{
                [self putDefaults:LanguageChange Value:@"English"];
                currLanguage = @"en";
            }
            //存储初始化语言
            [def setValue:currLanguage forKey:LocalLanguageKey];
            [def synchronize];
            
        }else{
            if ([currLanguage hasPrefix:@"en"]) {
                [self putDefaults:LanguageChange Value:@"English"];
                currLanguage = @"en";
            }else if ([currLanguage hasPrefix:@"zh"]) {
                [self putDefaults:LanguageChange Value:@"简体中文"];
                currLanguage = @"zh-Hans";
            }
            
        }
        
        //获取文件路径
        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];
        
    }
    
    + (void)putDefaults:(NSString *)key Value:(id)value{
        if (key!=NULL&&value!=NULL) {
            NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:value forKey:key];
            [userDefaults synchronize];//同步存储数据
            
        }
    }
    + (id)getDefaults:(NSString *)key{
        NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
        
        id obj;
        if (key!=NULL) {
            obj=[userDefaults objectForKey:key];
        }
        return obj;
    }
    @end
    

    使用

    宏定义

     //MARK:-- 国际化
    #define LocalizationKey(key) [[ChangeLanguage bundle] localizedStringForKey:key value:nil table:@"Localizable"]
    

    牛逼替换方法

    //正则匹配中文
    (@"[^"]*[\u4E00-\u9FA5]+[^"\n]*?")
    
    
    image.png

    参考资料

    iOS 国际化

    相关文章

      网友评论

          本文标题:iOS 国际化

          本文链接:https://www.haomeiwen.com/subject/dokwxqtx.html