Java English Translation

博客涉及Java函数相关内容,但具体内容缺失。Java是后端开发常用语言,函数在程序设计中至关重要,可实现特定功能,提高代码复用性。
F
1Function invocation 函数调用
import json import os from android import XmlHandler from common import Config, TextHandler, FormatSpecifierParser from ios.StringsFileAnalysis import StringsFileAnalysis def get_android_key(val): for key, value in Config.ANDROID_LOCALE_DICT.items(): if value==val: return key return(val) def get_ios_key(val): for key, value in Config.IOS_LOCALE_DICT.items(): if value==val: return key return(val) def get_flutter_key(val): for key, value in Config.FLUTTER_LOCALE_DICT.items(): if value==val: return key return(val) def __adjust_word_in_android(word) -> str: word = str(word) replace_dictionary = {'–': '-', ' ': ' ', '…': '...', '&': '&', '–': '-', '…': '...','%': '%'} for key in replace_dictionary.keys(): word = word.replace(key, replace_dictionary[key]) text = str(word).strip() return text def _modify_item_in_strings_file(dst_strings_element_dict: dict, dst_strings_path: str): f = open(dst_strings_path, 'rb') file_list = f.readlines() f.close() file_list_update = False for element in dst_strings_element_dict: modified_strings_content = element.get_element_string_with_key_and_value(element.key, '\"' + TextHandler.deal_all_in_translate_result( dst_strings_element_dict[element]) + '\"') index = 0 found = False for line in file_list: if line.decode(encoding='utf-8').find(element.key) == 0: found = True break index = index + 1 if found: file_list_update = True file_list[index] = bytes(modified_strings_content, 'utf-8') if file_list_update: f = open(dst_strings_path, 'wb') f.writelines(file_list) f.close() def _check_english_translate(english: str, result: str): # 检查回车符和换行符 for s in ['\\r', '\\n']: if not english.count(s) == result.count(s): log_output = "回车符或者换行符不一致: " + s + " english: " + english + " result: " + result + "\r\n" print(log_output) return 0 # 检查格式占位符 for s in ['%@', '%s', '%f', '%d']: if not english.count(s) == result.count(s): log_output = "格式占位符不一致: " + s + " english: " + english + " result: " + result + "\r\n" print(log_output) return 1 # 检查翻译中是否有换行 if '\n' in result: log_output = "翻译中有换行:" + " english: " + english + " result: " + result + "\r\n" print(log_output) return 2 # 检查缩略词大小写 for s in Config.ABBR_CHECK_DICT.keys(): if result.lower().count(s) > 0: if not Config.ABBR_CHECK_DICT.get(s) in result: log_output = "缩略词错误 : " + s + " english:" + english + " result: " + result + "\r\n" print(log_output) return 3 return -1 if __name__ == "__main__": extract_root_path = os.path.join(Config.WORKSPACE_BASE_PATH + r'\Deco_APP_Sprint_All') android_extract_root = os.path.join(extract_root_path, 'Android') android_extract_root_path = os.path.join(android_extract_root, 'values-old') android_extract_root_path_new = os.path.join(android_extract_root, 'values-new') ios_extract_root_path = os.path.join(extract_root_path, 'iOS') android_old_xml_file = os.path.join(android_extract_root_path, 'values', 'strings.xml') android_new_english_list = XmlHandler.parse_xml_to_kv_list(android_old_xml_file) new_english = [englist[1] for englist in android_new_english_list] pc_extract_root_path = os.path.join(extract_root_path, 'PC') pc_old_extract_root_path = os.path.join(pc_extract_root_path, 'old') pc_new_extract_root_path = os.path.join(pc_extract_root_path, 'new') pc_english_path = os.path.join(pc_old_extract_root_path, 'en.json') flutter_extract_root_path = os.path.join(extract_root_path, 'Flutter') flutter_old_extract_root_path = os.path.join(flutter_extract_root_path, 'old') flutter_new_extract_root_path = os.path.join(flutter_extract_root_path, 'new') flutter_english_path = os.path.join(flutter_old_extract_root_path, 'intl_en.arb') android_new_english_dict = XmlHandler.parse_xml_to_kv_dict(android_old_xml_file) android_translate_dir = os.listdir(android_extract_root_path) all_english_translate_dict = dict() # locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'android_englist_translate_dict.text') # with open(locale_json_dst_file, 'r', encoding='utf-8') as f: # all_english_translate_dict = json.load(f) # ----------------- # 提取Android里有重复英文的文案 tmp_english_dict = dict() for key in android_new_english_dict.keys(): android_new_english_list = list(android_new_english_dict.values()) if android_new_english_list.count(android_new_english_dict[key]) > 1: tmp_english_dict[android_new_english_dict[key]] = android_new_english_list.count(android_new_english_dict[key]) locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'differ_english_dict.text') with open(locale_json_dst_file, 'w', encoding='utf8') as f2: json.dump(tmp_english_dict, f2, ensure_ascii=False, indent=2) # ----------------- ## 提取Android文案和iOS共有文案 # for key in android_new_english_dict.keys(): # for values_dir in android_translate_dir: # translate_xml_file = os.path.join(android_extract_root_path, values_dir, 'strings.xml') # translate_dict = XmlHandler.parse_xml_to_kv_dict(translate_xml_file) # for translate_key in translate_dict.keys(): # if key == translate_key: # all_english_key = __adjust_word_in_android(FormatSpecifierParser.replace_java_format_char(android_new_english_dict[key].replace('100%', '100%'))) # new_language_key = get_android_key(values_dir.replace('values-', '')) # translate_value = __adjust_word_in_android(FormatSpecifierParser.replace_java_format_char(translate_dict[translate_key].replace('100%', '100%'))) # if all_english_key in all_english_translate_dict.keys(): # if new_language_key in all_english_translate_dict[all_english_key].keys(): # if translate_value not in all_english_translate_dict[all_english_key][new_language_key]: # tmp_translate_list = list() # tmp_translate_list.append(all_english_translate_dict[all_english_key][new_language_key]) # tmp_translate_list.append(translate_value) # all_english_translate_dict[all_english_key][new_language_key] = tmp_translate_list # print(values_dir, new_language_key, all_english_key, translate_value, all_english_translate_dict[all_english_key][new_language_key]) # else: # print(values_dir, all_english_key, new_language_key, translate_value) # all_english_translate_dict[all_english_key][new_language_key] = translate_value # else: # tmp_translate_dict = dict() # print(values_dir, all_english_key, new_language_key, translate_value) # tmp_translate_dict[new_language_key] = translate_value # all_english_translate_dict[all_english_key] = tmp_translate_dict # break # # 读写Android翻译字典 # locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'android_englist_translate_dict.text') # with open(locale_json_dst_file, 'w', encoding='utf8') as f2: # json.dump(all_english_translate_dict, f2, ensure_ascii=False, indent=2) ## ----------------- # with open(locale_json_dst_file, 'r', encoding='utf-8') as f: # all_english_translate_dict = json.load(f) # ## ----------------- # # 读取iOS英文文案 ios_old_xml_dir = os.path.join(extract_root_path, 'en.lproj') ios_have_translation_english_dict = dict() ios_have_translation_strings_list = os.listdir(ios_old_xml_dir) for file_name in ios_have_translation_strings_list: if file_name in Config.IOS_IGNORE_COMPARE_STRINGS_NAME_LIST: continue en_strings_path = os.path.join(ios_old_xml_dir, file_name) en_strings_analysis = StringsFileAnalysis(en_strings_path) en_strings_analysis.analyze() en_strings_content_dict = en_strings_analysis.analyze_dictionary_result for key in en_strings_content_dict: value = en_strings_content_dict[key].value.strip() key = TextHandler.remove_double_quotation_marks_in_head_tail(key.strip()) value = TextHandler.deal_all_in_translate_result( (TextHandler.remove_double_quotation_marks_in_head_tail(value))) ios_have_translation_english_dict[key] = value # # 提取iOS独有文案 # ios_language_dir = os.listdir(ios_extract_root_path) # for ios_english_key in ios_have_translation_english_dict.keys(): # ios_english_value = ios_have_translation_english_dict[ios_english_key] # if ios_english_value not in all_english_translate_dict.keys(): # for ios_language_path in ios_language_dir: # ios_src_english_path = os.path.join(ios_extract_root_path, ios_language_path) # strings_file_array = os.listdir(ios_src_english_path) # for strings_file in strings_file_array: # en_strings_path = os.path.join(ios_src_english_path, strings_file) # print(en_strings_path) # en_strings_analysis = StringsFileAnalysis(en_strings_path) # en_strings_analysis.analyze() # en_strings_content_dict = en_strings_analysis.analyze_dictionary_result # for ios_translate_key in en_strings_content_dict.keys(): # new_language_key = get_ios_key(ios_language_path.replace('.lproj', '')) # value = en_strings_content_dict[ios_translate_key].value.strip() # key = TextHandler.remove_double_quotation_marks_in_head_tail(ios_translate_key.strip()) # value = TextHandler.deal_all_in_translate_result( # (TextHandler.remove_double_quotation_marks_in_head_tail(value))) # if ios_english_key == key: # if ios_english_value in all_english_translate_dict.keys(): # if new_language_key in all_english_translate_dict[ios_english_value].keys(): # if value not in all_english_translate_dict[ios_english_value][new_language_key]: # tmp_translate_list = list() # tmp_translate_list.append(all_english_translate_dict[ios_english_value][new_language_key]) # tmp_translate_list.append(value) # all_english_translate_dict[ios_english_value][new_language_key] = tmp_translate_list # print(new_language_key, strings_file, ios_english_key, ios_english_value, value) # else: # all_english_translate_dict[ios_english_value][new_language_key] = value # else: # tmp_translate_dict = dict() # tmp_translate_dict[new_language_key] = value # all_english_translate_dict[ios_english_value] = tmp_translate_dict # break # # locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'all_englist_translate_dict.text') # with open(locale_json_dst_file, 'w', encoding='utf8') as f2: # json.dump(all_english_translate_dict, f2, ensure_ascii=False, indent=2) ## ----------------- # 读取翻译字典 locale_json_dst_file = os.path.join(os.path.join(extract_root_path), 'all_englist_translate_dict_new.text') with open(locale_json_dst_file, 'r', encoding='utf-8') as f: all_english_translate_dict = json.load(f) # 合入iOS文案 ios_language_dir = os.listdir(ios_extract_root_path) for ios_language_path in ios_language_dir: ios_src_english_path = os.path.join(ios_extract_root_path, ios_language_path) strings_file_array = os.listdir(ios_src_english_path) new_language_key = get_ios_key(ios_language_path.replace('.lproj', '')) for strings_file in strings_file_array: if strings_file in Config.IOS_IGNORE_COMPARE_STRINGS_NAME_LIST: continue en_strings_path = os.path.join(ios_src_english_path, strings_file) en_strings_analysis = StringsFileAnalysis(en_strings_path) en_strings_analysis.analyze() en_strings_content_list = en_strings_analysis.analyze_array_result dst_strings_element_dict = dict() for strings_element in en_strings_content_list: strings_element_key = TextHandler.remove_double_quotation_marks_in_head_tail( strings_element.key.strip()) strings_element_english = TextHandler.remove_double_quotation_marks_in_head_tail( strings_element.value.strip()) dst_strings_element_dict[strings_element] = strings_element_english if strings_element_key in ios_have_translation_english_dict.keys() and strings_element_english == ios_have_translation_english_dict[strings_element_key] and strings_element_english not in Config.IOS_TRANSLATION_IGNORE_ENGLISH_LIST: if strings_element_english in all_english_translate_dict.keys() and new_language_key in all_english_translate_dict[strings_element_english].keys(): ios_translate_value = all_english_translate_dict[strings_element_english][new_language_key] if isinstance(ios_translate_value, list): print(ios_language_path, strings_file, strings_element_english, ios_translate_value) dst_strings_element_dict[strings_element] = ios_translate_value[0] else: dst_strings_element_dict[strings_element] = ios_translate_value _modify_item_in_strings_file(dst_strings_element_dict, en_strings_path) # 合入Android文案 android_dir = os.listdir(android_extract_root_path) for file_name in android_dir: if file_name == 'values': continue localization_dir_path = os.path.join(android_extract_root_path, file_name, 'strings.xml') localization_dst_path = os.path.join(android_extract_root_path_new, file_name, 'strings.xml') locale_dict = XmlHandler.parse_xml_to_kv_dict(localization_dir_path) new_locale_xml_dict = dict() none_translation_dict = dict() for english_key in android_new_english_dict: english = FormatSpecifierParser.replace_java_format_char(android_new_english_dict[english_key]) if english in all_english_translate_dict: new_language_key = get_android_key(file_name.replace('values-', '')) if new_language_key in all_english_translate_dict[english]: translation = all_english_translate_dict[english][new_language_key] if isinstance(translation, list): translation = translation[0] if translation == "": none_translation_dict[english_key] = english continue _check_english_translate(english, translation) if english != translation: if english_key not in locale_dict or locale_dict[english_key] == english: new_locale_xml_dict[english_key] = FormatSpecifierParser.build_format_char( android_new_english_dict[english_key], english, translation) # print('111 ' + locale_dict[english_key] + english, translation, android_new_english_dict[english_key], new_locale_xml_dict[english_key]) elif english_key not in all_english_translate_dict: if not english.startswith('@string/'): none_translation_dict[english_key] = english # new_locale_xml_dict[english_key] = android_new_english_dict[english_key] # print('222 ' + english_key, new_locale_xml_dict[english_key]) for english_key in locale_dict: if english_key in new_locale_xml_dict: continue new_locale_xml_dict[english_key] = locale_dict[english_key] print('333 ' + english_key + ' ' + new_locale_xml_dict[english_key]) for english_key in android_new_english_dict: if english_key not in locale_dict: continue english = android_new_english_dict[english_key] found_exist_translation_key = None for none_key in none_translation_dict: if english_key != none_key and english == none_translation_dict[none_key]: found_exist_translation_key = none_key break if found_exist_translation_key is not None: new_locale_xml_dict[found_exist_translation_key] = locale_dict[english_key] print('444 ' + english_key + ' ' + new_locale_xml_dict[english_key]) none_translation_dict.pop(found_exist_translation_key) XmlHandler.convert_to_xml(new_locale_xml_dict, localization_dst_path) # 合入PC文案 # pc_english_dict = dict() # with open(pc_english_path, 'r', encoding='utf-8') as f: # pc_english_dict = json.load(f) # pc_dir = os.listdir(pc_old_extract_root_path) # pattern = r'\{(.*?)\}' # for file_name in pc_dir: # if file_name != 'en.json': # translate_json = os.path.join(pc_old_extract_root_path, file_name) # translate_json_dict = dict() # with open(translate_json, 'r', encoding='utf-8') as f: # translate_json_dict = json.load(f) # print('\n', file_name, '\n') # for pc_english_key in pc_english_dict: # english = pc_english_dict[pc_english_key] # translation = english # if '{' in english and '}' in english: # englist_result = re.findall(pattern, english) # if len(englist_result) > 0: # count = 0 # for place_holder in englist_result: # english = str.replace(english, '{' + englist_result[count] + '}', # '{' + place_holder + '}') # # print(english) # count += 1 # if english in all_english_translate_dict: # new_language_key = get_ios_key(file_name.replace('.json', '')) # print(english, new_language_key) # if new_language_key in all_english_translate_dict[english].keys(): # translation = all_english_translate_dict[english][new_language_key] # if english == translation or (pc_english_key in translate_json_dict.keys() and translate_json_dict[pc_english_key] != english): # print(pc_english_key, english, translation) # else: # translate_json_dict[pc_english_key] = translation # locale_json_dst_file = os.path.join(pc_new_extract_root_path, file_name) # with open(locale_json_dst_file, 'w', encoding='utf8') as f2: # json.dump(translate_json_dict, f2, ensure_ascii=False, indent=2) # # 合入Flutter文案 # flutter_english_dict = dict() # with open(flutter_english_path, 'r', encoding='utf-8') as f: # flutter_english_dict = json.load(f) # flutter_dir = os.listdir(flutter_old_extract_root_path) # pattern = r'\{(.*?)\}' # for file_name in flutter_dir: # if file_name != 'intl_en.arb': # # if file_name != 'en.json': # translate_json = os.path.join(flutter_old_extract_root_path, file_name) # translate_json_dict = dict() # with open(translate_json, 'r', encoding='utf-8') as f: # translate_json_dict = json.load(f) # print('\n', file_name, '\n') # for flutter_english_key in flutter_english_dict: # english = flutter_english_dict[flutter_english_key] # translation = english # if '{' in english and '}' in english: # englist_result = re.findall(pattern, english) # if len(englist_result) > 0: # count = 0 # for place_holder in englist_result: # english = str.replace(english, '{' + englist_result[count] + '}', # '{' + place_holder + '}') # # print(english) # count += 1 # if english in all_english_translate_dict: # new_language_key = get_flutter_key(file_name.replace('.arb', '').replace('intl_', '')) # print(english, new_language_key) # if new_language_key in all_english_translate_dict[english].keys(): # translation = all_english_translate_dict[english][new_language_key] # if english == translation or flutter_english_key in translate_json_dict.keys() and translate_json_dict[flutter_english_key] != english: # print(flutter_english_key, english) # else: # translate_json_dict[flutter_english_key] = translation # # _check_english_translate(english, translation) # locale_json_dst_file = os.path.join(flutter_new_extract_root_path, file_name) # with open(locale_json_dst_file, 'w', encoding='utf8') as f2: # json.dump(translate_json_dict, f2, ensure_ascii=False, indent=2) 代码解析
10-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值