iOS快速国际化(适合项目后期)

2018-09-19  本文已影响0人  火山脚下

iOS国际化的时候,怎样快速找到需要格式化的中文内容
1,先把项目中的class文件拷贝到iOS模拟器的沙盒中的document目录里面。
2,调用函数dosomething(self.mutableDic里面存的就是我们想要的数据)。
3,将self.mutableDic.allKeys取出来就是你想要的内容。

@interface TwoViewController ()
@property(nonatomic,strong) NSMutableDictionary *mutableDic;
@end

- (void)dosomething{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSLog(@"%@", documentDir);
[self searchFileWithFilePath:documentDir];
for (NSString *str in self.mutableDic.allKeys) {
    NSSLog(@"%@", str);
 }
}
- (void)searchFileWithFilePath:(NSString*)filePath{
   static NSInteger countss = 0;
   static NSInteger countwenjian = 0;
   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error = nil;
   NSArray *fileList = [[NSArray alloc] init];
   //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
   fileList = [fileManager contentsOfDirectoryAtPath:filePath error:&error];
   //以下这段代码则可以列出给定一个文件夹里的所有子文件夹名
  BOOL isDir = NO;
 //在上面那段程序中获得的fileList中列出文件夹名
  for (NSString *file in fileList) {
      NSString *path = [filePath stringByAppendingPathComponent:file];
      [fileManager fileExistsAtPath:path isDirectory:(&isDir)];
      if (isDir) {
  //            NSLog(@"文件夹:%@ %ld", file, countss++);
  //            if ([file rangeOfString:@"youqslog"].location != NSNotFound) {
  //                continue;
  //            }
  //            if ([file rangeOfString:@"."].location != NSNotFound) {
  //                continue;
  //            }
           [self searchFileWithFilePath:path];
      }else{
           BOOL isPass = YES;
          if ([file length] > 1) {
            NSString *rearString = [file substringFromIndex:[file length]-2];
            if ( [rearString isEqualToString:@".h"] || [rearString isEqualToString:@".m"] || ([file rangeOfString:@".xib"].location!=NSNotFound)) {
                isPass = NO;
            }
        }
        if (isPass) {
            continue;
         }
        NSLog(@"文件:%@  %ld", file, countwenjian++);
        [self findChinese:path];
      }
     isDir = NO;
   }
}


- (void)findChinese:(NSString*)path{
FILE *fp;
if((fp = fopen(path.UTF8String,"r")) == NULL) //判断文件是否存在及可读
{
    printf("error!");
}
while (!feof(fp)) {
    char StrLine[2048];             //每行最大读取的字符数
    fgets(StrLine, 1024, fp);  //读取一行
    //                printf("%s\n", StrLine);   //输出
    NSString *searchText = [NSString stringWithUTF8String:StrLine];
    NSError *error = NULL;
    NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:@"@\"[^\"]*[\u4E00-\u9FA5]+[^\"\n]*?\"" options:NSRegularExpressionCaseInsensitive error:&error];
    NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"NSLog.@\"[^\"]*[\u4E00-\u9FA5]+[^\"\n]*?\"" options:NSRegularExpressionCaseInsensitive error:&error];
    NSTextCheckingResult *result1 = [regex1 firstMatchInString:searchText options:0 range:NSMakeRange(0, [searchText length])];
    NSTextCheckingResult *result2 = [regex2 firstMatchInString:searchText options:0 range:NSMakeRange(0, [searchText length])];
    if (result1) {
        NSString *zw1 = [searchText substringWithRange:result1.range];
        NSString *zw2 = [searchText substringWithRange:result2.range];
        if ([zw2 hasPrefix:@"NSLog(@"]) {
            NSLog(@"");
        }else{
            if ([zw1 hasPrefix:@"@"]) {
                zw1 = [zw1 substringFromIndex:1];
            }
            if (zw1) {
                [self.mutableDic setObject:zw1 forKey:zw1];
            }
        }
        // NSSLog(@"%@", zw);
       }
    }
  fclose(fp);                     //关闭文件
}

最后我们再写一个UILabel的Category就可以一键实现国际化了

@implementation UILabel (Swizzle)
+ (void)initialize {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
      [UILabel swizzleSEL:@selector(setText:) withSEL:@selector(setTextHooked:)];
  });
}
- (void)setTextHooked:(NSString *)astring
{
  NSLog(@"xxx %@ %@",[self class], astring);
  NSString *string = NSLocalizedString(astring, nil);
  [self setTextHooked:string];
}
上一篇下一篇

猜你喜欢

热点阅读