// // TranslateUtils.m // UUTUtils // // Created by zhuge on 2017/12/25. // Copyright © 2017年 zhuge. All rights reserved. // #import "TranslateUtils.h" #import "DataUtils.h" #import "ProjectInfo.h" #import "CountryInfoManager.h" #import "ZGFileUtils.h" #import "ZGImacroUtils.h" #import "WordUtils.h" #define TRANSLATE_SPLIT_STRING @"\n010101010101001\n" @interface TranslateUtils() { ProjectInfo_Local_Translate *translateInfo; NSString *input; CountryInfoManager *cim; NSString *tempOutputDir; } @end @implementation TranslateUtils - (void)translate:(ProjectInfo_Local_Translate *)theTranslateInfo { translateInfo = theTranslateInfo; input = [NSString stringWithFormat:@"%@%@%@%@%@" , translateInfo.appName, TRANSLATE_SPLIT_STRING , translateInfo.shortDesc, TRANSLATE_SPLIT_STRING , translateInfo.desc]; input = [input stringByReplacingOccurrencesOfString:@"%" withString:@" percent"]; input = [input stringByReplacingOccurrencesOfString:@"&" withString:@" and "]; input = [input stringByReplacingOccurrencesOfString:@" " withString:@" "]; // 开始翻译 cim = [[CountryInfoManager alloc] init]; tempOutputDir = translateInfo.path.cache; // [self doTheTranslate]; [self checkTranslateResult]; } - (void) doTheTranslate { [ZGFileUtils deleteFileOrDirectoryAtPath:tempOutputDir]; [ZGFileUtils createDirectoryIfNotExist:tempOutputDir]; NSString *jsOutTranslateConfig = @""; // googleplay country code is different with translate country code NSString *jsOutPlayConfig = @""; // 组合所有的国家的代码 for (CountryInfo *ci in cim.countryInfos) { if (![cim.ignoreLanguages containsObject:ci.shortName]) { NSString *shortName = ci.shortName; NSString *translateName = ci.translateName; jsOutTranslateConfig = [NSString stringWithFormat:@"\"%@\", %@", translateName, jsOutTranslateConfig]; jsOutPlayConfig = [NSString stringWithFormat:@"\"%@\", %@", shortName, jsOutPlayConfig]; } } jsOutTranslateConfig = [jsOutTranslateConfig substringToIndex:[jsOutTranslateConfig length] - 2]; jsOutPlayConfig = [jsOutPlayConfig substringToIndex:[jsOutPlayConfig length] - 2]; // 忽略的国家,直接把默认的语言写过去 for (NSString *shortName in cim.ignoreLanguages) { NSString *outputFilePath = [NSString stringWithFormat:@"%@/%@.txt", tempOutputDir, shortName]; [input writeToFile:outputFilePath atomically:true encoding:NSUTF8StringEncoding error:nil]; } // do translate NSString *translateIIMPath = [NSString stringWithFormat:@"%@/Translate/", [ZGImacroUtils iimPathOfMacOS]]; [ZGFileUtils createDirectoryIfNotExist:translateIIMPath]; NSString *dotPath = [[NSBundle mainBundle] pathForResource:@"doOneTranslate.iim" ofType:nil]; [ZGFileUtils copyFileFromPath:dotPath toPath:translateIIMPath]; // do translate.js input = [WordUtils urlEncode:input]; NSString *iimFilePath = [NSString stringWithFormat:@"%@/robot.js", translateIIMPath]; NSString *jsTemplatePath = [[NSBundle mainBundle] pathForResource:@"TranslateTemplate.js" ofType:nil]; NSString *jsTemplate = [NSString stringWithContentsOfFile:jsTemplatePath encoding:NSUTF8StringEncoding error:nil]; jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_outputDir_ZG" withString:tempOutputDir]; jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_input_ZG" withString:input]; jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_outTranslateConfig_ZG" withString:jsOutTranslateConfig]; jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_outPlayConfig_ZG" withString:jsOutPlayConfig]; [jsTemplate writeToFile:iimFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; [ZGImacroUtils makeIIMReadable:iimFilePath]; [ZGImacroUtils runIIMFile:@"Translate/robot.js"]; } - (void) checkTranslateResult { while (true) { NSArray *files = [ZGFileUtils arrayOfFileAtDir:tempOutputDir extensionFilterArray:@[@"txt"]]; if ([files count] == [cim.countryInfos count]) { for (NSString *file in files) { NSString *fileName = [[file lastPathComponent] stringByDeletingPathExtension]; NSString *s = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil]; if (![cim.ignoreLanguages containsObject:fileName]) { s = [s substringFromIndex:1]; s = [s substringToIndex:[s length] - 3]; s = [s stringByReplacingOccurrencesOfString:@"\"\"" withString:@"\""]; s = [s stringByReplacingOccurrencesOfString:@"<" withString:@"<"]; s = [s stringByReplacingOccurrencesOfString:@">" withString:@">"]; s = [WordUtils stringByTrimHTML:s]; while ([s rangeOfString:@" 010101010101001001"].location != NSNotFound) { s = [s stringByReplacingOccurrencesOfString:@" 010101010101001001" withString:@"010101010101001001"]; } while ([s rangeOfString:@"010101010101001001 "].location != NSNotFound) { s = [s stringByReplacingOccurrencesOfString:@"010101010101001001 " withString:@"010101010101001001"]; } while ([s rangeOfString:@" 010101010101001"].location != NSNotFound) { s = [s stringByReplacingOccurrencesOfString:@" 010101010101001" withString:@"010101010101001"]; } while ([s rangeOfString:@"010101010101001 "].location != NSNotFound) { s = [s stringByReplacingOccurrencesOfString:@"010101010101001 " withString:@"010101010101001"]; } } NSString *gotString = s; // check length NSArray *inputArray = [input componentsSeparatedByString:TRANSLATE_SPLIT_STRING]; NSString *inputName = [inputArray objectAtIndex:0]; NSString *inputShortDesc = [inputArray objectAtIndex:1]; NSString *inputDesc = [inputArray objectAtIndex:2]; NSString *gotName = inputName; NSString *gotShortDesc = inputShortDesc; NSString *gotDesc = inputDesc; NSArray *gotArray = [gotString componentsSeparatedByString:TRANSLATE_SPLIT_STRING]; if ([gotArray count] == 3) { gotName = [gotArray objectAtIndex:0]; gotShortDesc = [gotArray objectAtIndex:1]; gotDesc = [gotArray objectAtIndex:2]; } if ([gotName length] > 30) { gotName = inputName; NSLog(@"%@: name is out of length", file); } if ([gotShortDesc length] > 80) { gotShortDesc = inputShortDesc; NSLog(@"%@: short desc is out of length", file); } if ([gotDesc length] > 4000) { gotDesc = inputDesc; NSLog(@"%@: desc is out of length", file); } gotString = [NSString stringWithFormat:@"%@%@%@%@%@", gotName, TRANSLATE_SPLIT_STRING, gotShortDesc, TRANSLATE_SPLIT_STRING, gotDesc]; NSString *writeToPath = [NSString stringWithFormat:@"%@/%@", translateInfo.path.config.translate, [file lastPathComponent]]; [ZGFileUtils createDirectoryIfNotExist:translateInfo.path.config.translate]; [gotString writeToFile:writeToPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; } break; } sleep(3); } } @end