Skip to content

Instantly share code, notes, and snippets.

@pdehaan
Created July 10, 2015 14:54
Show Gist options
  • Select an option

  • Save pdehaan/364172fcef550f8fd0a2 to your computer and use it in GitHub Desktop.

Select an option

Save pdehaan/364172fcef550f8fd0a2 to your computer and use it in GitHub Desktop.
Convert l10n .json files to .html
module.exports = function (grunt) {
grunt.registerMultiTask('l10n-json-to-html', 'Convert l10n JSON files to HTML', function () {
var done = this.async();
var yeoman = grunt.config('yeoman');
var fileCount = 0;
this.files.forEach(function (file) {
var src = file.src[0];
// Convert source file to something like '.tmp/i18n/{locale}/{file}.html'.
var dst = src.replace(yeoman.dist, yeoman.tmp).replace(/\.json$/i, '.html');
var data = grunt.file.readJSON(src);
var content = [];
Object.keys(data).forEach(function (key) {
var value = data[key];
// Ignore values with empty keys (l10n metadata) or empty values (no translations).
if (key.trim() !== '' && value.trim() !== '') {
content.push(value);
}
});
// Convert from an array to a string.
content = content.join('\n\n').trim();
// Only write file if there are at least some translations.
if (content.length !== 0) {
fileCount += 1;
grunt.verbose.writeln('Writing %s...', dst);
grunt.file.write(dst, content + '\n');
}
});
grunt.log.writeln('Wrote %d files', fileCount);
done();
});
grunt.config('l10n-json-to-html', {
dist: {
files: [
{
expand: true,
src: [
'<%= yeoman.dist %>/i18n/*/*.json'
]
}
]
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment