diff --git a/app/scripts/glossaire.sh b/app/scripts/glossaire.sh index afdae272..6d1a5193 100755 --- a/app/scripts/glossaire.sh +++ b/app/scripts/glossaire.sh @@ -213,8 +213,8 @@ function updateAndroidl10n() { fi echogreen "Extract strings for android-l10n" cd $install - nice -20 $install/app/scripts/tmx/tmx_projectconfig.py $android_l10n/firefox.toml --ref en-US --repo android_l10n - nice -20 $install/app/scripts/tmx/tmx_projectconfig.py $android_l10n/mozilla-mobile/focus-android/l10n.toml --ref en-US --repo android_l10n --append --prefix mozilla-mobile/focus-android + nice -20 $install/app/scripts/tmx/tmx_projectconfig.py $android_l10n/firefox.toml --ref en-US --repo android_l10n --android + nice -20 $install/app/scripts/tmx/tmx_projectconfig.py $android_l10n/mozilla-mobile/focus-android/l10n.toml --ref en-US --repo android_l10n --android --append --prefix mozilla-mobile/focus-android } function updateMozOrg() { diff --git a/app/scripts/tmx/tmx_products.py b/app/scripts/tmx/tmx_products.py index 869e511a..9399b7b6 100755 --- a/app/scripts/tmx/tmx_products.py +++ b/app/scripts/tmx/tmx_products.py @@ -24,6 +24,8 @@ "Default settings will be used." ) root_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + storage_path = os.path.join(root_folder, "TMX") + os.makedirs(storage_path, exist_ok=True) else: config_parser = ConfigParser() config_parser.read(config_file) diff --git a/app/scripts/tmx/tmx_projectconfig.py b/app/scripts/tmx/tmx_projectconfig.py index a074cdef..87e2ae8e 100755 --- a/app/scripts/tmx/tmx_projectconfig.py +++ b/app/scripts/tmx/tmx_projectconfig.py @@ -1,12 +1,13 @@ #!/usr/bin/env python +from compare_locales.parser import getParser from configparser import ConfigParser +from moz.l10n.paths import L10nConfigPaths, get_android_locale import argparse import codecs import json import logging import os -import sys logging.basicConfig() # Get absolute path of ../../config from the current script location (not the @@ -24,22 +25,23 @@ "Default settings will be used." ) root_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + storage_path = os.path.join(root_folder, "TMX") + os.makedirs(storage_path, exist_ok=True) else: config_parser = ConfigParser() config_parser.read(config_file) storage_path = os.path.join(config_parser.get("config", "root"), "TMX") -try: - from compare_locales import paths - from compare_locales.parser import getParser -except ImportError as e: - print("FATAL: make sure that dependencies are installed") - print(e) - sys.exit(1) - class StringExtraction: - def __init__(self, toml_path, storage_path, reference_locale, repository_name): + def __init__( + self, + toml_path, + storage_path, + reference_locale, + repository_name, + android_project, + ): """Initialize object.""" # Set defaults @@ -52,6 +54,7 @@ def __init__(self, toml_path, storage_path, reference_locale, repository_name): self.storage_path = storage_path self.reference_locale = reference_locale self.repository_name = repository_name + self.android_project = android_project def setStorageAppendMode(self, prefix): """Set storage mode and prefix.""" @@ -80,12 +83,30 @@ def readExistingJSON(locale): def readFiles(locale): """Read files for locale""" - if locale == self.reference_locale: - files = paths.ProjectFiles(None, [project_config]) + if locale != self.reference_locale: + locale_files = [ + ( + os.path.abspath(ref_path), + os.path.abspath(tgt_path), + ) + for ( + ref_path, + raw_tgt_path, + ), locales in project_config_paths.all().items() + if locale in locales + and os.path.exists( + tgt_path := project_config_paths.format_target_path( + raw_tgt_path, locale + ) + ) + ] else: - files = paths.ProjectFiles(locale, [project_config]) + locale_files = [ + (os.path.abspath(ref_path), os.path.abspath(ref_path)) + for ref_path in project_config_paths.ref_paths + ] - for l10n_file, reference_file, _, _ in files: + for reference_file, l10n_file in locale_files: if not os.path.exists(l10n_file): # File not available in localization continue @@ -113,8 +134,12 @@ def readFiles(locale): ) basedir = os.path.dirname(self.toml_path) - project_config = paths.TOMLParser().parse(self.toml_path, env={"l10n_base": ""}) - basedir = os.path.join(basedir, project_config.root) + if self.android_project: + project_config_paths = L10nConfigPaths( + self.toml_path, locale_map={"android_locale": get_android_locale} + ) + else: + project_config_paths = L10nConfigPaths(self.toml_path) # Read strings for reference locale self.translations[self.reference_locale] = ( @@ -122,7 +147,9 @@ def readFiles(locale): ) readFiles(self.reference_locale) - for locale in project_config.all_locales: + locales = list(project_config_paths.all_locales) + locales.sort() + for locale in locales: # If storage mode is append, read existing translations (if available) self.translations[locale] = ( readExistingJSON(locale) if self.storage_append else {} @@ -212,6 +239,13 @@ def main(): action="store_true", help="If set to 'append', translations will be added to an existing cache file", ) + parser.add_argument( + "--android", + dest="android_project", + action="store_true", + help="If passed, the script will parse the config file using Android locale codes", + default=False, + ) parser.add_argument( "--prefix", dest="storage_prefix", @@ -235,6 +269,7 @@ def main(): storage_path, args.reference_code, args.repository_name, + args.android_project, ) if args.append_mode: extracted_strings.setStorageAppendMode(args.storage_prefix) diff --git a/requirements.txt b/requirements.txt index 5038f3b8..b46a7d6d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -compare-locales==9.0.* +compare-locales~=9.0.0 +moz-l10n~=0.6.1