Permalink
Browse files

template plugin and build-i18n no longer hard-code template and i18n …

…dirs.
  • Loading branch information...
1 parent 6debbd7 commit 311384f23aa199c8b197a2931eb45071e3c04972 @toolness committed Jan 16, 2013
Showing with 35 additions and 21 deletions.
  1. +9 −4 bin/build-i18n.js
  2. +6 −0 js/require-config.js
  3. +20 −17 js/require-plugins/template.js
View
@@ -6,7 +6,8 @@ var fs = require('fs');
var resolve = require('path').resolve;
var buildRequire = require('./build-require');
var rootDir = buildRequire.rootDir;
-var templateDir = resolve(rootDir, '..', 'templates');
+var templateDir;
+var templateI18n;
var requirejs = require('requirejs');
var bundles = exports.bundles = {};
var config = buildRequire.generateConfig();
@@ -49,11 +50,15 @@ findNlsPaths(rootDir).forEach(function(path) {
});
// Parse all inline l10n strings out of all templates and fill them into
-// the fc/nls/ui bundle.
+// the template i18n bundle.
+templateDir = requirejs.toUrl(config.config.template.htmlPath)
+ .replace(".js", "");
+templateI18n = config.config.template.i18nPath;
+
fs.readdirSync(templateDir).forEach(function(filename) {
var InlineL10n = requirejs('inline-l10n');
- var root = bundles['fc/nls/ui'].root;
- var metadata = bundles['fc/nls/ui'].metadata;
+ var root = bundles[templateI18n].root;
+ var metadata = bundles[templateI18n].metadata;
var content = fs.readFileSync(templateDir + '/' + filename, 'utf8');
var defaultValues = InlineL10n.parse(content);
for (var key in defaultValues) {
View
@@ -71,6 +71,12 @@ var require = {
test: "../test",
templates: "../templates"
},
+ config: {
+ template: {
+ htmlPath: "templates",
+ i18nPath: "fc/nls/ui"
+ }
+ },
githubUrl: "https://github.com/mozilla/friendlycode"
};
@@ -3,35 +3,38 @@ define([
"module",
"text",
"underscore",
- "inline-l10n",
- "i18n!fc/nls/ui"
-], function (module, text, _, InlineL10n, i18nBundle) {
+ "inline-l10n"
+], function (module, text, _, InlineL10n) {
var buildMap = {},
- masterConfig = module.config();
+ masterConfig = module.config(),
+ i18nModuleName = "i18n!" + masterConfig.i18nPath;
+ function templatePath(require, name) {
+ return require.toUrl(masterConfig.htmlPath + "/" + name)
+ .replace(".js", ".html");
+ }
+
return {
load: function(name, req, onLoad, config) {
- var url = req.toUrl("templates/" + name).replace(".js", ".html");
-
- text.get(url, function (data) {
- var template;
+ text.get(templatePath(req, name), function (data) {
if (config.isBuild) {
- template = buildMap[name] = "_.template(InlineL10n(" +
- JSON.stringify(data) + ", i18nBundle))";
- } else {
- template = _.template(InlineL10n(data, i18nBundle));
+ buildMap[name] = JSON.stringify(data);
+ return onLoad();
}
-
- onLoad(template);
+ req([i18nModuleName], function(i18nBundle) {
+ onLoad(_.template(InlineL10n(data, i18nBundle)));
+ });
});
},
write: function (pluginName, moduleName, write) {
if (buildMap[moduleName]) {
var content = buildMap[moduleName];
write.asModule(pluginName + "!" + moduleName,
- "define(['underscore', 'inline-l10n', 'i18n!fc/nls/ui'], " +
- "function (_, InlineL10n, i18nBundle) { \n return " + content +
- ";});\n");
+ "define(['underscore', 'inline-l10n', " +
+ "'" + i18nModuleName + "'], " +
+ "function (_, InlineL10n, i18nBundle) { \n return " +
+ "_.template(InlineL10n(" + content + ", i18nBundle));});\n"
+ );
}
}
};

0 comments on commit 311384f

Please sign in to comment.