3
This commit is contained in:
@@ -11,6 +11,16 @@
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// 语言码类型(集中管理,避免在各处散落写 @"en"/@"zh-Hans")
|
||||
typedef NSString *KBLanguageCode NS_EXTENSIBLE_STRING_ENUM;
|
||||
|
||||
/// 项目内统一使用的语言常量
|
||||
FOUNDATION_EXPORT KBLanguageCode const KBLanguageCodeEnglish; // @"en"
|
||||
FOUNDATION_EXPORT KBLanguageCode const KBLanguageCodeSimplifiedChinese; // @"zh-Hans"
|
||||
|
||||
/// 默认支持的语言列表(目前为 @[KBLanguageCodeEnglish, KBLanguageCodeSimplifiedChinese])
|
||||
FOUNDATION_EXPORT NSArray<KBLanguageCode> *KBDefaultSupportedLanguageCodes(void);
|
||||
|
||||
/// 当前语言变更通知(不附带 userInfo)
|
||||
extern NSString * const KBLocalizationDidChangeNotification;
|
||||
|
||||
@@ -20,18 +30,18 @@ extern NSString * const KBLocalizationDidChangeNotification;
|
||||
/// 单例
|
||||
+ (instancetype)shared;
|
||||
|
||||
/// 当前语言代码(如:"en"、"zh-Hans"、"ja")。
|
||||
/// 当前语言代码(如:KBLanguageCodeEnglish、KBLanguageCodeSimplifiedChinese)。
|
||||
/// 默认会在受支持语言中,按系统首选语言择优匹配。
|
||||
@property (nonatomic, copy, readonly) NSString *currentLanguageCode;
|
||||
@property (nonatomic, copy, readonly) KBLanguageCode currentLanguageCode;
|
||||
|
||||
/// 支持的语言代码列表。默认 @[@"en", @"zh-Hans"]。
|
||||
/// 支持的语言代码列表。默认 `KBDefaultSupportedLanguageCodes()`。
|
||||
/// 建议在启动早期设置;或设置后再调用 `-setCurrentLanguageCode:persist:` 以刷新。
|
||||
@property (nonatomic, copy) NSArray<NSString *> *supportedLanguageCodes;
|
||||
@property (nonatomic, copy) NSArray<KBLanguageCode> *supportedLanguageCodes;
|
||||
|
||||
/// 设置当前语言。
|
||||
/// @param code 语言代码
|
||||
/// @param persist 是否持久化到共享钥匙串(以便 App 与扩展共享该选择)
|
||||
- (void)setCurrentLanguageCode:(NSString *)code persist:(BOOL)persist;
|
||||
- (void)setCurrentLanguageCode:(KBLanguageCode)code persist:(BOOL)persist;
|
||||
|
||||
/// 清除用户选择,恢复为系统最佳匹配。
|
||||
- (void)resetToSystemLanguage;
|
||||
@@ -45,7 +55,7 @@ extern NSString * const KBLocalizationDidChangeNotification;
|
||||
value:(nullable NSString *)value;
|
||||
|
||||
/// 基于一组“偏好语言”计算最佳支持语言。
|
||||
- (NSString *)bestSupportedLanguageForPreferred:(NSArray<NSString *> *)preferred;
|
||||
- (KBLanguageCode)bestSupportedLanguageForPreferred:(NSArray<NSString *> *)preferred;
|
||||
|
||||
- (void)reloadFromSharedStorageIfNeeded;
|
||||
|
||||
|
||||
@@ -7,6 +7,20 @@
|
||||
#import <Security/Security.h>
|
||||
#import "KBConfig.h"
|
||||
|
||||
/// 语言常量定义
|
||||
KBLanguageCode const KBLanguageCodeEnglish = @"en";
|
||||
KBLanguageCode const KBLanguageCodeSimplifiedChinese = @"zh-Hans";
|
||||
|
||||
/// 默认支持语言列表(集中配置)
|
||||
NSArray<KBLanguageCode> *KBDefaultSupportedLanguageCodes(void) {
|
||||
static NSArray<KBLanguageCode> *codes;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
codes = @[KBLanguageCodeEnglish, KBLanguageCodeSimplifiedChinese];
|
||||
});
|
||||
return codes;
|
||||
}
|
||||
|
||||
/// 语言变更通知名称
|
||||
NSString * const KBLocalizationDidChangeNotification = @"KBLocalizationDidChangeNotification";
|
||||
|
||||
@@ -15,7 +29,7 @@ static NSString * const kKBLocService = @"com.loveKey.nyx.loc";
|
||||
static NSString * const kKBLocAccount = @"lang"; // 保存 UTF8 的语言代码
|
||||
|
||||
@interface KBLocalizationManager ()
|
||||
@property (nonatomic, copy, readwrite) NSString *currentLanguageCode; // 当前语言代码
|
||||
@property (nonatomic, copy, readwrite) KBLanguageCode currentLanguageCode; // 当前语言代码
|
||||
@property (nonatomic, strong) NSBundle *langBundle; // 对应语言的 .lproj 资源包
|
||||
@end
|
||||
|
||||
@@ -37,11 +51,11 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
static KBLocalizationManager *m; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
|
||||
m = [KBLocalizationManager new];
|
||||
// 默认支持语言;可在启动时由外部重置
|
||||
m.supportedLanguageCodes = @[ @"en", @"zh-Hans" ];
|
||||
m.supportedLanguageCodes = KBDefaultSupportedLanguageCodes();
|
||||
// 启动读取:先取共享钥匙串,再按系统偏好回退
|
||||
NSString *saved = [[self class] kc_read];
|
||||
if (saved.length == 0) {
|
||||
saved = [m bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: @"en";
|
||||
saved = [m bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: KBLanguageCodeEnglish;
|
||||
}
|
||||
[m applyLanguage:saved];
|
||||
});
|
||||
@@ -56,7 +70,7 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
for (NSString *c in supportedLanguageCodes) {
|
||||
if (c.length) { [set addObject:c]; }
|
||||
}
|
||||
_supportedLanguageCodes = set.array.count ? set.array : @[ @"en" ];
|
||||
_supportedLanguageCodes = set.array.count ? (NSArray<KBLanguageCode> *)set.array : @[ KBLanguageCodeEnglish ];
|
||||
// 若当前语言不再受支持,则按最佳匹配切回(不持久化,仅内存),并广播变更
|
||||
if (self.currentLanguageCode.length && ![set containsObject:self.currentLanguageCode]) {
|
||||
NSString *best = [self bestSupportedLanguageForPreferred:@[self.currentLanguageCode]];
|
||||
@@ -74,7 +88,7 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
}
|
||||
|
||||
- (void)resetToSystemLanguage {
|
||||
NSString *best = [self bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: @"en";
|
||||
NSString *best = [self bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: KBLanguageCodeEnglish;
|
||||
[self setCurrentLanguageCode:best persist:NO];
|
||||
}
|
||||
|
||||
@@ -92,7 +106,7 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
}
|
||||
|
||||
- (NSString *)bestSupportedLanguageForPreferred:(NSArray<NSString *> *)preferred {
|
||||
if (self.supportedLanguageCodes.count == 0) return @"en";
|
||||
if (self.supportedLanguageCodes.count == 0) return KBLanguageCodeEnglish;
|
||||
// 1) 完全匹配
|
||||
for (NSString *p in preferred) {
|
||||
NSString *pLC = p.lowercaseString;
|
||||
@@ -129,7 +143,7 @@ static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
}
|
||||
}
|
||||
// 4) 兜底:取第一个受支持语言
|
||||
return self.supportedLanguageCodes.firstObject ?: @"en";
|
||||
return self.supportedLanguageCodes.firstObject ?: KBLanguageCodeEnglish;
|
||||
}
|
||||
|
||||
#pragma mark - 内部实现
|
||||
|
||||
Reference in New Issue
Block a user