TranslateUtils.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // TranslateUtils.m
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2017/12/25.
  6. // Copyright © 2017年 zhuge. All rights reserved.
  7. //
  8. #import "TranslateUtils.h"
  9. #import "DataUtils.h"
  10. #import "ProjectInfo.h"
  11. #import "CountryInfoManager.h"
  12. #import "ZGFileUtils.h"
  13. #import "ZGImacroUtils.h"
  14. #import "WordUtils.h"
  15. #define TRANSLATE_SPLIT_STRING @"\n010101010101001\n"
  16. @interface TranslateUtils() {
  17. ProjectInfo_Local_Translate *translateInfo;
  18. NSString *input;
  19. CountryInfoManager *cim;
  20. NSString *tempOutputDir;
  21. }
  22. @end
  23. @implementation TranslateUtils
  24. - (void)translate:(ProjectInfo_Local_Translate *)theTranslateInfo {
  25. translateInfo = theTranslateInfo;
  26. input = [NSString stringWithFormat:@"%@%@%@%@%@"
  27. , translateInfo.appName, TRANSLATE_SPLIT_STRING
  28. , translateInfo.shortDesc, TRANSLATE_SPLIT_STRING
  29. , translateInfo.desc];
  30. input = [input stringByReplacingOccurrencesOfString:@"%" withString:@" percent"];
  31. input = [input stringByReplacingOccurrencesOfString:@"&" withString:@" and "];
  32. input = [input stringByReplacingOccurrencesOfString:@" " withString:@" "];
  33. // 开始翻译
  34. cim = [[CountryInfoManager alloc] init];
  35. tempOutputDir = translateInfo.path.cache;
  36. // [self doTheTranslate];
  37. [self checkTranslateResult];
  38. }
  39. - (void) doTheTranslate {
  40. [ZGFileUtils deleteFileOrDirectoryAtPath:tempOutputDir];
  41. [ZGFileUtils createDirectoryIfNotExist:tempOutputDir];
  42. NSString *jsOutTranslateConfig = @""; // googleplay country code is different with translate country code
  43. NSString *jsOutPlayConfig = @"";
  44. // 组合所有的国家的代码
  45. for (CountryInfo *ci in cim.countryInfos) {
  46. if (![cim.ignoreLanguages containsObject:ci.shortName]) {
  47. NSString *shortName = ci.shortName;
  48. NSString *translateName = ci.translateName;
  49. jsOutTranslateConfig = [NSString stringWithFormat:@"\"%@\", %@", translateName, jsOutTranslateConfig];
  50. jsOutPlayConfig = [NSString stringWithFormat:@"\"%@\", %@", shortName, jsOutPlayConfig];
  51. }
  52. }
  53. jsOutTranslateConfig = [jsOutTranslateConfig substringToIndex:[jsOutTranslateConfig length] - 2];
  54. jsOutPlayConfig = [jsOutPlayConfig substringToIndex:[jsOutPlayConfig length] - 2];
  55. // 忽略的国家,直接把默认的语言写过去
  56. for (NSString *shortName in cim.ignoreLanguages) {
  57. NSString *outputFilePath = [NSString stringWithFormat:@"%@/%@.txt", tempOutputDir, shortName];
  58. [input writeToFile:outputFilePath atomically:true encoding:NSUTF8StringEncoding error:nil];
  59. }
  60. // do translate
  61. NSString *translateIIMPath = [NSString stringWithFormat:@"%@/Translate/", [ZGImacroUtils iimPathOfMacOS]];
  62. [ZGFileUtils createDirectoryIfNotExist:translateIIMPath];
  63. NSString *dotPath = [[NSBundle mainBundle] pathForResource:@"doOneTranslate.iim" ofType:nil];
  64. [ZGFileUtils copyFileFromPath:dotPath toPath:translateIIMPath];
  65. // do translate.js
  66. input = [WordUtils urlEncode:input];
  67. NSString *iimFilePath = [NSString stringWithFormat:@"%@/robot.js", translateIIMPath];
  68. NSString *jsTemplatePath = [[NSBundle mainBundle] pathForResource:@"TranslateTemplate.js" ofType:nil];
  69. NSString *jsTemplate = [NSString stringWithContentsOfFile:jsTemplatePath encoding:NSUTF8StringEncoding error:nil];
  70. jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_outputDir_ZG" withString:tempOutputDir];
  71. jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_input_ZG" withString:input];
  72. jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_outTranslateConfig_ZG" withString:jsOutTranslateConfig];
  73. jsTemplate = [jsTemplate stringByReplacingOccurrencesOfString:@"ZG_outPlayConfig_ZG" withString:jsOutPlayConfig];
  74. [jsTemplate writeToFile:iimFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  75. [ZGImacroUtils makeIIMReadable:iimFilePath];
  76. [ZGImacroUtils runIIMFile:@"Translate/robot.js"];
  77. }
  78. - (void) checkTranslateResult {
  79. while (true) {
  80. NSArray *files = [ZGFileUtils arrayOfFileAtDir:tempOutputDir extensionFilterArray:@[@"txt"]];
  81. if ([files count] == [cim.countryInfos count]) {
  82. for (NSString *file in files) {
  83. NSString *fileName = [[file lastPathComponent] stringByDeletingPathExtension];
  84. NSString *s = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
  85. if (![cim.ignoreLanguages containsObject:fileName]) {
  86. s = [s substringFromIndex:1];
  87. s = [s substringToIndex:[s length] - 3];
  88. s = [s stringByReplacingOccurrencesOfString:@"\"\"" withString:@"\""];
  89. s = [s stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
  90. s = [s stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
  91. s = [WordUtils stringByTrimHTML:s];
  92. while ([s rangeOfString:@" 010101010101001001"].location != NSNotFound) {
  93. s = [s stringByReplacingOccurrencesOfString:@" 010101010101001001" withString:@"010101010101001001"];
  94. }
  95. while ([s rangeOfString:@"010101010101001001 "].location != NSNotFound) {
  96. s = [s stringByReplacingOccurrencesOfString:@"010101010101001001 " withString:@"010101010101001001"];
  97. }
  98. while ([s rangeOfString:@" 010101010101001"].location != NSNotFound) {
  99. s = [s stringByReplacingOccurrencesOfString:@" 010101010101001" withString:@"010101010101001"];
  100. }
  101. while ([s rangeOfString:@"010101010101001 "].location != NSNotFound) {
  102. s = [s stringByReplacingOccurrencesOfString:@"010101010101001 " withString:@"010101010101001"];
  103. }
  104. }
  105. NSString *gotString = s;
  106. // check length
  107. NSArray *inputArray = [input componentsSeparatedByString:TRANSLATE_SPLIT_STRING];
  108. NSString *inputName = [inputArray objectAtIndex:0];
  109. NSString *inputShortDesc = [inputArray objectAtIndex:1];
  110. NSString *inputDesc = [inputArray objectAtIndex:2];
  111. NSString *gotName = inputName;
  112. NSString *gotShortDesc = inputShortDesc;
  113. NSString *gotDesc = inputDesc;
  114. NSArray *gotArray = [gotString componentsSeparatedByString:TRANSLATE_SPLIT_STRING];
  115. if ([gotArray count] == 3) {
  116. gotName = [gotArray objectAtIndex:0];
  117. gotShortDesc = [gotArray objectAtIndex:1];
  118. gotDesc = [gotArray objectAtIndex:2];
  119. }
  120. if ([gotName length] > 30) {
  121. gotName = inputName;
  122. NSLog(@"%@: name is out of length", file);
  123. }
  124. if ([gotShortDesc length] > 80) {
  125. gotShortDesc = inputShortDesc;
  126. NSLog(@"%@: short desc is out of length", file);
  127. }
  128. if ([gotDesc length] > 4000) {
  129. gotDesc = inputDesc;
  130. NSLog(@"%@: desc is out of length", file);
  131. }
  132. gotString = [NSString stringWithFormat:@"%@%@%@%@%@", gotName, TRANSLATE_SPLIT_STRING, gotShortDesc, TRANSLATE_SPLIT_STRING, gotDesc];
  133. NSString *writeToPath = [NSString stringWithFormat:@"%@/%@", translateInfo.path.config.translate, [file lastPathComponent]];
  134. [ZGFileUtils createDirectoryIfNotExist:translateInfo.path.config.translate];
  135. [gotString writeToFile:writeToPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  136. }
  137. break;
  138. }
  139. sleep(3);
  140. }
  141. }
  142. @end