• 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
  • 相关阅读:
    基础:按值传递引用类型,按引用传递引用类新 细说并沉淀
    抽象与具体
    EXTJS 零星记录 VS2008中EXTJS智能提示插件
    javascript 与 coffescript来回转换:http://js2coffee.org/
    一个冒号引发的血案
    我基本上差不多做过了coffescript所做的事
    javascript的闭包中保存的是引用与循环中事件函数处理
    用webstorm调试coffeescript
    coffeescript 下的构造函数中如何使用return语句?
    coffeescript中的forin和forof
  • 原文地址:https://www.cnblogs.com/yuxiaoyiyou/p/11062804.html
Copyright © 2020-2023  润新知