From 513e25d8ce564299628d5f84661c28a472b2709c Mon Sep 17 00:00:00 2001 From: michaelarnauts Date: Wed, 24 Mar 2021 11:24:39 +0100 Subject: [PATCH] Update translation scripts --- .gitattributes | 1 + Makefile | 6 +-- .../check_for_unused_translations.py | 4 ++ scripts/update_translations.py | 40 +++++++++++++++++++ tests/update_translations.py | 31 -------------- 5 files changed, 47 insertions(+), 35 deletions(-) rename {tests => scripts}/check_for_unused_translations.py (91%) create mode 100755 scripts/update_translations.py delete mode 100755 tests/update_translations.py diff --git a/.gitattributes b/.gitattributes index f52271b..f5ed3a4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,6 @@ .env.example export-ignore .github/ export-ignore +scripts/ export-ignore tests/ export-ignore .gitattributes export-ignore .gitignore export-ignore diff --git a/Makefile b/Makefile index 2ca4b66..b49c0cf 100644 --- a/Makefile +++ b/Makefile @@ -32,13 +32,11 @@ check-translations: @$(foreach lang,$(languages), \ msgcmp --use-untranslated resources/language/resource.language.$(lang)/strings.po resources/language/resource.language.en_gb/strings.po; \ ) - @tests/check_for_unused_translations.py + @scripts/check_for_unused_translations.py update-translations: @printf ">>> Updating languages\n" - @-$(foreach lang,$(languages), \ - tests/update_translations.py resources/language/resource.language.$(lang)/strings.po resources/language/resource.language.en_gb/strings.po; \ - ) + @scripts/update_translations.py check-addon: clean build @printf ">>> Running addon checks\n" diff --git a/tests/check_for_unused_translations.py b/scripts/check_for_unused_translations.py similarity index 91% rename from tests/check_for_unused_translations.py rename to scripts/check_for_unused_translations.py index b993a5d..bae1f27 100755 --- a/tests/check_for_unused_translations.py +++ b/scripts/check_for_unused_translations.py @@ -19,6 +19,10 @@ # Load po file po = polib.pofile('resources/language/resource.language.en_gb/strings.po') for entry in po: + # Skip empty translations + if entry.msgid == '': + continue + # Extract msgctxt msgctxt = entry.msgctxt.lstrip('#') diff --git a/scripts/update_translations.py b/scripts/update_translations.py new file mode 100755 index 0000000..60a9cce --- /dev/null +++ b/scripts/update_translations.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# pylint: disable=missing-docstring,no-self-use,wrong-import-order,wrong-import-position,invalid-name + +import sys +from glob import glob + +import polib + +original_file = 'resources/language/resource.language.en_gb/strings.po' +original = polib.pofile(original_file, wrapwidth=0) + +for translated_file in glob('resources/language/resource.language.*/strings.po'): + # Skip original file + if translated_file == original_file: + continue + print('Updating %s...' % translated_file) + + # Load po-files + translated = polib.pofile(translated_file, wrapwidth=0) + + for entry in original: + # Find a translation + translation = translated.find(entry.msgctxt, 'msgctxt') + + if translation and entry.msgid == translation.msgid: + entry.msgstr = translation.msgstr + + original.metadata = translated.metadata + + if sys.platform.startswith('win'): + # On Windows save the file keeping the Linux return character + with open(translated_file, 'wb') as _file: + content = str(original).encode('utf-8') + content = content.replace(b'\r\n', b'\n') + _file.write(content) + else: + # Save it now over the translation + original.save(translated_file) diff --git a/tests/update_translations.py b/tests/update_translations.py deleted file mode 100755 index efc8a07..0000000 --- a/tests/update_translations.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# pylint: disable=missing-docstring,no-self-use,wrong-import-order,wrong-import-position,invalid-name - -import sys - -import polib - -# Load po-files -translated = polib.pofile(sys.argv[1], wrapwidth=0) -english = polib.pofile(sys.argv[2], wrapwidth=0) - -for entry in english: - # Find a translation - translation = translated.find(entry.msgctxt, 'msgctxt') - - if translation and entry.msgid == translation.msgid: - entry.msgstr = translation.msgstr - -english.metadata = translated.metadata - -if sys.platform.startswith('win'): - # On Windows save the file keeping the Linux return character - with open(sys.argv[1], 'wb') as _file: - content = str(english).encode('utf-8') - content = content.replace(b'\r\n', b'\n') - _file.write(content) -else: - # Save it now over the translation - english.save(sys.argv[1])