修改目录结构
This commit is contained in:
37
Class/NSBundle+AppLanguageSwitch.h
Normal file
37
Class/NSBundle+AppLanguageSwitch.h
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// NSBundle+AppLanguageSwitch.h
|
||||
// https://github.com/zengqingf/iOSAppLanguageSwitch
|
||||
//
|
||||
// Created by zengqingfu on 2017/6/13.
|
||||
// Copyright © 2017年 zengqingfu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
//语言改变通知
|
||||
FOUNDATION_EXPORT NSString * const ZZAppLanguageDidChangeNotification;
|
||||
|
||||
|
||||
@interface NSBundle (AppLanguageSwitch)
|
||||
|
||||
/**
|
||||
设置语言
|
||||
|
||||
@param language 参数为语言包的前缀,比如Base.lproj 传入Base、
|
||||
中文语言包zh-Hans.lproj传入zh-Hans,前提是工程中已经提前加入了语言包
|
||||
*/
|
||||
+ (void)setCusLanguage:(NSString *)language;
|
||||
|
||||
|
||||
/**
|
||||
获取当前的自定义语言,如果使用的是跟随系统语言出参为nil
|
||||
|
||||
@return 语言类型
|
||||
*/
|
||||
+ (NSString *)getCusLanguage;
|
||||
|
||||
|
||||
/**
|
||||
恢复成跟随系统语言
|
||||
*/
|
||||
+ (void)restoreSysLanguage;
|
||||
@end
|
70
Class/NSBundle+AppLanguageSwitch.m
Normal file
70
Class/NSBundle+AppLanguageSwitch.m
Normal file
@ -0,0 +1,70 @@
|
||||
//
|
||||
// NSBundle+AppLanguageSwitch.m
|
||||
// https://github.com/zengqingf/iOSAppLanguageSwitch
|
||||
//
|
||||
// Created by zengqingfu on 2017/6/13.
|
||||
// Copyright © 2017年 zengqingfu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSBundle+AppLanguageSwitch.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
|
||||
NSString * const ZZAppLanguageDidChangeNotification = @"cc.devfu.languagedidchange";
|
||||
|
||||
static const char kBundleKey = 0;
|
||||
@interface ZZBundleEx : NSBundle
|
||||
@end
|
||||
|
||||
@implementation ZZBundleEx
|
||||
|
||||
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
|
||||
NSBundle *bundle = objc_getAssociatedObject(self, &kBundleKey);
|
||||
if (bundle) {
|
||||
return [bundle localizedStringForKey:key value:value table:tableName];
|
||||
} else {
|
||||
return [super localizedStringForKey:key value:value table:tableName];
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
static NSString *AppLanguageSwitchKey = @"App_Language_Switch_Key";
|
||||
@implementation NSBundle (AppLanguageSwitch)
|
||||
+ (void)setCusLanguage:(NSString *)language {
|
||||
id value = nil;
|
||||
NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
|
||||
if (language) {
|
||||
value = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]];
|
||||
NSAssert(value != nil, @"value不能为空,请检查参数是否正确");
|
||||
[df setObject:language forKey:AppLanguageSwitchKey];
|
||||
[df synchronize];
|
||||
} else {
|
||||
[df removeObjectForKey:AppLanguageSwitchKey];
|
||||
[df synchronize];
|
||||
}
|
||||
objc_setAssociatedObject([NSBundle mainBundle], &kBundleKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:ZZAppLanguageDidChangeNotification object:nil];
|
||||
}
|
||||
|
||||
+ (NSString *)getCusLanguage {
|
||||
NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
|
||||
NSString *language = [df objectForKey:AppLanguageSwitchKey];
|
||||
return language;
|
||||
}
|
||||
|
||||
+ (void)restoreSysLanguage {
|
||||
[self setCusLanguage:nil];
|
||||
}
|
||||
|
||||
+ (void)load {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
object_setClass([NSBundle mainBundle],[ZZBundleEx class]);
|
||||
NSString *language = [self getCusLanguage];
|
||||
if (language) {
|
||||
[self setCusLanguage:language];
|
||||
}
|
||||
});
|
||||
}
|
||||
@end
|
Reference in New Issue
Block a user