Created
January 19, 2016 21:49
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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