This commit is contained in:
2026-03-04 19:49:07 +08:00
parent c42ccfbcdf
commit 7029209a4d
6 changed files with 293 additions and 25 deletions

View File

@@ -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];
// TargetApp 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"];
}