iOS项目快速国际化

最近刚接手了一个直播的项目,但是面向的用户人群是境外华侨和各国人士,整个项目本身就很大。很无奈,但是还得弄啊。怎么办?我就用了一天将完整的整个项目实现了国际化。基础的我就不讲了,想了解的看《iOS之应用程序国际化》《iOS国际化开发》。下面开始讲讲我的方法:
工具
由于也是第一次接触到国际化,抱着向前辈学习思想(其实我就是想偷懒),找到了这个工具,这个可以支持中文和繁体文导出,用它你整个项目中的中文都可以获取到,我fork了这个项目并且在此项目的基础上修改了一下正则表达式,添加了支持xib,storyBoard,swift文件的支持,大家可以用这个国际化导出中文工具
Demo效果

录制的

代码

扩展

只需要在项目中导入Categories文件夹,就可以实现项目国际化了。
主要的方法:
扩展NSBundle

#import "NSBundle+I18N.h"
#import <objc/runtime.h>


static const NSString *bundleKey = @"bundleKey";

@interface BundleEx : NSBundle

@end

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
    NSBundle *bundle = objc_getAssociatedObject(self, &bundleKey);
    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}

@end


@implementation NSBundle (I18N)

+ (void)setMainBundelLanguage:(NSString *)language {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object_setClass([NSBundle mainBundle], [BundleEx class]);
    });
    //设置关联
    objc_setAssociatedObject([NSBundle mainBundle], &bundleKey, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (NSBundle *)getCurrentMainBundel {
    NSString * currentLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"][0];
    NSString *path = [[ NSBundle mainBundle ] pathForResource:currentLanguage ofType:@"lproj" ];
    NSBundle * current = [NSBundle bundleWithPath:path];
    return current;
}


@end

扩展UILable

#import "UILabel+I18N.h"
#import <objc/runtime.h>

@implementation UILabel (I18N)

+(void)load {
    SEL originalSelector = @selector(setText:);
    SEL swizzledSelector = @selector(swizzled_setText:);
    Method originMehtod = class_getInstanceMethod([self class], @selector(setText:));
    Method otherMehtod = class_getInstanceMethod([self class], @selector(swizzled_setText:));
    BOOL didAddMethod =
    class_addMethod(self,
                    originalSelector,
                    method_getImplementation(otherMehtod),
                    method_getTypeEncoding(otherMehtod));
    if (didAddMethod) {
        class_replaceMethod(self,
                            swizzledSelector,
                            method_getImplementation(originMehtod),
                            method_getTypeEncoding(originMehtod));
    }
    else {
        // 交换2个方法的实现
        method_exchangeImplementations(otherMehtod, originMehtod);
    }
}

- (void)swizzled_setText:(NSString *)string {
    [self swizzled_setText:NSLocalizedString(string, nil)];
}

@end

扩展UIImage

#import "UIImage+I18N.h"
#import <objc/runtime.h>

@implementation UIImage (I18N)

+(void)load {
    Method otherMehtod = class_getClassMethod(self, @selector(swizzled_imageNamed:));
    Method originMehtod = class_getClassMethod(self, @selector(imageNamed:));
    // 交换2个方法的实现
    method_exchangeImplementations(otherMehtod, originMehtod);
}

+ (UIImage *)swizzled_imageNamed:(NSString *)name {
    return [self swizzled_imageNamed:NSLocalizedString(name, nil)];
}

@end

源码
点击这里下载源代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,672评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,715评论 7 249
  • 我想做她的骑士 为她 披上沉重而坚实的盔甲 为她在每个清晨练习剑法 直到每一个关节都跃跃欲试 等待着 幻想着 为她...
    夜谷阅读 3,077评论 0 6
  • 前几天撩Boss任务挑战失败,原因在于我对沟通中的“对象”了解不够彻底。沟通中,最难获取的是“个性特征”,但是最能...
    W南茜阅读 3,712评论 0 0
  • 这个题目其实涵盖了很多的东西,具体真的太多太多。所以,就事论事,给大家讲个比较易接受的爱情故事吧! ...
    1生热爱阅读 2,490评论 1 0

友情链接更多精彩内容