-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
expand.js
58 lines (48 loc) · 1.68 KB
/
expand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* expand - expands templates with internationalized strings. The resulting HTML files are written to disk.
*
* This utility builds the Japanese and English versions of the HTML files. The templates are written using Swig,
* and roughly follows the methodology described at http://ejohn.org/blog/a-strategy-for-i18n-and-node/
*
* English files are placed in the root: public/
* Japanese files are placed under the language code: public/jp
* ...and so on for other languages, if ever...
*
* don't really care about making the code nice at the moment
* ideally, this would be a grunt task run at build time
*/
"use strict";
var fs = require("fs");
var path = require("path");
var swig = require("swig");
var mkdirp = require("mkdirp");
var dictionary = require("./public/templates/il8n.json");
var templateDir = "public/templates";
var templates = [
"index.html",
"about.html"
];
var languages = [
{code: "en", target: "public"},
{code: "ja", target: "public/jp"} // *lang* code for Japanese is JA not JP. Too late now. Site already public.
];
function newContext(languageCode) {
return {
__: function(s) {
var entry = dictionary[s];
if (!entry) {
console.error("unknown il8n key: " + s);
}
return entry && entry[languageCode] || s;
}
};
}
templates.forEach(function(file) {
var template = swig.compileFile(path.join(templateDir, file));
languages.forEach(function(language) {
var context = newContext(language.code);
var result = template(context);
mkdirp.sync(language.target);
fs.writeFileSync(path.join(language.target, file), result);
});
});