Skip to content

Commit

Permalink
Bug 1048411 - Remove GAIA_INLINE_LOCALES optimization. r=stas
Browse files Browse the repository at this point in the history
  • Loading branch information
Zbigniew Braniecki committed Sep 17, 2014
1 parent 61d0c95 commit e1efcef
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 161 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ GAIA_LOCALES_PATH?=locales
LOCALES_FILE?=shared/resources/languages.json
GAIA_LOCALE_SRCDIRS=$(GAIA_DIR)$(SEP)shared $(GAIA_APPDIRS)
GAIA_DEFAULT_LOCALE?=en-US
GAIA_INLINE_LOCALES?=0
GAIA_PRETRANSLATE?=1
GAIA_CONCAT_LOCALES?=1

Expand Down Expand Up @@ -501,7 +500,6 @@ define BUILD_CONFIG
"DOGFOOD" : "$(DOGFOOD)", \
"OFFICIAL" : "$(MOZILLA_OFFICIAL)", \
"GAIA_DEFAULT_LOCALE" : "$(GAIA_DEFAULT_LOCALE)", \
"GAIA_INLINE_LOCALES" : "$(GAIA_INLINE_LOCALES)", \
"GAIA_PRETRANSLATE" : "$(GAIA_PRETRANSLATE)", \
"GAIA_CONCAT_LOCALES" : "$(GAIA_CONCAT_LOCALES)", \
"GAIA_ENGINE" : "xpcshell", \
Expand Down
54 changes: 9 additions & 45 deletions build/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,62 +108,26 @@
return '';
};

// return an array of all {{placeables}} found in a string
function getPlaceableNames(str) {
/* jshint boss:true */
var placeables = [];
var match;
while (match = L10n.rePlaceables.exec(str)) {
placeables.push(match[1]);
}
return placeables;
}

// put all dependencies required for string interpolation in the AST
// XXX only first-level deps are supported for now to avoid having to check
// for cyclic and recursive references
function getPlaceables(ast, val) {
var placeables = getPlaceableNames(val);
for (var i = 0; i < placeables.length; i++) {
var id = placeables[i];
ast[id] = this.ctx.getEntitySource(id);
}
}

navigator.mozL10n.translateDocument = L10n.translateDocument;

navigator.mozL10n.getDictionary = function getDictionary(fragment) {
navigator.mozL10n.getDictionary = function getDictionary() {
// don't do anything for pseudolocales
if (this.ctx.supportedLocales[0] in this.qps) {
return null;
}

var ast = {};

if (!fragment) {
// en-US is the de facto source locale of Gaia
var sourceLocale = this.ctx.getLocale('en-US');
if (!sourceLocale.isReady) {
sourceLocale.build(null);
}
// iterate over all strings in en-US
for (var id in sourceLocale.ast) {
ast[id] = this.ctx.getEntitySource(id);
}
flushBuildMessages.call(this, 'compared to en-US');
return ast;
// en-US is the de facto source locale of Gaia
var sourceLocale = this.ctx.getLocale('en-US');
if (!sourceLocale.isReady) {
sourceLocale.build(null);
}

var elements = L10n.getTranslatableChildren(fragment);

for (var i = 0; i < elements.length; i++) {
var attrs = this.getAttributes(elements[i]);
var val = this.ctx.getEntitySource(attrs.id);
ast[attrs.id] = val;
L10n.walkContent(val, getPlaceables.bind(this, ast));
// iterate over all strings in en-US
for (var id in sourceLocale.ast) {
ast[id] = this.ctx.getEntitySource(id);
}
flushBuildMessages.call(this, 'in the visible DOM');

flushBuildMessages.call(this, 'compared to en-US');
return ast;
};

Expand Down
12 changes: 6 additions & 6 deletions build/test/integration/multilocale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ suite('multilocale Integration tests', function() {
rmrf('profile');
});

function makeHelper(localesFilePath, localesDir, inlineAndConcat, done) {
function makeHelper(localesFilePath, localesDir, concat, done) {
var settingsZipPath = path.join(process.cwd(), 'profile', 'webapps',
'settings.gaiamobile.org', 'application.zip');
var cnPathInZip = 'locales-obj/zh-CN.json';
Expand All @@ -25,8 +25,8 @@ suite('multilocale Integration tests', function() {
' LOCALE_BASEDIR=' + localesDir +
' make';

if (!inlineAndConcat) {
command = 'GAIA_INLINE_LOCALES=0 GAIA_CONCAT_LOCALES=0 ' + command;
if (!concat) {
command = 'GAIA_CONCAT_LOCALES=0 ' + command;
}

// We were failing because the output from the gaia build process was
Expand All @@ -36,7 +36,7 @@ suite('multilocale Integration tests', function() {

helper.checkError(error, stdout, stderr);
var zip = new AdmZip(settingsZipPath);
if (inlineAndConcat) {
if (concat) {
assert.isNotNull(zip.getEntry(cnPathInZip),
'concat file ' + cnPathInZip + ' should exist');
} else {
Expand Down Expand Up @@ -66,13 +66,13 @@ suite('multilocale Integration tests', function() {
makeHelper(localesFilePath, absoluteLocalesDir, true, done);
});

test('make with relative l10n path but without inline & concat',
test('make with relative l10n path but without concat',
function(done) {
var localesFilePath = path.join(localesDir, 'languages.json');
makeHelper(localesFilePath, localesDir, false, done);
});

test('make with absolute l10n path but without inline & concat',
test('make with absolute l10n path but without concat',
function(done) {
var localesFilePath= path.join(process.cwd(), localesDir, 'languages.json');
var absoluteLocalesDir = path.join(process.cwd(), localesDir);
Expand Down
23 changes: 0 additions & 23 deletions build/test/unit/webapp-optimize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ suite('webapp-optimize.js', function() {
mockConfig = {
GAIA_DEFAULT_LOCALE: 'default-locale',
GAIA_CONCAT_LOCALES: '1',
GAIA_INLINE_LOCALES: '1',
GAIA_OPTIMIZE: '1',
stageDir: 'testStageDir',
DEBUG: '0',
Expand Down Expand Up @@ -441,28 +440,6 @@ suite('webapp-optimize.js', function() {
'should modify its innerHTML');
});

test('embed10nResources', function() {
htmlOptimizer.subDict = {
'test-lang': {
'testkey': 'testContent'
}
};
var originalHasAttributeFlag = hasAttributeFlag;
hasAttributeFlag = false;
htmlOptimizer.embedSubsetL10nResources();
hasAttributeFlag = originalHasAttributeFlag;

assert.equal(createdDOMs[3].query, 'document/script',
'should modify document/script');
assert.equal(createdDOMs[3].innerHTML,
'\n ' + JSON.stringify(htmlOptimizer.subDict['test-lang']) + '\n',
'should embed stringify l10ned object');
assert.equal(createdDOMs[3].lang, 'test-lang',
'should modify script.lang');
assert.equal(createdDOMs[3].type, 'application/l10n',
'should have application/l10n type');
});

test('concatL10nResources', function() {
hasAttributeFlag = false;
htmlOptimizer.concatL10nResources();
Expand Down
1 change: 0 additions & 1 deletion build/test/unit/webapp-shared.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ suite('webapp-shared.js', function() {
'push unstable_styles');

var locales = 'testlocales';
webappShared.config.GAIA_INLINE_LOCALES = 'zh-TW';
webappShared.pushFileByType('locales', locales + '.obj');
assert.equal(webappShared.used.locales[0], locales,
'push locales');
Expand Down
49 changes: 7 additions & 42 deletions build/webapp-optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* webpp-optimize will do below things.
* 1. Inline embeded html from <link rel="import" href="test.html" name="name">
* into html and commented (<!--CONTENT-->).
* 2. Embed l10n resource in script tag to html.
* 3. Concat l10n resource to json files and put them as link and attach to
* 2. Concat l10n resource to json files and put them as link and attach to
* html.
* 4. Aggregate and uglify all JS files used in html to one JS file.
* 5. Optimize inline JS/CSS content.
* 3. Aggregate and uglify all JS files used in html to one JS file.
* 4. Optimize inline JS/CSS content.
*/

var utils = require('./utils');
Expand All @@ -22,7 +21,6 @@ var HTMLOptimizer = function(options) {
this.webapp = options.webapp;
/**
* Optimization helpers -- these environment variables are used:
* - config.GAIA_INLINE_LOCALES - embed the minimum l10n data in HTML files
* - config.GAIA_PRETRANSLATE - pretranslate html into default locale
* - config.GAIA_CONCAT_LOCALES - aggregates l10n files
* - config.GAIA_OPTIMIZE - aggregates JS files
Expand All @@ -35,18 +33,13 @@ var HTMLOptimizer = function(options) {
// When file has done optimized, we call done.
this.done = options.callback;
/**
* For each HTML file, we retrieve two multi-locale dictionaries:
*
* - subDict (used with config.GAIA_INLINE_LOCALES)
* = minimal set of strings required to translate all HTML elements that
* use data-l10n-id attributes; it gets embedded in the HTML document.
* For each HTML file, we retrieve multi-locale dictionary:
*
* - fullDict (used with config.GAIA_CONCAT_LOCALES)
* = full set of all l10n strings that are loaded by the HTML document,
* including subDict and all strings that are used dynamically from JS;
* including all strings that are used dynamically from JS;
* it gets merged into webapp.dictionary.
*/
this.subDict = utils.cloneJSON(this.webapp.dictionary);
this.fullDict = utils.cloneJSON(this.webapp.dictionary);
this.getDictionary = null;

Expand Down Expand Up @@ -84,10 +77,6 @@ HTMLOptimizer.prototype.process = function() {
HTMLOptimizer.prototype._optimize = function() {
this._proceedLocales();

if (this.config.GAIA_INLINE_LOCALES === '1') {
this.embedSubsetL10nResources();
}

if (this.config.GAIA_CONCAT_LOCALES === '1') {
this.concatL10nResources();
}
Expand Down Expand Up @@ -129,16 +118,13 @@ HTMLOptimizer.prototype._optimize = function() {
// create JSON dicts for the current language; one for the <script> tag
// embedded in HTML and one for locales-obj/
HTMLOptimizer.prototype._proceedLocales = function() {
var docElt = this.win.document.documentElement;
var mozL10n = this.win.navigator.mozL10n;
var processedLocales = 0;
while (processedLocales < this.locales.length) {
// change the language of the localization context
mozL10n.ctx.requestLocales(this.locales[processedLocales]);

// create JSON dicts for the current language; one for the <script> tag
// embedded in HTML and one for locales-obj/
this.subDict[mozL10n.language.code] = this.getDictionary(docElt);
// create JSON dicts for the current language for locales-obj/
this.fullDict[mozL10n.language.code] = this.getDictionary();
processedLocales++;
}
Expand Down Expand Up @@ -242,26 +228,6 @@ HTMLOptimizer.prototype.embededGlobals = function() {
doc.documentElement.appendChild(script);
};

/**
* Creates a dictionary for all l10n entities that are required by the HTML
* document, and include it as an inline JSON.
*/
HTMLOptimizer.prototype.embedSubsetL10nResources = function() {
var doc = this.win.document;
var noFetchRes =
doc.querySelector('link[type="application/l10n"][data-no-fetch]');

// if the document has at least one data-no-fetch link, we will
// embed the whole l10n dictionary, no need to embed the partial one.
//
// hasAttribute is needed because tests always return value for querySelector
if (noFetchRes && noFetchRes.hasAttribute('data-no-fetch')) {
return;
}
embedL10nResources(doc.documentElement, this.subDict);
};


/**
* Replaces all external l10n resource nodes by a single link:
* <link rel="localization" href="/locales-obj/{locale}.json" />,
Expand Down Expand Up @@ -789,8 +755,7 @@ WebappOptimize.prototype.execute = function(config) {
function execute(config) {
var gaia = utils.gaia.getInstance(config);
var locales;
if (config.GAIA_INLINE_LOCALES === '1' ||
config.GAIA_CONCAT_LOCALES === '1') {
if (config.GAIA_CONCAT_LOCALES === '1') {
locales = getLocales(config);
} else {
locales = [config.GAIA_DEFAULT_LOCALE];
Expand Down
2 changes: 1 addition & 1 deletion locales/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You can build a multilocale Gaia profile with the following `make` command:
LOCALES_FILE=locales/languages_basecamp.json \
GAIA_DEFAULT_LOCALE=pt-BR

Use `GAIA_INLINE_LOCALES=1` to precompile all HTML to include text content in
Use `GAIA_PRETRANSLATE=1` to precompile all HTML to include text content in
the deafult locale.

The full documentation on building multilocale Gaia and B2G is avaiable on MDN: https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Building#Building_multilocale
Expand Down
40 changes: 0 additions & 40 deletions shared/js/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,15 +1146,6 @@
}
};

Locale.prototype.getEntity = function(id, ctxdata) {
var entry = this.getEntry(id);

if (!entry) {
return null;
}
return entry.valueOf(ctxdata);
};



function Context(id) {
Expand Down Expand Up @@ -1408,11 +1399,9 @@
Entity: Entity,
getPluralRule: getPluralRule,
rePlaceables: rePlaceables,
getTranslatableChildren: getTranslatableChildren,
translateDocument: translateDocument,
onManifestInjected: onManifestInjected,
onMetaInjected: onMetaInjected,
fireLocalizedEvent: fireLocalizedEvent,
PropertiesParser: PropertiesParser,
compile: compile,
walkContent: walkContent
Expand Down Expand Up @@ -1471,7 +1460,6 @@

function init(pretranslate) {
if (pretranslate) {
inlineLocalization.call(navigator.mozL10n);
initResources.call(navigator.mozL10n);
} else {
// if pretranslate is false, we want to initialize MO
Expand All @@ -1483,34 +1471,6 @@
}
}

function inlineLocalization() {
var locale = this.ctx.getLocale(navigator.language);
var scriptLoc = locale.isPseudo ? this.ctx.defaultLocale : locale.id;
var script = document.documentElement
.querySelector('script[type="application/l10n"]' +
'[lang="' + scriptLoc + '"]');
if (!script) {
return;
}

// the inline localization is happenning very early, when the ctx is not
// yet ready and when the resources haven't been downloaded yet; add the
// inlined JSON directly to the current locale
locale.addAST(JSON.parse(script.innerHTML));
// localize the visible DOM
var l10n = {
ctx: locale,
language: {
code: locale.id,
direction: getDirection(locale.id)
}
};
translateDocument.call(l10n);

// the visible DOM is now pretranslated
isPretranslated = true;
}

function initResources() {
/* jshint boss:true */
var manifestFound = false;
Expand Down
1 change: 0 additions & 1 deletion tools/extensions/[email protected]/content/gaia.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var Gaia = {
config.DOGFOOD = '0';
config.OFFICIAL = '';
config.GAIA_DEFAULT_LOCALE = 'en-US';
config.GAIA_INLINE_LOCALES = '1';
config.GAIA_PRETRANSLATE = '1';
config.GAIA_CONCAT_LOCALES = '1';
config.GAIA_ENGINE = 'xpcshell';
Expand Down

0 comments on commit e1efcef

Please sign in to comment.