def update_android_resource(self): repository.load() # 解析指定的多语言路径中的词条 xmlOperator = XmlOperator() # 更新多语言 for f in os.listdir(self.appConfig.android_resources_root_directory): language = self.__get_android_file_language(f) if len(language) <= 0: continue # 语言名称 repository.try_to_add_new_language(language) # 更新词条 for f in os.listdir(self.appConfig.android_resources_root_directory): language = self.__get_android_file_language(f) if len(language) <= 0: continue path = os.path.join( self.appConfig.android_resources_root_directory, f, "strings.xml") # 词条新增 or 变更 dist = xmlOperator.read_android_resources(path) for k, v in dist.items(): repository.try_to_add_new_keyword(k, v, language) # 重写 repo json repository.rewrite_repo_json()
def import_translated_excel(self, fname): # 读取 Excel excelOperator = ExcelOperator() dists = excelOperator.read_excel(fname) if TRANSLATE_EXCEL_SHEET_NAME in dists: # 加载仓库 repository.load() col_list = dists.get(TRANSLATE_EXCEL_SHEET_NAME) # 获取所有的关键字 keywords = [] for row in range(1, len(col_list[0])): keyword = col_list[0][row] keywords.append(keyword) # 语言到词条映射 for col in range(1, len(col_list)): language = "" translations = [] for row in range(0, len(col_list[col])): value = col_list[col][row] if row == 0: language = value else: translations.append(value) # 词条对应起来 for j in range(0, len(keywords)): keyword = keywords[j] translation = translations[j] # 更新到仓库 repository.update_keyword(keyword, translation, language) # 重写 repo json repository.rewrite_repo_json() logging.debug(dists)
def import_modified_ios_resource(self): repository.load() fileOperator = FileOperator() # 更新词条 for f in os.listdir(self.appConfig.ios_resources_root_directory): language = self.__get_ios_file_language(f) if len(language) <= 0: continue path = os.path.join(self.appConfig.ios_resources_root_directory, f, "Localizable.strings") dist = fileOperator.read_ios_keywords(path) for k, v in dist.items(): repository.try_ro_modify_keyword(k, v, language) # 重写 repo json repository.rewrite_repo_json()
def import_modified_android_resource(self): repository.load() xmlOperator = XmlOperator() # 更新词条 for f in os.listdir(self.appConfig.android_resources_root_directory): language = self.__get_android_file_language(f) if len(language) <= 0: continue path = os.path.join( self.appConfig.android_resources_root_directory, f, "strings.xml") # 词条新增 or 变更 dist = xmlOperator.read_android_resources(path) for k, v in dist.items(): repository.try_ro_modify_keyword(k, v, language) # 重写 repo json repository.rewrite_repo_json()
def start_translate(self, progress_callback, finish_callback): repository.load() translator = BaiduTranslator() dist = repository.get_keywords_to_translate() logging.debug("Dist to translate : " + str(dist)) total = len(dist) count = 0 # 遍历词条 translated_languages = [] for keyword,v in dist.items(): logging.debug("Translating " + keyword) # 清空标记 translated_languages.clear() for item in v: # from_language = item["from"] to_language = item["to"] translation = item["translation"] if to_language in translated_languages: continue # 可能已经被翻译过了 result = translator.translate(translation, to_language) if len(result) != 0: translated = result["translation"] mapped_language = result["mapped_language"] # 反向的映射关系填充翻译结果 reversed_mappings = translator.get_reversed_mapped_languages(mapped_language) logging.debug("Get reversed mappings for " + mapped_language + " : " + str(reversed_mappings)) for reversed_mapping in reversed_mappings: # 标记方向映射列表,更新词条信息 translated_languages.append(reversed_mapping) repository.update_keyword(keyword, translated, reversed_mapping) else: logging.warning("One keyword missed to translate : " + keyword) # 回调 count = count + 1 progress_callback(count * 100 / total) # 重写 repo json repository.rewrite_repo_json() finish_callback()
def gen_ios_resources(self): repository.load() ios_blacklist = self.appConfig.ios_language_black_list fileOperator = FileOperator() for language in repository.languages: # 过滤黑名单 if language in ios_blacklist: continue dist = {} for data in repository.datas: keyword = data["keyword"] translates = data["translates"] translation = translates[language] dist[keyword] = translation # 写入资源 language_dir = os.path.join( self.appConfig.ios_resources_root_directory, language + ".lproj") if not os.path.exists(language_dir): os.mkdir(language_dir) fname = os.path.join(language_dir, "Localizable.strings") fileOperator.write_ios_resources(dist, fname)
def gen_android_resources(self): repository.load() android_blacklist = self.appConfig.android_language_black_list xmlOperator = XmlOperator() for language in repository.languages: # 过滤黑名单 if language in android_blacklist: continue dist = {} for data in repository.datas: keyword = data["keyword"] translates = data["translates"] translation = translates[language] dist[keyword] = translation # 写入资源 filename = "values" if language == "default" else "values-" + language language_dir = os.path.join( self.appConfig.android_resources_root_directory, filename) if not os.path.exists(language_dir): os.mkdir(language_dir) fname = os.path.join(language_dir, "strings.xml") xmlOperator.write_android_resources(dist, fname)
def gen_translate_excel(self, output_dir): repository.load() dist = {} for data in repository.datas: # 增加词条列 keyword = data["keyword"] if "Keyword" not in dist: dist["Keyword"] = [] dist["Keyword"].append(keyword) # 增加翻译列 translates = data["translates"] for k, v in translates.items(): if k not in dist: dist[k] = [] dist[k].append(v) # 生成 Excel 文件 excelOperator = ExcelOperator() out_file = os.path.join( self.appConfig.translate_excel_output_directory, TRANSLATE_EXCEL_FILE_NAME) excelOperator.write_excel(dist, out_file) return True