iOS经典

iOS开发之SDK国际化

2018-01-15  本文已影响532人  KODIE

需求

对SDK内部text进行国际化处理

详细业务需求

在控制器上显示一段文字,底下有三个按钮,每一个按钮点击之后切换不同的语种,分英文、简体中文和繁体中文三种。

准备工作

注意:
创建静态库可以参考:iOS开发库之(二):静态库Framework的创建
创建Bundle可以参考:iOS之Bundle的创建与使用

国际化步骤

点击刚刚创建的Localizable.strings文件,点击以下红框:


Snip20180115_27.png Snip20180115_25.png

默认只支持英文的:


Snip20180115_34.png

选择需要支持的语言,比如说Chinese(Simlified)/Chinese(Traditional):


Snip20180115_35.png

本处只需要支持三种:英文、简体中文、繁体中文,那配置结果如下图:


Snip20180115_36.png Snip20180115_42.png
Snip20180115_43.png

以上属于Bundle里面的设置内容,这个国际化的内容就设置完了,接下来是如何显示不同的内容,先编译一下Bundle,另外每次改变Bundle都需要重新单独编译~

//KODCommonConst.h
#import <UIKit/UIKit.h>

UIKIT_EXTERN NSString *const KODShowText;

//KODCommonConst.m
#import "KODCommonConst.h"

NSString *const KODShowText = @"KODShowText";

//NSBundle+KODLocalizable.h
#import <UIKit/UIKit.h>

@interface NSBundle (KODLocalizable)
+ (instancetype)KOD_localizableBundleWithBundleName:(NSString *)bundleName;
+ (NSString *)KOD_localizedStringForKey:(NSString *)key value:(NSString *)value;
+ (NSString *)KOD_localizedStringForKey:(NSString *)key;
@end

//NSBundle+KODLocalizable.m
#import "NSBundle+KODLocalizable.h"
#define kKODBundleName @"FrameworkTestBundle.bundle"
#define kDSADLanguageStyle @"DSADLanguageStyle"

@implementation NSBundle (KODLocalizable)
+ (instancetype)KOD_localizableBundleWithBundleName:(NSString *)bundleName{
    static NSBundle *localizableBundle = nil;
    if (localizableBundle == nil) {
        if (!bundleName) {
            bundleName = kKODBundleName;
        }
        NSString *bundleType = nil;
        if (bundleName && ![bundleName hasSuffix:@"bundle"]) {
            bundleType = @"bundle";
        }
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:bundleType];
        localizableBundle = [NSBundle bundleWithPath:bundlePath];
    }
    return localizableBundle;
}

+ (NSString *)KOD_localizedStringForKey:(NSString *)key{
    return [self KOD_localizedStringForKey:key value:nil];
}

+ (NSString *)KOD_localizedStringForKey:(NSString *)key value:(NSString *)value{
    NSBundle *bundle = nil;
    //NSString *language = [self getLanguageFromSystem];
    //NSString *language = [self getLanguageFromPlist];
    NSString * language = [self getLanguageFromDevelopersSetup];
    if (!language) {
        language = [self getLanguageFromSystem];
    }
    //从FrameworkTestBundle.bundle中查找资源
    NSString *bundlePath = [[NSBundle KOD_localizableBundleWithBundleName:nil] pathForResource:language ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:bundlePath];
    value = [bundle localizedStringForKey:key value:value table:nil];
    return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
}

//这个设置语言是通过读取当前系统使用语言
+ (NSString *)getLanguageFromSystem{
    NSString *language = [NSLocale preferredLanguages].firstObject;
    if ([language hasPrefix:@"en"]) {
        language = @"en";
    } else if ([language hasPrefix:@"zh"]) {
        if ([language rangeOfString:@"Hans"].location != NSNotFound) {
            language = @"zh-Hans"; // 简体中文
        } else {
            language = @"zh-Hant"; // 繁體中文
        }
    } else {
        language = @"en";
    }
    return language;
}

//这个是设置语言通过Plist文件来读取
+ (NSString *)getLanguageFromPlist{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"SDKInternationalizationDemoPlist.plist" ofType:nil];
    if (!bundlePath) {
        return nil;
    }
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
    if (dict) {
        NSInteger languageNum = [[dict valueForKey:@"language"] integerValue];
        switch (languageNum) {
            case 1:
                return @"en";     //语言为英语:en
                break;
            case 2:
                return @"zh-Hans";//语言为简中:zh-Hans
                break;
            case 3:
                return @"zh-Hant";//语言为繁中:zh-Hanz
                break;
            default:
                return @"en";
                break;
        }
    }
    return @"en";
}

//这个是设置语言通过开发者手动调用,从NSUserDefaults里面去读kDSADLanguageStyle这个字段是哪一种语言
+ (NSString *)getLanguageFromDevelopersSetup{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger languageStyle = [[userDefaults valueForKey:kDSADLanguageStyle] integerValue];
    if (!languageStyle) {
        return nil;
    }
    switch (languageStyle) {
        case 1:
            return @"en";
            break;
        case 2:
            return @"zh-Hans";
            break;
        case 3:
            return @"zh-Hant";
            break;
        default:
            return @"en";
            break;
    }
}

@end
NSString *showText = [NSBundle KOD_localizedStringForKey: KODShowText];
showLabel.text = showText;

注意:其他的联调的我就不说了,另外还有Alert的那个类我也不说了,大家更详细的可以看DEMO,DEMO传送门

以下附上三张效果图:

附带其他的国际化:
3分钟实现iOS语言本地化/国际化(图文详解)

以上!!!


七禾.jpg
上一篇下一篇

猜你喜欢

热点阅读