Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/scripts/glossaire.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/tmx/tmx_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
69 changes: 52 additions & 17 deletions app/scripts/tmx/tmx_projectconfig.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -113,16 +134,22 @@ 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] = (
readExistingJSON(self.reference_locale) if self.storage_append else {}
)
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 {}
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
compare-locales==9.0.*
compare-locales~=9.0.0
moz-l10n~=0.6.1