Skip to content

Commit

Permalink
Merge pull request #72 from LEDfan/sync_trans_script
Browse files Browse the repository at this point in the history
Add a script to synchronise translations from the English file to the …
  • Loading branch information
LEDfan authored Sep 5, 2017
2 parents b7ee6cb + ed5b0de commit 6477f4e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
web-ext-artifacts
.web-extension-id
*.pyc
2 changes: 1 addition & 1 deletion _locales/nl/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@
"message": "De Secure Storage kon niet geïnitialiseerd worden.",
"description": "Message shown when the secure storage can't be initialized"
}
}
}
77 changes: 77 additions & 0 deletions sync_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python3

# @copyright Tobia De Koninck
# @copyright Robin Jadoul
#
# This file is part of Keywi.
# Keywi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Keywi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Keywi. If not, see <http://www.gnu.org/licenses/>.

"""
This script synchronises a translated language file with the English one
It will:
- add new keys from `en/message.json` to any other language file
- update descriptions from `en/messages.json` to any other language file
"""

import os
import os.path
from os.path import join
import json
from collections import OrderedDict

translations = {}


def sync_translation(base_path, base, target_file_name):
added = []
updated = []
not_translated = []

with open(join(base_path, target_file_name)) as file:
target = json.load(file, object_pairs_hook=OrderedDict)

for id in sorted(base):
if id not in target:
added.append(id)
target[id] = base[id]
elif target[id]["description"] != base[id]["description"]:
updated.append(id)
target[id]["description"] = base[id]["description"]

if target[id]["message"] == base[id]["message"]:
not_translated.append(id)

with open(join(base_path, target_file_name), 'w') as file:
json.dump(target, file, indent=2, ensure_ascii=False)

return {"added": added, "updated": updated, "nottranslated": not_translated}


if __name__ == "__main__":
base_path = os.path.dirname(__file__)

with open(join(base_path, '_locales', 'en', 'messages.json')) as file:
enTranslation = json.load(file)

for f in os.listdir(join(base_path, '_locales')):
if f != "en":
stats = sync_translation(base_path, enTranslation, os.path.join("_locales", f, "messages.json"))

for key in stats["added"]:
print("[" + str(f) + "] Added " + str(key))

for key in stats["updated"]:
print("[" + str(f) + "] Updated " + str(key))

for key in stats["nottranslated"]:
print("[" + str(f) + "] Needs translation " + str(key))

0 comments on commit 6477f4e

Please sign in to comment.