Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@marciomazza
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marciomazza/a275f98285490ba90b79 to your computer and use it in GitHub Desktop.
Save marciomazza/a275f98285490ba90b79 to your computer and use it in GitHub Desktop.
Script to aid on a "reverse translation" FROM [a portuguese codebase with no i18n] TO [an english one with i18n]
# Script to aid on a "reverse translation":
# * FROM a portuguese codebase with no i18n
# * TO an english one with i18n
#
# Process:
#
# In the cenario wher your have all your code with portuguese strings and no i18n
# (Naturally that applies to other languages as well)
#
# 1. Mark all your translatable portuguese strings with gettext marks _(...)
# 2. generate a temporary .po file and save it as version "pt"
# 3. translate to english inplace (that means in the files itself)
# 4. generate another temporary .po file and save it as version "en"
# 5. call translate(...) to generate the final .po file from the previous two
def read_lines_from_file(filename):
with open(filename, "r") as f:
return f.readlines()
def write_lines_to_file(filename, content):
with open(filename, "w") as text_file:
text_file.writelines(content)
def translated_sequence(en, pt):
translations = dict((a, b.replace('msgid', 'msgstr'))
for a, b in zip(en, pt)
if a != b and a.startswith('msgid'))
en = iter(en)
line = next(en, None)
while line:
match = translations.get(line, None)
if match:
yield line
assert next(en) == 'msgstr ""\n' # we skip this one
yield match
else:
yield line
line = next(en, None)
def translate(en_filename, pt_filename, final_filename):
en, pt = read_lines_from_file(en_filename), read_lines_from_file(pt_filename)
write_lines_to_file(final_filename, translated_sequence(en, pt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment