chore: Initial clean commit
- Removed YuMi/Library/ (138 MB, not tracked) - Removed YuMi/Resources/ (23 MB, not tracked) - Removed old version assets (566 files, not tracked) - Excluded Pods/, xcuserdata/ and other build artifacts - Clean repository optimized for company server deployment
This commit is contained in:
20
YuMi/Tools/Bundle/NSBundle+Localizable.h
Normal file
20
YuMi/Tools/Bundle/NSBundle+Localizable.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// NSBundle+Localizable.h
|
||||
// YuMi
|
||||
//
|
||||
// Created by YuMi on 2023/6/19.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSBundle (Localizable)
|
||||
+(NSString *)ymLocalizedStringForKey:(NSString *)key;
|
||||
+(NSString *)ymLocalizedStringForKey:(NSString *)key value:(NSString *)value;
|
||||
+(void)setLanguageText:(NSString *)language;
|
||||
+(NSString *)uploadLanguageText;
|
||||
+(NSString *)getLanguageText;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
96
YuMi/Tools/Bundle/NSBundle+Localizable.m
Normal file
96
YuMi/Tools/Bundle/NSBundle+Localizable.m
Normal file
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// NSBundle+Localizable.m
|
||||
// YuMi
|
||||
//
|
||||
// Created by YuMi on 2023/6/19.
|
||||
//
|
||||
|
||||
#import "NSBundle+Localizable.h"
|
||||
#import "YMLanguageConfig.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
// 定义常量用于存储当前语言的 key
|
||||
static NSString * const kSaveCurLanguageKey = @"kSaveCurLanguage";
|
||||
static NSString * const kResourceType = @"lproj";
|
||||
|
||||
@implementation NSBundle (Localizable)
|
||||
|
||||
+(void)load{
|
||||
Method oldMethod = class_getClassMethod(self, @selector(mj_localizedStringForKey:));
|
||||
Method newMethod = class_getClassMethod(self, @selector(ms_localizedStringForKey:));
|
||||
method_exchangeImplementations(oldMethod, newMethod);
|
||||
}
|
||||
+ (NSString *)ms_localizedStringForKey:(NSString *)key{
|
||||
return [self ymLocalizedStringForKey:key];
|
||||
}
|
||||
+(NSString *)ymLocalizedStringForKey:(NSString *)key
|
||||
{
|
||||
return [self ymLocalizedStringForKey:key value:@""];
|
||||
}
|
||||
|
||||
+(NSString *)ymLocalizedStringForKey:(NSString *)key value:(NSString *)value
|
||||
{
|
||||
NSString *language = [NSBundle getLanguageText];
|
||||
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language
|
||||
ofType:kResourceType]];
|
||||
value = [bundle localizedStringForKey:key
|
||||
value:value
|
||||
table:nil];
|
||||
return value;
|
||||
}
|
||||
|
||||
+(NSString *)getLanguageText{
|
||||
NSString *saveLanguage = [[NSUserDefaults standardUserDefaults] valueForKey:kSaveCurLanguageKey];
|
||||
if (saveLanguage){
|
||||
return saveLanguage;
|
||||
}
|
||||
|
||||
NSString *preferredLanguage = [NSLocale preferredLanguages].firstObject;
|
||||
return [self mapLanguageForLocalization:preferredLanguage];
|
||||
}
|
||||
|
||||
+(NSString *)uploadLanguageText{
|
||||
NSString *saveLanguage = [[NSUserDefaults standardUserDefaults]valueForKey:kSaveCurLanguageKey];
|
||||
if (saveLanguage){
|
||||
return [self mapLanguageForLocalization:saveLanguage];
|
||||
}
|
||||
|
||||
NSString *preferredLanguage = [NSLocale preferredLanguages].firstObject;
|
||||
return [self mapLanguageForLocalization:preferredLanguage];
|
||||
}
|
||||
|
||||
+(void)setLanguageText:(NSString *)language{
|
||||
[[NSUserDefaults standardUserDefaults]setValue:language forKey:kSaveCurLanguageKey];
|
||||
[[NSUserDefaults standardUserDefaults]synchronize];
|
||||
}
|
||||
|
||||
// 辅助方法,用于映射本地化语言 - 重构为使用统一配置
|
||||
+ (NSString *)mapLanguageForLocalization:(NSString *)language {
|
||||
// 首先尝试精确匹配
|
||||
for (NSDictionary *langConfig in [YMLanguageConfig supportedLanguages]) {
|
||||
NSString *code = langConfig[@"code"];
|
||||
NSString *localizationCode = langConfig[@"localizationCode"];
|
||||
|
||||
if ([language isEqualToString:code] || [language isEqualToString:localizationCode]) {
|
||||
return localizationCode;
|
||||
}
|
||||
}
|
||||
|
||||
// 然后尝试前缀匹配
|
||||
for (NSDictionary *langConfig in [YMLanguageConfig supportedLanguages]) {
|
||||
NSString *code = langConfig[@"code"];
|
||||
if ([language hasPrefix:code]) {
|
||||
return langConfig[@"localizationCode"];
|
||||
}
|
||||
}
|
||||
|
||||
// 特殊处理:pt-BR 和 pt 的映射
|
||||
if ([language hasPrefix:@"pt"]) {
|
||||
return @"pt-BR";
|
||||
}
|
||||
|
||||
// 默认返回英文
|
||||
return [YMLanguageConfig defaultLanguage];
|
||||
}
|
||||
|
||||
@end
|
||||
31
YuMi/Tools/Bundle/YMLanguageConfig.h
Normal file
31
YuMi/Tools/Bundle/YMLanguageConfig.h
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// YMLanguageConfig.h
|
||||
// YuMi
|
||||
//
|
||||
// Created by Linus on 2024/12/19.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface YMLanguageConfig : NSObject
|
||||
|
||||
/// 支持的语言列表
|
||||
+ (NSArray<NSDictionary *> *)supportedLanguages;
|
||||
|
||||
/// 根据语言代码获取显示名称
|
||||
+ (NSString *)displayNameForLanguageCode:(NSString *)languageCode;
|
||||
|
||||
/// 根据语言代码获取本地化代码
|
||||
+ (NSString *)localizationCodeForLanguageCode:(NSString *)languageCode;
|
||||
|
||||
/// 检查是否为RTL语言
|
||||
+ (BOOL)isRTLanguage:(NSString *)languageCode;
|
||||
|
||||
/// 获取默认语言
|
||||
+ (NSString *)defaultLanguage;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
61
YuMi/Tools/Bundle/YMLanguageConfig.m
Normal file
61
YuMi/Tools/Bundle/YMLanguageConfig.m
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// YMLanguageConfig.m
|
||||
// YuMi
|
||||
//
|
||||
// Created by Linus on 2024/12/19.
|
||||
//
|
||||
|
||||
#import "YMLanguageConfig.h"
|
||||
|
||||
@implementation YMLanguageConfig
|
||||
|
||||
+ (NSArray<NSDictionary *> *)supportedLanguages {
|
||||
static NSArray<NSDictionary *> *languages = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
languages = @[
|
||||
@{@"code": @"en", @"localizationCode": @"en", @"displayName": @"English", @"isRTL": @NO},
|
||||
@{@"code": @"zh-Hant", @"localizationCode": @"zh-Hant", @"displayName": @"繁體中文", @"isRTL": @NO},
|
||||
@{@"code": @"ar", @"localizationCode": @"ar", @"displayName": @"العربية", @"isRTL": @YES},
|
||||
@{@"code": @"tr", @"localizationCode": @"tr", @"displayName": @"Türkçe", @"isRTL": @NO},
|
||||
@{@"code": @"pt-BR", @"localizationCode": @"pt-BR", @"displayName": @"Português (Brasil)", @"isRTL": @NO},
|
||||
@{@"code": @"es", @"localizationCode": @"es", @"displayName": @"Español", @"isRTL": @NO},
|
||||
@{@"code": @"ru", @"localizationCode": @"ru", @"displayName": @"Русский", @"isRTL": @NO},
|
||||
@{@"code": @"uz-UZ", @"localizationCode": @"uz-UZ", @"displayName": @"O'zbek", @"isRTL": @NO}
|
||||
];
|
||||
});
|
||||
return languages;
|
||||
}
|
||||
|
||||
+ (NSString *)displayNameForLanguageCode:(NSString *)languageCode {
|
||||
for (NSDictionary *language in [self supportedLanguages]) {
|
||||
if ([language[@"code"] isEqualToString:languageCode]) {
|
||||
return language[@"displayName"];
|
||||
}
|
||||
}
|
||||
return @"English"; // 默认返回英文
|
||||
}
|
||||
|
||||
+ (NSString *)localizationCodeForLanguageCode:(NSString *)languageCode {
|
||||
for (NSDictionary *language in [self supportedLanguages]) {
|
||||
if ([language[@"code"] isEqualToString:languageCode]) {
|
||||
return language[@"localizationCode"];
|
||||
}
|
||||
}
|
||||
return @"en"; // 默认返回英文
|
||||
}
|
||||
|
||||
+ (BOOL)isRTLanguage:(NSString *)languageCode {
|
||||
for (NSDictionary *language in [self supportedLanguages]) {
|
||||
if ([language[@"code"] isEqualToString:languageCode]) {
|
||||
return [language[@"isRTL"] boolValue];
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
+ (NSString *)defaultLanguage {
|
||||
return @"en";
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user