Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script to synchronise translations from the English file to the … #72

Merged
merged 3 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))