ios国家化的一些点
将时间NSDate本地化format显示时,泰国有佛历年,比公历年领先了一千多数值,也就是2019=2562(2563?)
本地化文件除了可以内嵌还可以strings文件放在网络上下载,再通过本地封装成本地的bundle读取里面的内容
static NSMutableDictionary *wjy_lanBundleDict = nil; #define WJYCONVERLAN(lan) [WjyLanguage languageCodeForLanguage:lan] @implementation WjyLanguage +(void)load { [self loadSystemLanguage]; } +(void)loadSystemLanguage { NSString *dirPath = [[NSBundle mainBundle] bundlePath]; NSFileManager *man = [NSFileManager defaultManager]; for (NSString *subpath in [man subpathsOfDirectoryAtPath:dirPath error:nil]) { NSArray *arr = [subpath componentsSeparatedByString:@"."]; if (arr.count == 2) { if ([arr[1] isEqualToString:@"lproj"]) { [self copySystemLocalizableForLanguage:arr[0] then:^(BOOL success) { }]; } } } } +(void)copySystemLocalizableForLanguage:(NSString *)language then:(void (^)(BOOL))then { NSBundle *localBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]]; if (localBundle) { [[self bundleDict] setObject:localBundle forKey:WJYCONVERLAN(language)]; !then?:then(YES); } else { [[self bundleDict] setObject:@(NO) forKey:WJYCONVERLAN(language)]; !then?:then(NO); } } +(void)downloadLocalizableFile:(NSURL*)url forLanguage:(NSString*)language md5:(NSString*)md5 then:(void(^)(BOOL success))then { NSFileManager *man = [NSFileManager defaultManager]; NSString *path = [self pathForLanguage:WJYCONVERLAN(language)]; NSString *file = [path stringByAppendingPathComponent:@"Localizable.strings"]; if (![man fileExistsAtPath:file]) { [self downloadLocalizableFile:url forLanguage:language then:^(BOOL success) { NSString *fmd5 = [MSFileKit md5HashOfFileAtPath:file]; NSLog(@"file md5:%@",fmd5); BOOL result = (success && [fmd5 isEqualToString:md5]); if (result) { [TextKeyData saveTextIgnoreUserVersion:md5 forKey:file]; } else { [man removeItemAtPath:file error:nil]; } !then?:then(result); }]; } else { typeof(self) __weak weakself = self; [TextKeyData textForKey:file duringVersion:nil textHandle:^(NSString *text) { if (text && [text isEqualToString:md5]) { !then?:then(YES); } else { typeof(weakself) __strong strongself = weakself; [strongself downloadLocalizableFile:url forLanguage:language then:^(BOOL success) { if (success) { [TextKeyData saveTextIgnoreUserVersion:md5 forKey:file]; } !then?:then(success); }]; } }]; } } +(void)downloadLocalizableFile:(NSURL*)url forLanguage:(NSString *)language then:(void (^)(BOOL))then { if (!url) { !then?:then(NO); return; } YDOutHttpKit *http = [[YDOutHttpKit alloc] init]; [http setShouldNotify:NO]; NSString *savedDir = [self languageCacheDirPath]; [http getFile:url.absoluteString param:@{} distinationURL:savedDir downloadProgress:^(NSProgress *aProgress) { } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { if (!error) { NSData *data = [NSData dataWithContentsOfURL:filePath]; if (data.length > 0) { BOOL result = [self saveLanguageData:data forLanguage:language]; dispatch_async(dispatch_get_main_queue(), ^{ !then?:then(result); }); } else { dispatch_async(dispatch_get_main_queue(), ^{ !then?:then(NO); }); } } else { dispatch_async(dispatch_get_main_queue(), ^{ !then?:then(NO); }); } }]; /* dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [NSData dataWithContentsOfURL:url]; dispatch_async(dispatch_get_main_queue(), ^{ if (data.length <= 0) { !then?:then(NO); return; } BOOL result = [self saveLanguageData:data forLanguage:language]; !then?:then(result); }); }); */ } +(BOOL)saveLanguageData:(NSData*)data forLanguage:(NSString *)language { NSFileManager *man = [NSFileManager defaultManager]; NSString *path = [self pathForLanguage:WJYCONVERLAN(language)]; if (![man fileExistsAtPath:path]) { [man createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *file = [path stringByAppendingPathComponent:@"Localizable.strings"]; if ([man fileExistsAtPath:file]) { [man removeItemAtPath:file error:nil]; } return [man createFileAtPath:file contents:data attributes:nil]; } +(NSString*)languageDirPath { NSArray * arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * path = [arr objectAtIndex:0]; path = [path stringByAppendingPathComponent:@"wjy_lans"]; NSFileManager *man = [NSFileManager defaultManager]; if (![man fileExistsAtPath:path]) { [man createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } return path; } +(NSString*)pathForLanguage:(NSString*)language { return [[self languageDirPath] stringByAppendingFormat:@"/%@.lproj",language]; } +(NSString*)languageCacheDirPath { NSArray * arr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString * path = [arr objectAtIndex:0]; path = [path stringByAppendingPathComponent:@"wjy_lans_cache"]; NSFileManager *man = [NSFileManager defaultManager]; if (![man fileExistsAtPath:path]) { [man createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } return path; } +(NSString*)cachePathForLanguage:(NSString*)language { return [[self languageCacheDirPath] stringByAppendingFormat:@"/%@.strings",language]; } +(void)loadLanguage:(NSString*)language then:(void(^)(BOOL success))then { NSString *path = [self pathForLanguage:WJYCONVERLAN(language)]; NSString *file = [path stringByAppendingPathComponent:@"Localizable.strings"]; NSFileManager *man = [NSFileManager defaultManager]; if (![man fileExistsAtPath:file]) { [[self bundleDict] setObject:@(NO) forKey:WJYCONVERLAN(language)]; !then?:then(NO); return; } else { /* NSString *bundlePath = [[[NSBundle mainBundle] pathForResource:@"languages" ofType:@"bundle"] stringByAppendingPathComponent:path.lastPathComponent]; if (![man fileExistsAtPath:bundlePath]) { if (![man createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:nil]) { [[self bundleDict] setObject:@(NO) forKey:WJYCONVERLAN(language)]; !then?:then(NO); return; } } BOOL result = [man copyItemAtPath:file toPath:[bundlePath stringByAppendingPathComponent:file.lastPathComponent] error:nil]; NSBundle *localBundle = [NSBundle bundleWithPath:bundlePath]; */ NSBundle *localBundle = [NSBundle bundleWithPath:[self pathForLanguage:WJYCONVERLAN(language)]]; if (!localBundle) { [[self bundleDict] setObject:@(NO) forKey:WJYCONVERLAN(language)]; !then?:then(NO); return; } /* if (result) { [[self bundleDict] setObject:localBundle forKey:WJYCONVERLAN(language)]; } else { [[self bundleDict] setObject:@(NO) forKey:WJYCONVERLAN(language)]; } !then?:then(result); */ [[self bundleDict] setObject:localBundle forKey:WJYCONVERLAN(language)]; !then?:then(YES); } } +(NSMutableDictionary*)bundleDict { if (!wjy_lanBundleDict) { wjy_lanBundleDict = [NSMutableDictionary dictionary]; } return wjy_lanBundleDict; } +(NSString*)localizedStringForLanguage:(NSString*)language forKey:(NSString *)key value:(NSString *)value { id localBundle = [[self bundleDict] objectForKey:WJYCONVERLAN(language)]; if (localBundle && [localBundle class] == [NSBundle class]) { NSString *text = [localBundle localizedStringForKey:key value:value table:nil]; if (text) { return text; } return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil]; } else if ([localBundle isEqual:@(NO)]) { return [self recommendStringForLanguage:language forKey:key value:value]; } else { [[self bundleDict] setObject:@(NO) forKey:WJYCONVERLAN(language)]; [self loadLanguage:language then:nil]; return [self localizedStringForLanguage:language forKey:key value:nil]; } } +(NSString*)appDefaultLanguage { return @"en"; } +(NSString*)appReplaceLanguageForLanguage:(NSString*)language { return @"en"; } +(NSString*)recommendStringForLanguage:(NSString*)language forKey:(NSString *)key value:(NSString *)value { if ([WJYCONVERLAN(language) isEqualToString:[self appDefaultLanguage]]) { return key; } else { return [self localizedStringForLanguage:[self appReplaceLanguageForLanguage:language] forKey:key value:nil]; } } +(NSString*)languageCodeForLanguage:(NSString*)language { NSArray *arr = [language componentsSeparatedByString:@"-"]; if (arr.count > 1) { return [arr[0] lowercaseString]; } else { return [language lowercaseString]; } } @end