From 71c16e055646553908062027c70995c922d403e8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 2 May 2022 13:07:04 +1000 Subject: [PATCH 1/4] Auto generate a list of supported locales whenever translations are updated --- lib/l10n/.gitignore | 3 ++- lib/l10n/collect_translations.py | 38 ++++++++++++++++++++++++++++---- lib/main.dart | 34 +++++----------------------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/lib/l10n/.gitignore b/lib/l10n/.gitignore index 1a05426d..d0e30b82 100644 --- a/lib/l10n/.gitignore +++ b/lib/l10n/.gitignore @@ -1,2 +1,3 @@ # Do not track the collected translation files -collected/ \ No newline at end of file +collected/ +supported_locales.dart diff --git a/lib/l10n/collect_translations.py b/lib/l10n/collect_translations.py index 9b5c0628..e6ce6f17 100644 --- a/lib/l10n/collect_translations.py +++ b/lib/l10n/collect_translations.py @@ -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 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.") diff --git a/lib/main.dart b/lib/main.dart index e52a8ff5..f8062e39 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,6 +12,9 @@ import "package:inventree/inventree/sentry.dart"; import "package:inventree/dsn.dart"; import "package:inventree/widget/home.dart"; +// Supported translations are automatically updated +import "package:inventree/l10n/supported_locales.dart"; + Future main() async { @@ -69,36 +72,9 @@ class InvenTreeApp extends StatelessWidget { I18N.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, ], - supportedLocales: [ - const Locale("cs", ""), // Czech - const Locale("de", ""), // German - const Locale("el", ""), // Greek - const Locale("en", ""), // English - const Locale("es-ES", ""), // Spanish - const Locale("es-MX", ""), // Spanish (mexican) - const Locale("fa", ""), // Farsi (Persian) - const Locale("fr", ""), // French - const Locale("he", ""), // Hebrew - const Locale("hu", ""), // Hungarian - const Locale("id", ""), // Indonesian - const Locale("it", ""), // Italian - const Locale("ja", ""), // Japanese - const Locale("ko", ""), // Korean - const Locale("nl", ""), // Dutch - const Locale("no", ""), // Norwegian - const Locale("pl", ""), // Polish - const Locale("pt", ""), // Portuguese - const Locale("pt-BR", ""), - const Locale("ru", ""), // Russian - const Locale("sv", ""), // Swedish - const Locale("th", ""), // Thai - const Locale("tr", ""), // Turkish - const Locale("vi", ""), // Vietnamese - const Locale("zh-CN", ""), // Chinese - ], - + locale: const Locale("hu"), + supportedLocales: supported_locales, ); } } \ No newline at end of file From 95c3bc5ae57f793c1137c478c773cbc239be9cb8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 2 May 2022 13:14:17 +1000 Subject: [PATCH 2/4] Fix delegate issue --- lib/l10n/collect_translations.py | 2 +- lib/main.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/l10n/collect_translations.py b/lib/l10n/collect_translations.py index e6ce6f17..fa1cb884 100644 --- a/lib/l10n/collect_translations.py +++ b/lib/l10n/collect_translations.py @@ -105,7 +105,7 @@ def generate_locale_list(locales): lc = locale cc = '' - output.write(f' const Locale("{lc}", "{cc}"), // Translations avilable in app_{locale}.arb\n') + output.write(f' const Locale("{lc}", "{cc}"), // Translations available in app_{locale}.arb\n') output.write("];\n") output.write("") diff --git a/lib/main.dart b/lib/main.dart index f8062e39..9d97ccc4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -71,9 +71,9 @@ class InvenTreeApp extends StatelessWidget { localizationsDelegates: [ I18N.delegate, GlobalMaterialLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], - locale: const Locale("hu"), supportedLocales: supported_locales, ); } From 69bbc3bec30a8fd16cf45dbb88d1844eec70180b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 2 May 2022 13:20:37 +1000 Subject: [PATCH 3/4] remove const keyword --- lib/l10n/collect_translations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/l10n/collect_translations.py b/lib/l10n/collect_translations.py index fa1cb884..4f0182a0 100644 --- a/lib/l10n/collect_translations.py +++ b/lib/l10n/collect_translations.py @@ -105,7 +105,7 @@ def generate_locale_list(locales): lc = locale cc = '' - output.write(f' const Locale("{lc}", "{cc}"), // Translations available in app_{locale}.arb\n') + output.write(f' Locale("{lc}", "{cc}"), // Translations available in app_{locale}.arb\n') output.write("];\n") output.write("") From 501971681e4d7de6b22cb222bb52d62d180c6eeb Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 2 May 2022 13:28:47 +1000 Subject: [PATCH 4/4] Adds missing step from android CI build --- .github/workflows/android.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/android.yaml b/.github/workflows/android.yaml index 32aed76d..a9d49403 100644 --- a/.github/workflows/android.yaml +++ b/.github/workflows/android.yaml @@ -32,6 +32,10 @@ jobs: uses: gradle/gradle-build-action@v2 with: gradle-version: 6.1.1 + - name: Collect Translation Files + run: | + cd lib/l10n + python3 collect_translations.py - name: Build for Android run: | flutter pub get