From f6843220dc0e15833523b7584a39b3f1745093ed Mon Sep 17 00:00:00 2001 From: Ed Lee Date: Tue, 12 Jan 2016 08:34:51 -0800 Subject: [PATCH] Bug 1237662 - Set up a script to automatically import from the loop-client-l10n directory. r=Standard8 --- Makefile | 26 +++++-- add-on/chrome/modules/MozLoopService.jsm | 20 +++-- add-on/jar.mn | 1 + bin/locale_update.py | 93 ++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 13 deletions(-) create mode 100755 bin/locale_update.py diff --git a/Makefile b/Makefile index 11e1fb82..5f83a0e4 100644 --- a/Makefile +++ b/Makefile @@ -185,6 +185,15 @@ $(BUILT)/standalone/content/js/%.js: standalone/content/js/%.jsx @mkdir -p $(@D) $(BABEL) $< --out-file $@ +standalone_l10n_files=$(wildcard locale/*/standalone.properties) +built_standalone_l10n_files=$(patsubst locale/%/standalone.properties, \ + $(BUILT)/standalone/content/l10n/%/loop.properties, \ + $(standalone_l10n_files)) + +$(BUILT)/standalone/content/l10n/%/loop.properties: locale/%/standalone.properties locale/%/shared.properties + @mkdir -p $(@D) + cat $^ > $@ + # add-on add_on_jsx_files=$(wildcard add-on/panels/js/*.jsx) built_add_on_js_files=$(patsubst add-on/panels/js/%.jsx, \ @@ -195,6 +204,15 @@ $(BUILT)/add-on/chrome/content/panels/js/%.js: add-on/panels/js/%.jsx @mkdir -p $(@D) $(BABEL) $< --out-file $@ +add_on_l10n_files=$(wildcard locale/*/add-on.properties) +built_add_on_l10n_files=$(patsubst locale/%/add-on.properties, \ + $(BUILT)/add-on/chrome/locale/%/loop.properties, \ + $(add_on_l10n_files)) + +$(BUILT)/add-on/chrome/locale/%/loop.properties: locale/%/add-on.properties locale/%/shared.properties + @mkdir -p $(@D) + cat $^ > $@ + # XXX maybe just build one copy of shared in standalone, and then use # server.js magic to redirect? # XXX ecma3 transform for IE? @@ -206,16 +224,14 @@ ui: node_modules $(built_ui_js_files) $(built_ui_shared_js_files) $(RSYNC) shared $(BUILT)/$@ .PHONY: standalone -standalone: node_modules $(built_standalone_js_files) $(built_standalone_shared_js_files) +standalone: node_modules $(built_standalone_js_files) $(built_standalone_shared_js_files) $(built_standalone_l10n_files) mkdir -p $(BUILT)/$@ $(RSYNC) $@ $(BUILT) mkdir -p $(BUILT)/$@/content/shared $(RSYNC) shared $(BUILT)/$@/content - mkdir -p $(BUILT)/$@/content/l10n/en-US - cat locale/en-US/$@.properties locale/en-US/shared.properties > $(BUILT)/$@/content/l10n/en-US/loop.properties .PHONY: add-on -add-on: node_modules $(built_add_on_js_files) $(built_add_on_shared_js_files) $(BUILT)/$(ADD-ON)/chrome.manifest $(BUILT)/add-on/chrome/content/preferences/prefs.js +add-on: node_modules $(built_add_on_js_files) $(built_add_on_shared_js_files) $(built_add_on_l10n_files) $(BUILT)/$(ADD-ON)/chrome.manifest $(BUILT)/add-on/chrome/content/preferences/prefs.js mkdir -p $(BUILT)/$@ $(RSYNC) $@/chrome/bootstrap.js $(BUILT)/$@ sed "s/@FIREFOX_VERSION@/$(FIREFOX_VERSION)/g" add-on/install.rdf.in | \ @@ -229,8 +245,6 @@ add-on: node_modules $(built_add_on_js_files) $(built_add_on_shared_js_files) $( mkdir -p $(BUILT)/$@/chrome/content/shared $(RSYNC) shared $(BUILT)/$@/chrome/content $(RSYNC) $@/chrome/skin $(BUILT)/$@/chrome/ - mkdir -p $(BUILT)/$@/chrome/locale/en-US - cat locale/en-US/$@.properties locale/en-US/shared.properties > $(BUILT)/$@/chrome/locale/en-US/loop.properties $(BUILT)/$(ADD-ON)/chrome.manifest: $(ADD-ON)/jar.mn mkdir -p $(BUILT)/$(ADD-ON) diff --git a/add-on/chrome/modules/MozLoopService.jsm b/add-on/chrome/modules/MozLoopService.jsm index 7a7dda69..a82e9e93 100644 --- a/add-on/chrome/modules/MozLoopService.jsm +++ b/add-on/chrome/modules/MozLoopService.jsm @@ -770,14 +770,20 @@ var MozLoopServiceInternal = { return gLocalizedStrings; } - let stringBundle = - Services.strings.createBundle("chrome://loop/locale/loop.properties"); - - let enumerator = stringBundle.getSimpleEnumeration(); - while (enumerator.hasMoreElements()) { - let string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement); - gLocalizedStrings.set(string.key, string.value); + // Load all strings from a bundle location preferring strings loaded later. + function loadAllStrings(location) { + let bundle = Services.strings.createBundle(location); + let enumerator = bundle.getSimpleEnumeration(); + while (enumerator.hasMoreElements()) { + let string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement); + gLocalizedStrings.set(string.key, string.value); + } } + + // Load fallback/en-US strings then prefer the localized ones if available. + loadAllStrings("chrome://loop-locale-fallback/content/loop.properties"); + loadAllStrings("chrome://loop/locale/loop.properties"); + // Supply the strings from the branding bundle on a per-need basis. let brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties"); diff --git a/add-on/jar.mn b/add-on/jar.mn index 64516db6..e979fcf2 100644 --- a/add-on/jar.mn +++ b/add-on/jar.mn @@ -4,6 +4,7 @@ [features/loop@mozilla.org] chrome.jar: % content loop %content/ contentaccessible=yes +% content loop-locale-fallback %locale/en-US/ % locale loop en-US %locale/en-US/ % skin loop classic/1.0 %skin/linux/ os=Linux % skin loop classic/1.0 %skin/osx/ os=Darwin diff --git a/bin/locale_update.py b/bin/locale_update.py new file mode 100755 index 00000000..c25f8db1 --- /dev/null +++ b/bin/locale_update.py @@ -0,0 +1,93 @@ +#!/usr/bin/python + +## +# This script is designed to update the l10n files and locale registration for +# the standalone loop client and loop add-on. The source of the localization +# files is https://github.com/mozilla/loop-client-l10n repository. +# The loop repo is assumed to be https://github.com/mozilla/loop. +# +# Run this script from the local version of loop. It assumes that a local +# version of loop-client-l10n is in a parallel directory: ../loop-client-l10n. +## + +from __future__ import print_function + +import argparse +import io +import os +import re +import shutil + +# defaults +DEF_L10N_SRC = os.path.join(os.pardir, "loop-client-l10n", "l10n") +DEF_L10N_DST = os.path.join("locale") +DEF_INDEX_FILE_NAME = os.path.join("standalone", "content", os.extsep.join(["index", "html"])) +DEF_JAR_FILE_NAME = os.path.join("add-on", os.extsep.join(["jar", "mn"])) + + +def main(l10n_src, l10n_dst, index_file_name, jar_file_name): + print("deleting existing l10n content tree:", l10n_dst) + shutil.rmtree(l10n_dst, ignore_errors=True) + + print("updating l10n tree from", l10n_src) + + def create_locale(src_dir): + # Convert loop-client-l10n repo names to loop repo names. + dst_dir = src_dir.replace('_', '-').replace('templates', 'en-US') + + shutil.copytree(os.path.join(l10n_src, src_dir), os.path.join(l10n_dst, dst_dir)) + return dst_dir + + locale_dirs = os.listdir(l10n_src) + locale_list = sorted([create_locale(x) for x in locale_dirs if x[0] != "."]) + + print("updating locales list in", index_file_name) + with io.open(index_file_name, "r+") as index_file: + index_html = index_file.read() + + # Replace the one meta line with new locales. + new_content = re.sub( + '', + '', + index_html, 1, re.M | re.S) + + index_file.seek(0) + index_file.truncate(0) + index_file.write(new_content) + + print("updating locales list in", jar_file_name) + with io.open(jar_file_name, "r+") as jar_file: + jar_mn = jar_file.read() + + # Replace multiple locale registrations with new locales. + jar_locales = ['% locale loop {0} %locale/{0}/'.format(x) for x in locale_list] + new_content = re.sub( + '(% locale loop .+\n)+', + '\n'.join(jar_locales) + '\n', + jar_mn) + + jar_file.seek(0) + jar_file.truncate(0) + jar_file.write(new_content) + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Loop Stand-alone Client localization update script") + parser.add_argument('--src', + default=DEF_L10N_SRC, + metavar="path", + help="Source path for l10n resources. Default = " + DEF_L10N_SRC) + parser.add_argument('--dst', + default=DEF_L10N_DST, + metavar="path", + help="Destination path for l10n resources. Default = " + DEF_L10N_DST) + parser.add_argument('--index-file', + default=DEF_INDEX_FILE_NAME, + metavar="name", + help="File to be updated with the locales list. Default = " + DEF_INDEX_FILE_NAME) + parser.add_argument('--jar-file', + default=DEF_JAR_FILE_NAME, + metavar="name", + help="Jar file to be updated with the locales list. Default = " + DEF_JAR_FILE_NAME) + args = parser.parse_args() + main(args.src, args.dst, args.index_file, args.jar_file)