2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-17 04:35:26 +00:00

Auto generate a list of supported locales whenever translations are updated

This commit is contained in:
Oliver Walters
2022-05-02 13:07:04 +10:00
parent 5e4abcf68c
commit 71c16e0556
3 changed files with 41 additions and 34 deletions

View File

@ -15,8 +15,6 @@ import glob
from posixpath import dirname
import shutil
import re
import json
import sys
def process_locale_file(filename, locale_name):
"""
@ -84,11 +82,38 @@ def copy_locale_file(path):
process_locale_file(fallback_file, locale)
def generate_locale_list(locales):
"""
Generate a .dart file which contains all the supported locales,
for importing into the project
"""
with open("supported_locales.dart", "w") as output:
output.write("// This file is auto-generated by the 'collect_translations.py' script - do not edit it directly!\n\n")
output.write('import "package:flutter/material.dart";\n\n')
output.write("const List<Locale> supported_locales = [\n");
for locale in locales:
splt = locale.split("_")
if len(splt) == 2:
lc, cc = splt
else:
lc = locale
cc = ''
output.write(f' const Locale("{lc}", "{cc}"), // Translations avilable in app_{locale}.arb\n')
output.write("];\n")
output.write("")
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)
@ -99,7 +124,7 @@ if __name__ == '__main__':
for arb in arbs:
os.remove(arb)
locales = []
locales = ['en']
for locale in os.listdir(here):
@ -111,6 +136,7 @@ if __name__ == '__main__':
if os.path.exists(f) and os.path.isdir(locale):
copy_locale_file(f)
locales.append(locale)
# Ensure the translation source file ('app_en.arb') is copied also
# Note that this does not require any further processing
@ -118,3 +144,7 @@ if __name__ == '__main__':
dst = os.path.join(here, 'collected', 'app_en.arb')
shutil.copyfile(src, dst)
generate_locale_list(locales)
print(f"Updated translations for {len(locales)} locales.")