diff --git a/l10n.yaml b/l10n.yaml index a5b40b9e..7b9da738 100644 --- a/l10n.yaml +++ b/l10n.yaml @@ -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 \ No newline at end of file diff --git a/lib/l10n/collect_translations.py b/lib/l10n/collect_translations.py index f1987e30..9b5c0628 100644 --- a/lib/l10n/collect_translations.py +++ b/lib/l10n/collect_translations.py @@ -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