167 lines
5.9 KiB
Objective-C
167 lines
5.9 KiB
Objective-C
//
|
||
// HBMultiLanguageManager.m
|
||
// QinJiuTV
|
||
//
|
||
// Created by HI_LOSER on 2024/5/6.
|
||
//
|
||
|
||
#import "HBMultiLanguageManager.h"
|
||
|
||
/// 多语言 userdefine key
|
||
NSString *const HBMultiLanguageKey = @"HBMultiLanguageKey";
|
||
|
||
NSString *const HBLocaleLanguageKey = @"AppleLanguages";
|
||
|
||
NSString *const HBMultiLanguageChangeNotification = @"HBMultiLanguageChangeNotification";
|
||
|
||
@implementation HBLanguageModel
|
||
+ (HBLanguageModel *) newWithTitle:(NSString *) title subTitle:(NSString *)subTitle abbr:(NSString *) abbr imageNamed:(NSString *)imageNamed{
|
||
HBLanguageModel * model = [[HBLanguageModel alloc]init];
|
||
model.title = title;
|
||
model.abbr = abbr;
|
||
model.imageNamed = imageNamed;
|
||
model.subTitle = subTitle;
|
||
return model;
|
||
}
|
||
|
||
-(BOOL)selected{
|
||
return [self.abbr isEqualToString:[NSUserDefaults stringForKey:HBMultiLanguageKey]];
|
||
}
|
||
|
||
@end
|
||
|
||
|
||
|
||
@implementation HBMultiLanguageManager
|
||
|
||
+ (NSString *)abbr{
|
||
NSString * abbr = [self currentlanguageAbbr];
|
||
if ([abbr isEqualToString:@"--"]) {
|
||
NSString * localAbbr = [NSUserDefaults arrayForKey:HBLocaleLanguageKey].firstObject;
|
||
NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
|
||
if (localAbbr) {
|
||
localAbbr = [localAbbr stringByReplacingOccurrencesOfString:countryCode withString:@""];
|
||
}
|
||
return localAbbr;
|
||
}
|
||
return abbr;
|
||
}
|
||
|
||
+ (BOOL)isSystem {
|
||
NSString * abbr = [self currentlanguageAbbr];
|
||
return [abbr isEqualToString:@"--"];
|
||
}
|
||
|
||
+ (NSString *)currentlanguageAbbr{
|
||
NSString * abbr = [NSUserDefaults stringForKey:HBMultiLanguageKey];
|
||
if (!abbr) {
|
||
abbr = @"--";
|
||
[self saveCurrentLanguageAbbr:abbr];
|
||
}
|
||
return abbr;
|
||
}
|
||
|
||
+ (void)saveCurrentLanguageAbbr:(NSString *)abbr {
|
||
NSString * oldAbbr = [NSUserDefaults stringForKey:HBMultiLanguageKey];
|
||
if ([oldAbbr isEqualToString:abbr]) return;
|
||
|
||
// [NSUserDefaults setObject:abbr forKey:HBLocaleLanguageKey];
|
||
[NSUserDefaults setObject:abbr forKey:HBMultiLanguageKey];
|
||
[NSUserDefaults synchronize];
|
||
[[NSNotificationCenter defaultCenter]postNotificationName:HBMultiLanguageChangeNotification object:nil];
|
||
}
|
||
|
||
|
||
+(NSArray<HBLanguageModel *> *)languages{
|
||
return @[
|
||
[HBLanguageModel newWithTitle:@"中文" subTitle:@"选择语言后,您的系统将会切换成中文" abbr:@"zh" imageNamed:@"language_country_zh"],
|
||
[HBLanguageModel newWithTitle:@"English" subTitle:@"After selecting the language, your system will switch to English" abbr:@"en" imageNamed:@"language_country_en"],
|
||
[HBLanguageModel newWithTitle:@"Tiếng Việt" subTitle:@"Sau khi chọn ngôn ngữ, hệ thống của bạn sẽ chuyển sang tiếng việt" abbr:@"vi" imageNamed:@"language_country_vi"],
|
||
[HBLanguageModel newWithTitle:@"한국어" subTitle:@"언어를 선택하면 시스템이 한국어로 전환됩니다" abbr:@"ko" imageNamed:@"language_country_ko"],
|
||
[HBLanguageModel newWithTitle:@"日本語" subTitle:@"言語をせんたく選択すると、システムはにっぽん日本ご語にきりかわり切り替わります" abbr:@"ja" imageNamed:@"language_country_ja"]
|
||
];
|
||
}
|
||
@end
|
||
|
||
|
||
@implementation NSString (language)
|
||
- (NSString *)language {
|
||
return NSLocalizedStringFromTable(self, @"MultiLanguage",nil);
|
||
}
|
||
|
||
//- (NSString *)language {
|
||
//
|
||
// NSString * abbr = [HBMultiLanguageManager currentlanguageAbbr];
|
||
//
|
||
// NSString * localAbbr = [NSUserDefaults arrayForKey:HBLocaleLanguageKey].firstObject;
|
||
//
|
||
// NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
|
||
// if (localAbbr) {
|
||
// localAbbr = [localAbbr stringByReplacingOccurrencesOfString:countryCode withString:@""];
|
||
// }
|
||
//
|
||
// if ([abbr isEqualToString:@"--"]) {
|
||
// return [self bundleWithAbbr:localAbbr];
|
||
// } else {
|
||
// return [self bundleWithAbbr:abbr];
|
||
// }
|
||
//}
|
||
//
|
||
//- (NSBundle *) bundleWithAbbr:(NSString *) abbr {
|
||
// return [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:abbr ofType:@"lproj"]];
|
||
//}
|
||
//
|
||
//- (NSString *)languageWithBundle:(NSBundle *) bundle {
|
||
// if (bundle) {
|
||
// return NSLocalizedStringFromTableInBundle(self, @"MultiLanguage", bundle, ni);
|
||
// } else {
|
||
// return NSLocalizedStringFromTable(self, @"MultiLanguage",nil);
|
||
// }
|
||
//}
|
||
@end
|
||
|
||
|
||
@interface HBBundle : NSBundle
|
||
|
||
@end
|
||
|
||
|
||
@implementation HBBundle
|
||
|
||
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
|
||
if ([HBBundle bundle]) {
|
||
return [[HBBundle bundle] localizedStringForKey:key value:value table:tableName];
|
||
} else {
|
||
return [super localizedStringForKey:key value:value table:tableName];
|
||
}
|
||
}
|
||
|
||
+ (NSBundle *)bundle {
|
||
NSString * abbr = [HBMultiLanguageManager currentlanguageAbbr];
|
||
NSString * localAbbr = [NSUserDefaults arrayForKey:HBLocaleLanguageKey].firstObject;
|
||
NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
|
||
if (localAbbr) {
|
||
localAbbr = [localAbbr stringByReplacingOccurrencesOfString:countryCode withString:@""];
|
||
}
|
||
if ([abbr isEqualToString:@"--"]) {
|
||
return [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:localAbbr ofType:@"lproj"]];
|
||
} else {
|
||
return [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:abbr ofType:@"lproj"]];;
|
||
}
|
||
}
|
||
@end
|
||
|
||
@implementation NSBundle (Language)
|
||
+ (void)load {
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
// 动态继承、交换,方法类似KVO,通过修改[NSBundle mainBundle]对象的isa指针,使其指向它的子类CLBundle,
|
||
// 这样便可以调用子类的方法;其实这里也可以使用method_swizzling来交换mainBundle的实现,来动态判断,可以同样实现。
|
||
object_setClass([NSBundle mainBundle], [HBBundle class]);
|
||
});
|
||
}
|
||
|
||
@end
|
||
|
||
|