ci: add actions to autoreformat locale files

This commit is contained in:
Alexandre Aubin 2021-09-03 16:35:03 +02:00
parent 10cb1ebefe
commit f1b7e3005f
4 changed files with 114 additions and 1 deletions

28
.github/workflows/i18n.yml vendored Normal file
View file

@ -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

View file

@ -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)

38
test/reformat_locales.py Normal file
View file

@ -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)

View file

@ -2,7 +2,7 @@ import json
import glob import glob
from collections import OrderedDict from collections import OrderedDict
locale_folder = "../locales/" locale_folder = "locales/"
locale_files = glob.glob(locale_folder + "*.json") locale_files = glob.glob(locale_folder + "*.json")
locale_files = [filename.split("/")[-1] for filename in locale_files] locale_files = [filename.split("/")[-1] for filename in locale_files]
locale_files.remove("en.json") locale_files.remove("en.json")