This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
fix(legal): fix rendering of legal pages when loaded directly via url #1376
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,34 @@ | |
* 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. | ||
// Three steps are performed to create the pages: | ||
// 1. po files for each locale are converted to JSON | ||
// 2. Terms/Privacy markdown documents are converted to HTML | ||
// 3. Templates are compiled using the JSON strings and legal doc translations, and with URLs for locale | ||
// specific resources. | ||
// | ||
// They compiled templates are placed in the server's compiled template directory to await further processing | ||
// (requirejs, minification, revving). | ||
|
||
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; | ||
|
||
// Legal templates for each locale, key'ed by languages, e.g. | ||
// templates['en-US'] = { terms: ..., privacy: ... } | ||
var templates = { | ||
// The debug language does not have template files, so use an empty object | ||
'db-LB': {} | ||
}; | ||
|
||
// Make the 'gettext' function available in the templates. | ||
Handlebars.registerHelper('t', function (string) { | ||
if (string.fn) { | ||
|
@@ -25,20 +41,47 @@ module.exports = function (grunt) { | |
return string; | ||
}); | ||
|
||
grunt.registerTask('l10n-generate-pages', | ||
grunt.registerTask('l10n-generate-pages', ['l10n-create-json', 'l10n-generate-tos-pp', 'l10n-compile-templates']); | ||
|
||
|
||
grunt.registerTask('l10n-compile-templates', | ||
'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'); | ||
|
||
supportedLanguages.forEach(function (lang) { | ||
generatePagesForLanguage(i18n, lang); | ||
}); | ||
// Legal templates have already been generated and placed in the template destination directory. | ||
var getTemplate = legalTemplates(i18n, templateDest); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a note here about why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the legal templates are sourced from the legal repo, so we convert them markdown to HTML and put it in the template destination directory, at which point they are finished with processing. We're using them here to help process the other page templates. I'll add a note! |
||
|
||
// 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); | ||
|
||
}); | ||
|
||
|
||
|
@@ -61,13 +104,17 @@ module.exports = function (grunt) { | |
|
||
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 | ||
fontSupportDisabled: context.fontSupportDisabled, | ||
terms: terms, | ||
privacy: privacy | ||
}); | ||
return out; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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/. */ | ||
|
||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Promise = require('bluebird'); | ||
var logger = require('intel').getLogger('legal-templates'); | ||
|
||
module.exports = function (i18n, root) { | ||
|
||
var TOS_ROOT_PATH = path.join(root, 'terms'); | ||
var PP_ROOT_PATH = path.join(root, 'privacy'); | ||
|
||
function getRoot(type) { | ||
return type === 'terms' ? TOS_ROOT_PATH : PP_ROOT_PATH; | ||
} | ||
|
||
var templateCache = {}; | ||
function getTemplate(type, lang, defaultLang) { | ||
var DEFAULT_LOCALE = i18n.localeFrom(defaultLang); | ||
|
||
// Filenames are normalized to locale, not language. | ||
var locale = i18n.localeFrom(lang); | ||
var templatePath = path.join(getRoot(type), locale + '.html'); | ||
var resolver = Promise.defer(); | ||
|
||
// cache the promises to avoid multiple concurrent checks for | ||
// the same template due to async calls to the file system. | ||
if (templateCache[templatePath]) { | ||
resolver.resolve(templateCache[templatePath]); | ||
return resolver.promise; | ||
} | ||
|
||
fs.exists(templatePath, function (exists) { | ||
if (! exists) { | ||
var bestLang = i18n.bestLanguage(i18n.parseAcceptLanguage(lang)); | ||
|
||
if (locale === DEFAULT_LOCALE) { | ||
var err = new Error(type + ' missing `' + DEFAULT_LOCALE + '` template: ' + templatePath); | ||
return resolver.reject(err); | ||
} else if (lang !== bestLang) { | ||
logger.warn('`%s` does not exist, trying next best `%s`', lang, bestLang); | ||
return resolver.resolve(getTemplate(type, bestLang, defaultLang)); | ||
} | ||
|
||
|
||
templateCache[templatePath] = null; | ||
return resolver.resolve(null); | ||
} | ||
|
||
fs.readFile(templatePath, 'utf8', function(err, data) { | ||
if (err) { | ||
return resolver.reject(err); | ||
} | ||
|
||
templateCache[templatePath] = data; | ||
resolver.resolve(data); | ||
}); | ||
}); | ||
|
||
return resolver.promise; | ||
} | ||
|
||
return getTemplate; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are now run as part of
l10n-generate-pages
, since it depends on them.