Skip to content

Instantly share code, notes, and snippets.

@AmauryCarrade
Created January 19, 2016 21:49
Show Gist options
  • Save AmauryCarrade/ddfd84a8e43c949114de to your computer and use it in GitHub Desktop.
Save AmauryCarrade/ddfd84a8e43c949114de to your computer and use it in GitHub Desktop.
Translations injector for migration from key-based i18n to gettext (using the zDevelopers/zLib I18n component).
import fnmatch
import os
import yaml
def flat_yaml(stream):
with open(stream) as f:
raw_data = yaml.load(f)
data = {}
flatten_structure(raw_data, data)
return data
def flatten_structure(raw_data, flat, path=""):
for node in raw_data:
if isinstance(raw_data[node], dict):
flatten_structure(raw_data[node], flat, path + node + ".")
else:
flat[path + node] = raw_data[node]
ref_lang = flat_yaml("en_US.yml")
for root, dirnames, filenames in os.walk('../../src'):
for filename in fnmatch.filter(filenames, '*.java'):
filepath = os.path.join(root, filename)
content = ""
with open(filepath) as f:
for line in f:
content += line
updated = False
for key in ref_lang.keys():
real_key = key.replace("keys.", "")
if key != "author" and real_key in content:
print(real_key + " in " + filepath)
content = content.replace("I.t(\"" + real_key + "\"", "I.t(\"" + ref_lang[key].replace("\"", "\\\"") + "\"")
updated = True
if updated:
with open(filepath, mode='w') as f:
f.write(content)
print("Translations included into " + filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment