https://github.com/starainDou 歡迎點(diǎn)星
NSLocalizedString
Xcode中有本地化的內(nèi)置支持
- NSLocalizedString的定義:
#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
第一個參數(shù)key是從Localizable.strings這個文件中讀取對應(yīng)的key-value值食侮。
第二個參數(shù)comment可以是nil充活,可以是一段為空的字符串,也可以是對key的注釋。
- 本地化字符串
添加語言支持
建立String File文件霉祸,命名命名為“Localizable察净,生成一個Localizable.strings文件(名字不要其他的)
選中新建的Localizable.strings文件掠手,點(diǎn)擊右邊的Localize抒巢,添加English和Chinese
在Localizable.strings(English) 文件中寫 "OK" = "OK";(不要忘記寫分號)
在Localizable.strings(Chinese) 文件中寫 "OK" = "確定";(不要忘記寫分號)
使用
define OKText NSLocalizedString(@"OK", nil)
如果你的strings文件名字不是Localizable而是自定義的話,如hel.strings介粘,那么你就得使用NSLocalizedStringFromTable()來讀取本地化字符串 NSLocalizedStringFromTable(@"hello",@"hel", nil)
如果不能添加語言支持(localizations--choose files and reference language to create...為空白)
打開工程配置殖氏,搜 /* Begin XCBuildConfiguration section */
在之前添加段落
/* Begin PBXVariantGroup section /
4C0E56D11F09E6B900583071 / Localizable.strings / = {
isa = PBXVariantGroup;
children = (
4C0E56D01F09E6B900583071 / en /,
4C0E56D21F09E6C200583071 / zh-Hans /,
);
name = Localizable.strings;
sourceTree = "<group>";
};
ABCB97AC1D71D063000DEBEF / LaunchScreen.storyboard / = {
isa = PBXVariantGroup;
children = (
ABCB97AD1D71D063000DEBEF / Base /,
4C0E56CC1F09E37B00583071 / zh-Hans /,
);
name = LaunchScreen.storyboard;
sourceTree = "<group>";
};
/ End PBXVariantGroup section */
然后添加直接添加Localizable.strings,方法見上姻采,不考慮別的問題雅采,然后回到配置文件搜索
/* Localizable.strings in Resources /
復(fù)制ID
搜/ Begin PBXVariantGroup section /,將/ Localizable.strings */ 前面的ID 替換保存即可
XCBuildConfiguration
封裝的工具,在APP內(nèi)切換語言
你可以用黑魔法為全局添加婚瓜,不過需要處理很多特殊情況宝鼓。
DDYLanguageTool.h
/**
* 語言國際化使用方式
* 1.AppDelegata中 -application:didFinishLaunchingWithOptions: 里設(shè)置默認(rèn)(初始化即可) [DDYLanguageTool sharedManager];
* 2.選擇語言后 [[DDYLanguageTool sharedManager] ddy_SetLanguage:_selectLanguage callback:nil];
*/
#import <Foundation/Foundation.h>
/** 英漢雙語 */
#if TARGET_IPHONE_SIMULATOR
#define DDYStr(Chinese,English) [DDYCurrentLanguage isEqualToString:IOS_9_LATER?@"zh-Hans-US":@"zh-Hans"] ? Chinese : English
#elif TARGET_OS_IPHONE
#define DDYStr(Chinese,English) [DDYCurrentLanguage isEqualToString:IOS_9_LATER?@"zh-Hans-CN":@"zh-Hans"] ? Chinese : English
#endif
extern NSErrorDomain DDYLanguageErrorDomain;
#define kDDYLanguageErrorSuccess 0 // 設(shè)置語言成功
#define kDDYLanguageErrorUnknown -1 // 未知錯誤
#define kDDYLanguageErrorNil -2 // 語言為空
#define kDDYLanguageErrorNotSupport -3 // 不支持的語言
static NSString *const DDY_CN = @"zh-Hans";
static NSString *const DDY_EN = @"en";
@interface DDYLanguageTool : NSObject
/** 單例對象 */
+ (instancetype)sharedManager;
/** 獲取當(dāng)前語言 */
- (NSString *)localLanguage;
/** 切換語言(漢英時) */
- (void)changeLanguage;
/** 設(shè)置語言(較多語言支持時) */
- (void)ddy_SetLanguage:(NSString *)language callback:(void (^)(NSError *error))callback;
@end
DDYLanguageTool.m
#import "DDYLanguageTool.h"
#import <objc/runtime.h>
#define DDYLanguageSet @"DDYLanguageSet"
NSErrorDomain DDYLanguageErrorDomain = @"DDYLanguageErrorDomain";
//----------------------- NSBundle子類 -----------------------//
static const char _bundle = 0;
@interface DDYBundleExt : NSBundle
@end
@implementation DDYBundleExt
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);
return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}
@end
//------------------------ 語言設(shè)置工具類 -----------------------//
@implementation DDYLanguageTool
#pragma mark - 單例對象
static DDYLanguageTool *_instance;
+ (instancetype)sharedManager {
return [[self alloc] init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
- (id)copyWithZone:(NSZone *)zone {
return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return _instance;
}
#pragma mark 初始化
- (instancetype)init {
if (self = [super init]) {
NSString *currLanguage = DDYUserDefaultsGet(DDYLanguageSet);
// 默認(rèn)語言(中英)
if (!currLanguage) {
currLanguage = DDYStr(DDY_CN, DDY_EN);
}
DDYUserDefaultsSet(currLanguage, DDYLanguageSet)
[DDYUserDefaults synchronize];
[self setLanguage:currLanguage];
}
return self;
}
#pragma mark 獲取當(dāng)前語言
- (NSString *)localLanguage {
return DDYUserDefaultsGet(DDYLanguageSet);
}
#pragma mark 切換語言(中英時)
- (void)changeLanguage {
[DDYUserDefaults setObject:[DDYUserDefaultsGet(DDYLanguageSet) isEqualToString:DDY_CN] ? DDY_EN : DDY_CN forKey:DDYLanguageSet];
[DDYUserDefaults synchronize];
}
#pragma mark 設(shè)置語言(較多語言支持時)
- (void)ddy_SetLanguage:(NSString *)language callback:(void (^)(NSError *error))callback {
if ([language ddy_blankString]) {
if (callback) callback([NSError errorWithDomain:DDYLanguageErrorDomain code:kDDYLanguageErrorNil userInfo:@{@"reason":@"設(shè)置的語言不能為空"}]);
} else {
[self setLanguage:language];
[DDYUserDefaults setObject:language forKey:DDYLanguageSet];
[DDYUserDefaults synchronize];
if (callback) callback([NSError errorWithDomain:DDYLanguageErrorDomain code:kDDYLanguageErrorSuccess userInfo:@{@"reason":@"設(shè)置成功"}]);
}
}
#pragma mark 私有方法 設(shè)置語言
- (void)setLanguage:(NSString *)language {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass([NSBundle mainBundle], [DDYBundleExt class]);
});
objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end