ci: Add magic script to automagically fix i18n string format in trivial case

This commit is contained in:
Alexandre Aubin 2021-09-03 02:01:16 +02:00
parent 253eb6575c
commit 10fddc427e
2 changed files with 53 additions and 5 deletions

View file

@ -2,7 +2,7 @@
# TRANSLATION # TRANSLATION
######################################## ########################################
remove-stale-translated-strings: autofix-translated-strings:
stage: translation stage: translation
image: "before-install" image: "before-install"
needs: [] needs: []
@ -14,12 +14,13 @@ remove-stale-translated-strings:
script: script:
- cd tests # Maybe move this script location to another folder? - cd tests # Maybe move this script location to another folder?
# create a local branch that will overwrite distant one # create a local branch that will overwrite distant one
- git checkout -b "ci-remove-stale-translated-strings-${CI_COMMIT_REF_NAME}" --no-track - git checkout -b "ci-autofix-translated-strings-${CI_COMMIT_REF_NAME}" --no-track
- python3 remove_stale_translated_strings.py - python3 remove_stale_translated_strings.py
- python3 autofix_locale_format.py
- '[ $(git diff -w | wc -l) != 0 ] || exit 0' # stop if there is nothing to commit - '[ $(git diff -w | wc -l) != 0 ] || exit 0' # stop if there is nothing to commit
- git commit -am "[CI] Remove stale translated strings" || true - git commit -am "[CI] Autofix translated strings" || true
- git push -f origin "ci-remove-stale-translated-strings-${CI_COMMIT_REF_NAME}":"ci-remove-stale-translated-strings-${CI_COMMIT_REF_NAME}" - git push -f origin "ci-autofix-translated-strings-${CI_COMMIT_REF_NAME}":"ci-autofix-translated-strings-${CI_COMMIT_REF_NAME}"
- hub pull-request -m "[CI] Remove stale translated strings" -b Yunohost:dev -p || true # GITHUB_USER and GITHUB_TOKEN registered here https://gitlab.com/yunohost/yunohost/-/settings/ci_cd - hub pull-request -m "[CI] Autofix translated strings" -b Yunohost:dev -p || true # GITHUB_USER and GITHUB_TOKEN registered here https://gitlab.com/yunohost/yunohost/-/settings/ci_cd
only: only:
variables: variables:
- $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH - $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

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)