This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
l10n-generate-pages.js
110 lines (88 loc) · 3.44 KB
/
l10n-generate-pages.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// grunt task to create a copy of each static page for each locale.
// Any `{{ locale }}` tags will be replaced with the locale. This is
// used to create a per-locale template with locale specific resources.
module.exports = function (grunt) {
'use strict';
var path = require('path');
var Handlebars = require('handlebars');
var Promise = require('bluebird');
var legalTemplates = require('../server/lib/legal-templates');
var defaultLang;
var templateSrc;
var templateDest;
var templates = {
'db-LB': {}
};
// Make the 'gettext' function available in the templates.
Handlebars.registerHelper('t', function (string) {
if (string.fn) {
return this.l10n.format(this.l10n.gettext(string.fn(this)), this);
} else {
return this.l10n.format(this.l10n.gettext(string), this);
}
return string;
});
grunt.registerTask('l10n-generate-pages',
'Generate localized versions of the static pages', function () {
var done = this.async();
var i18n = require('../server/lib/i18n')(grunt.config.get('server.i18n'));
// server config is set in the selectconfig task
var supportedLanguages = grunt.config.get('server.i18n.supportedLanguages');
defaultLang = grunt.config.get('server.i18n.defaultLang');
templateSrc = grunt.config.get('yeoman.page_template_src');
templateDest = grunt.config.get('yeoman.page_template_dist');
var getTemplate = legalTemplates(i18n, templateDest);
// Create a cache of the templates so we can reference them synchronously later
Promise.settle(supportedLanguages.map(function (lang) {
return Promise.all([
getTemplate('terms', lang, defaultLang),
getTemplate('privacy', lang, defaultLang)
])
.then(function (temps) {
templates[lang] = {
terms: temps[0],
privacy: temps[1]
};
});
})).then(function () {
supportedLanguages.forEach(function (lang) {
generatePagesForLanguage(i18n, lang);
});
done();
}).then(null, done);
});
function generatePagesForLanguage(i18n, language) {
// items on disk are stored by locale, not language.
var locale = i18n.localeFrom(language);
var destRoot = path.join(templateDest, locale);
var context = i18n.localizationContext(language);
grunt.file.recurse(templateSrc,
function (srcPath, rootDir, subDir, fileName) {
var destPath = path.join(destRoot, (subDir || ''), fileName);
generatePage(srcPath, destPath, context);
});
}
function generatePage(srcPath, destPath, context) {
grunt.log.writeln('generating `%s`', destPath);
grunt.file.copy(srcPath, destPath, {
process: function (contents, path) {
var terms = templates[context.lang].terms || templates[defaultLang].terms;
var privacy = templates[context.lang].privacy || templates[defaultLang].privacy;
var template = Handlebars.compile(contents);
var out = template({
l10n: context,
locale: context.locale,
lang: context.lang,
lang_dir: context.lang_dir,
fontSupportDisabled: context.fontSupportDisabled,
terms: terms,
privacy: privacy
});
return out;
}
});
}
};