def upload_missing_json_translations_to_transifex(source_string_path): source_strings = get_json_strings(source_string_path) langs_dir_path = os.path.dirname(os.path.dirname(source_string_path)) lang_codes = get_acceptable_json_lang_codes(langs_dir_path) filename = transifex_name_from_filename(source_string_path, '') for lang_code in lang_codes: l10n_path = os.path.join(langs_dir_path, lang_code, 'messages.json') l10n_strings = get_json_strings(l10n_path) l10n_dict = { string_name: string_value for (string_name, string_value, _) in l10n_strings } for (string_name, string_value, _) in source_strings: if string_name not in l10n_dict: # print(f'Skipping string name {string_name} for language ' + # f'{lang_code}: non-existent') continue if l10n_dict[string_name] == string_value: # print(f'Skipping string name {string_name} for language ' + # f'{lang_code}: not localized') continue translation_value = (l10n_dict[string_name].replace( "\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n")) upload_missing_translation_to_transifex(source_string_path, lang_code, filename, string_name.split(".")[0], translation_value)
def upload_source_string_file_to_transifex(source_file_path, filename, xml_content, i18n_type): """Uploads the specified source string file to transifex""" print(f'Uploading resource for filename {filename}') resource_name = transifex_name_from_filename(source_file_path, filename) return get_api_wrapper().transifex_upload_resource_content( resource_name, xml_content, i18n_type)
def upload_string_desc(source_file_path, filename, string_name, string_desc): """Uploads descriptions for strings""" string_hash = get_transifex_string_hash(string_name) resource_name = transifex_name_from_filename(source_file_path, filename) print('Uploading string description for string: ' f'{string_name} (hash: {string_hash})') get_api_wrapper().transifex_upload_string_desc(resource_name, string_hash, string_desc)
def upload_missing_translation_to_transifex(source_string_path, lang_code, filename, string_name, translated_value): """Uploads the specified string to the specified language code.""" resource_name = transifex_name_from_filename(source_string_path, filename) string_hash = get_transifex_string_hash(string_name) translated_value = braveify(translated_value) get_api_wrapper().transifex_upload_string_l10n(resource_name, string_hash, lang_code, translated_value) print(f'Uploaded {lang_code} string: {string_name}...')
def get_transifex_translation_file_content(source_file_path, filename, lang_code, dump_path): """Obtains a translation Android xml format and returns the string""" lang_code = xtb_lang_to_transifex_lang(lang_code) resource_name = transifex_name_from_filename(source_file_path, filename) ext = os.path.splitext(source_file_path)[1] content = get_api_wrapper().transifex_get_resource_l10n( resource_name, lang_code, ext) content = fix_transifex_translation_file_content(content, ext) if dump_path: with open(dump_path, mode='wb') as f: f.write(content) verify_transifex_translation_file_content(content, ext) return content.decode('utf-8')
def get_transifex_source_resource_strings(grd_file_path): """Obtains the list of strings from Transifex""" filename = os.path.basename(grd_file_path).split('.')[0] resource_name = transifex_name_from_filename(grd_file_path, filename) content = get_api_wrapper().transifex_get_resource_source(resource_name) return get_strings_dict_from_xml_content(content)