Permalink
Browse files

refactorings to make l10n infrastructure easier to understand.

  • Loading branch information...
1 parent aa6ffad commit a43b0fb21aed5f9b7cd04587c7aaea651aaed813 @toolness committed Jan 15, 2013
Showing with 52 additions and 46 deletions.
  1. +39 −6 build-i18n.js
  2. +13 −40 js/fc/nls/ui.js
View
@@ -2,21 +2,33 @@
// module names, and only export their localizations.
var MODULE_FILTER = process.argv.slice(2);
-var NLS_PATHS = [
- 'fc/nls',
- 'slowparse-errors/nls'
-];
-
var fs = require('fs');
var buildRequire = require('./build-require');
var requirejs = require('requirejs');
var bundles = {};
var config = buildRequire.generateConfig();
+function findNlsPaths(root, subdir) {
+ var nlsPaths = [];
+ subdir = subdir || '';
+ fs.readdirSync(root + subdir).forEach(function(filename) {
+ var relpath = subdir + '/' + filename;
+ var stat = fs.statSync(root + relpath);
+ if (stat.isDirectory()) {
+ if (filename == 'nls') {
+ nlsPaths.push(relpath.slice(1));
+ } else
+ nlsPaths = nlsPaths.concat(findNlsPaths(root, relpath));
+ }
+ });
+
+ return nlsPaths;
+};
+
config.isBuild = true;
requirejs.config(config);
-NLS_PATHS.forEach(function(path) {
+findNlsPaths(__dirname + '/js').forEach(function(path) {
fs.readdirSync(__dirname + '/js/' + path).forEach(function(filename) {
var match = filename.match(/^(.*)\.js$/);
if (match) {
@@ -33,4 +45,25 @@ NLS_PATHS.forEach(function(path) {
});
});
+// Parse all inline l10n strings out of all templates and fill them into
+// the fc/nls/ui bundle.
+fs.readdirSync('templates').forEach(function(filename) {
+ var InlineL10n = requirejs('inline-l10n');
+ var root = bundles['fc/nls/ui'].root;
+ var metadata = bundles['fc/nls/ui'].metadata;
+ var content = fs.readFileSync('templates/' + filename, 'utf8');
+ var defaultValues = InlineL10n.parse(content);
+ for (var key in defaultValues) {
+ var value = defaultValues[key];
+ if (key in root && root[key] != value)
+ throw new Error("conflicting definitions for key: " + key);
+ root[key] = value;
+ metadata[key] = {
+ help: 'This string appears in ' +
+ '<a href="https://github.com/mozilla/friendlycode/blob/gh-pages/templates/' + filename + '">' +
+ filename + '</a>.'
+ };
+ }
+});
+
require('sys').puts(JSON.stringify(bundles, null, 2));
View
@@ -1,46 +1,19 @@
"use strict";
-define([
- "inline-l10n"
-], function(InlineL10n) {
- var root = {
- "page-load-err": 'Sorry, an error occurred while trying to get the page.',
- "publish-err": 'Sorry, an error occurred while trying to publish.',
- "facebook-locale": "en_US",
- "default-tweet": "Check out the #MozThimble page I just made:",
- "tweet": "Tweet"
- };
-
- var metadata = {
- "facebook-locale": {
- help: "Locale passed to Facebook for social media actions."
- }
- };
-
- if (typeof(document) == "undefined") {
- // We're running in node. Parse all inline l10n strings out
- // of all templates.
- var fs = require('fs');
- fs.readdirSync('templates').forEach(function(filename) {
- var content = fs.readFileSync('templates/' + filename, 'utf8');
- var defaultValues = InlineL10n.parse(content);
- for (var key in defaultValues) {
- var value = defaultValues[key];
- if (key in root && root[key] != value)
- throw new Error("conflicting definitions for key: " + key);
- root[key] = value;
- metadata[key] = {
- help: 'This string appears in ' +
- '<a href="https://github.com/mozilla/friendlycode/blob/gh-pages/templates/' + filename + '">' +
- filename + '</a>.'
- };
- }
- });
- }
-
+define(function() {
return {
description: "Strings for the user interface.",
- root: root,
- metadata: metadata
+ root: {
+ "page-load-err": 'Sorry, an error occurred while trying to get the page.',
+ "publish-err": 'Sorry, an error occurred while trying to publish.',
+ "facebook-locale": "en_US",
+ "default-tweet": "Check out the #MozThimble page I just made:",
+ "tweet": "Tweet"
+ },
+ metadata: {
+ "facebook-locale": {
+ help: "Locale passed to Facebook for social media actions."
+ }
+ }
};
});

0 comments on commit a43b0fb

Please sign in to comment.