def translate_file(cls, root, file_name, target_language): """ convenience method for translating a pot file :param root: the absolute path of folder where the file is present :param file_name: name of the file to be translated (it should be a pot file) :param target_language: language in which the file needs to be translated """ print('translating ', target_language) po = polib.pofile(os.path.join(root, file_name)) strings = [] translations = {} for index, entry in enumerate(po): strings.append(entry.msgid) translations.update({index: entry.msgstr}) # translate the strings, # all the translated strings are returned # in the same order on the same index # viz. [a, b] -> [trans_a, trans_b] translated_strings = translate_strings(strings, target_language, 'en', False) for index, entry in enumerate(po): # Google Translate removes a lot of formatting, these are the fixes: # - Add newline in the beginning if msgid also has that if entry.msgid.startswith( "\n") and not translated_strings[index].startswith("\n"): translated_strings[index] = u"\n" + translated_strings[index] # - Add newline at the end if msgid also has that if entry.msgid.endswith( "\n") and not translated_strings[index].endswith("\n"): translated_strings[index] = translated_strings[index] + u"\n" # Remove spaces that have been placed between %(id) tags translated_strings[index] = re.sub( "%\s*\(\s*(\w+)\s*\)\s*s", lambda match: r'%({})s'.format(match.group(1).lower()), translated_strings[index]) entry.msgstr = translated_strings[index] po.save()
def translate_file(self, root, file_name, target_language): """ convenience method for translating a pot file :param root: the absolute path of folder where the file is present :param file_name: name of the file to be translated (it should be a pot file) :param target_language: language in which the file needs to be translated """ logger.info('filling up translations for locale `{}`'.format(target_language)) po = polib.pofile(os.path.join(root, file_name)) strings = self.get_strings_to_translate(po) # translate the strings, # all the translated strings are returned # in the same order on the same index # viz. [a, b] -> [trans_a, trans_b] translated_strings = translate_strings(strings, target_language, 'en', False) self.update_translations(po, translated_strings) po.save()
def translate_file(cls, root, file_name, target_language): """ convenience method for translating a pot file :param root: the absolute path of folder where the file is present :param file_name: name of the file to be translated (it should be a pot file) :param target_language: language in which the file needs to be translated """ print('translating ', target_language) po = polib.pofile(os.path.join(root, file_name)) strings = [] translations = {} for index, entry in enumerate(po): strings.append(entry.msgid) translations.update({index: entry.msgstr}) # translate the strings, # all the translated strings are returned # in the same order on the same index # viz. [a, b] -> [trans_a, trans_b] translated_strings = translate_strings(strings, target_language, 'en', False) for index, entry in enumerate(po): # Google Translate removes a lot of formatting, these are the fixes: # - Add newline in the beginning if msgid also has that if entry.msgid.startswith("\n") and not translated_strings[index].startswith("\n"): translated_strings[index] = u"\n" + translated_strings[index] # - Add newline at the end if msgid also has that if entry.msgid.endswith("\n") and not translated_strings[index].endswith("\n"): translated_strings[index] = translated_strings[index] + u"\n" # Remove spaces that have been placed between %(id) tags translated_strings[index] = re.sub("%\s*\(\s*(\w+)\s*\)\s*s", lambda match: r'%({})s'.format(match.group(1).lower()),translated_strings[index]) entry.msgstr = translated_strings[index] po.save()