From f1b7e3005fa013d74bdbd3c0a19c163fd9dcfce2 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 3 Sep 2021 16:35:03 +0200 Subject: [PATCH] ci: add actions to autoreformat locale files --- .github/workflows/i18n.yml | 28 ++++++++++++++++++ test/autofix_locale_format.py | 47 +++++++++++++++++++++++++++++++ test/reformat_locales.py | 38 +++++++++++++++++++++++++ test/remove_stale_i18n_strings.py | 2 +- 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/i18n.yml create mode 100644 test/autofix_locale_format.py create mode 100644 test/reformat_locales.py diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml new file mode 100644 index 00000000..d92b3f57 --- /dev/null +++ b/.github/workflows/i18n.yml @@ -0,0 +1,28 @@ +name: Check / autofix locales +on: + push: + branches: + - dev +jobs: + i18n: + name: Check / auto apply black + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Apply reformating scripts + id: action_reformat + run: | + python3 test/remove_stale_i18n_strings.py + python3 test/autofix_locale_format.py + python3 test/reformat_locales.py + git diff -w --exit-cide + continue-on-error: true + - name: Create Pull Request + if: ${{ failure() }} + uses: peter-evans/create-pull-request@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + title: "Reformat locale files" + commit-message: ":robot: Reformat locale files" + base: ${{ github.head_ref }} + branch: actions/i18n diff --git a/test/autofix_locale_format.py b/test/autofix_locale_format.py new file mode 100644 index 00000000..f777f06f --- /dev/null +++ b/test/autofix_locale_format.py @@ -0,0 +1,47 @@ +import re +import json +import glob + +# List all locale files (except en.json being the ref) +locale_folder = "locales/" +locale_files = glob.glob(locale_folder + "*.json") +locale_files = [filename.split("/")[-1] for filename in locale_files] +locale_files.remove("en.json") + +reference = json.loads(open(locale_folder + "en.json").read()) + + +def fix_locale(locale_file): + + this_locale = json.loads(open(locale_folder + locale_file).read()) + fixed_stuff = False + + # We iterate over all keys/string in en.json + for key, string in reference.items(): + + # Ignore check if there's no translation yet for this key + if key not in this_locale: + continue + + # Then we check that every "{stuff}" (for python's .format()) + # should also be in the translated string, otherwise the .format + # will trigger an exception! + subkeys_in_ref = [k[0] for k in re.findall(r"{(\w+)(:\w)?}", string)] + subkeys_in_this_locale = [k[0] for k in re.findall(r"{(\w+)(:\w)?}", this_locale[key])] + + if set(subkeys_in_ref) != set(subkeys_in_this_locale) and (len(subkeys_in_ref) == len(subkeys_in_this_locale)): + for i, subkey in enumerate(subkeys_in_ref): + this_locale[key] = this_locale[key].replace('{%s}' % subkeys_in_this_locale[i], '{%s}' % subkey) + fixed_stuff = True + + if fixed_stuff: + json.dump( + this_locale, + open(locale_folder + locale_file, "w"), + indent=4, + ensure_ascii=False, + ) + + +for locale_file in locale_files: + fix_locale(locale_file) diff --git a/test/reformat_locales.py b/test/reformat_locales.py new file mode 100644 index 00000000..90251d04 --- /dev/null +++ b/test/reformat_locales.py @@ -0,0 +1,38 @@ +import re + +def reformat(lang, transformations): + + locale = open(f"locales/{lang}.json").read() + for pattern, replace in transformations.items(): + locale = re.compile(pattern).sub(replace, locale) + + open(f"locales/{lang}.json", "w").write(locale) + +###################################################### + +godamn_spaces_of_hell = ["\u00a0", "\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", "\u2008", "\u2009", "\u200A", "\u202f", "\u202F", "\u3000"] + +transformations = {s: " " for s in godamn_spaces_of_hell} +transformations.update({ + "…": "...", +}) + + +reformat("en", transformations) + +###################################################### + +transformations.update({ + "courriel": "email", + "e-mail": "email", + "Courriel": "Email", + "E-mail": "Email", + "« ": "'", + "«": "'", + " »": "'", + "»": "'", + "’": "'", + #r"$(\w{1,2})'|( \w{1,2})'": r"\1\2’", +}) + +reformat("fr", transformations) diff --git a/test/remove_stale_i18n_strings.py b/test/remove_stale_i18n_strings.py index 48f2180e..d3a4fe2a 100644 --- a/test/remove_stale_i18n_strings.py +++ b/test/remove_stale_i18n_strings.py @@ -2,7 +2,7 @@ import json import glob from collections import OrderedDict -locale_folder = "../locales/" +locale_folder = "locales/" locale_files = glob.glob(locale_folder + "*.json") locale_files = [filename.split("/")[-1] for filename in locale_files] locale_files.remove("en.json")