2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-27 04:56:48 +00:00

Translation script creates fallback locale files

This commit is contained in:
Oliver Walters 2022-05-02 12:49:59 +10:00
parent 26e48f57eb
commit 5e4abcf68c
2 changed files with 35 additions and 12 deletions

View File

@ -1,4 +1,4 @@
arb-dir: lib/l10n
arb-dir: lib/l10n/collected
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
output-class: I18N

View File

@ -11,21 +11,20 @@ So, simply copy them here!
"""
import os
import glob
from posixpath import dirname
import shutil
import re
import json
import sys
def process_locale_file(filename):
def process_locale_file(filename, locale_name):
"""
Process a locale file after copying
- Ensure the 'locale' matches
"""
# Extract the locale name from the filename
f = os.path.basename(filename)
locale = re.search(r"^app\_(\w+)\.arb$", f).groups()[0]
# TODO: Use JSON processing instead of manual
# Need to work out unicode issues for this to work
@ -38,7 +37,7 @@ def process_locale_file(filename):
# but it does not preserve unicode data!
for line in lines:
if '@@locale' in line:
new_line = f' "@@locale": "{locale}"'
new_line = f' "@@locale": "{locale_name}"'
if ',' in line:
new_line += ','
@ -67,26 +66,50 @@ def copy_locale_file(path):
shutil.copyfile(src, dst)
print(f"Copied file '{f}'")
process_locale_file(dst)
locale = os.path.split(path)[-1]
process_locale_file(dst, locale)
# Create a "fallback" locale file, without a country code specifier, if it does not exist
r = re.search(r"app_(\w+)_(\w+).arb", f)
locale = r.groups()[0]
fallback = f"app_{locale}.arb"
fallback_file = os.path.join(here, 'collected', fallback)
if not os.path.exists(fallback_file):
print(f"Creating fallback file:", fallback_file)
shutil.copyfile(dst, fallback_file)
process_locale_file(fallback_file, locale)
if __name__ == '__main__':
here = os.path.abspath(os.path.dirname(__file__))
# Ensure the 'collected' output directory exists
output_dir = os.path.join(here, 'collected')
os.makedirs(output_dir, exist_ok=True)
for item in os.listdir(here):
# Remove existing .arb files from output directory
arbs = glob.glob(os.path.join(output_dir, "*.arb"))
for arb in arbs:
os.remove(arb)
locales = []
for locale in os.listdir(here):
# Ignore the output directory
if item == 'collected':
if locale == 'collected':
continue
f = os.path.join(here, item)
f = os.path.join(here, locale)
if os.path.exists(f) and os.path.isdir(item):
if os.path.exists(f) and os.path.isdir(locale):
copy_locale_file(f)
# Ensure the translation source file ('app_en.arb') is copied also