1
This commit is contained in:
@@ -12,7 +12,7 @@ KBLanguageCode const KBLanguageCodeEnglish = @"en";
|
||||
KBLanguageCode const KBLanguageCodeSimplifiedChinese = @"zh-Hans";
|
||||
KBLanguageCode const KBLanguageCodeTraditionalChinese = @"zh-Hant";
|
||||
KBLanguageCode const KBLanguageCodeSpanish = @"es";
|
||||
KBLanguageCode const KBLanguageCodePortuguese = @"pt";
|
||||
KBLanguageCode const KBLanguageCodePortuguese = @"pt-PT";
|
||||
KBLanguageCode const KBLanguageCodeIndonesian = @"id";
|
||||
|
||||
/// 默认支持语言列表(集中配置)
|
||||
@@ -94,7 +94,7 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
if (code.length == 0) return; // 忽略空值
|
||||
if ([code isEqualToString:self.currentLanguageCode]) return; // 无变更
|
||||
[self applyLanguage:code];
|
||||
if (persist) { [[self class] kc_write:code]; } // 需同步到 App/扩展
|
||||
if (persist) { [[self class] kc_write:self.currentLanguageCode]; } // 需同步到 App/扩展
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:KBLocalizationDidChangeNotification object:nil];
|
||||
}
|
||||
|
||||
@@ -160,12 +160,16 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
#pragma mark - 内部实现
|
||||
|
||||
- (void)applyLanguage:(NSString *)code {
|
||||
_currentLanguageCode = [code copy];
|
||||
NSString *normalizedCode = code;
|
||||
if ([normalizedCode.lowercaseString isEqualToString:@"pt"]) {
|
||||
normalizedCode = @"pt-PT";
|
||||
}
|
||||
_currentLanguageCode = [normalizedCode copy];
|
||||
// 基于当前 Target(App 或扩展)的主 bundle 加载 .lproj 资源
|
||||
NSString *path = [NSBundle.mainBundle pathForResource:code ofType:@"lproj"];
|
||||
NSString *path = [NSBundle.mainBundle pathForResource:normalizedCode ofType:@"lproj"];
|
||||
if (!path) {
|
||||
// 尝试去区域后缀:如 en-GB -> en
|
||||
NSString *shortCode = [[code componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]] firstObject];
|
||||
NSString *shortCode = [[normalizedCode componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]] firstObject];
|
||||
if (shortCode.length > 0) {
|
||||
path = [NSBundle.mainBundle pathForResource:shortCode ofType:@"lproj"];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user