Created
July 10, 2015 14:54
-
-
Save pdehaan/364172fcef550f8fd0a2 to your computer and use it in GitHub Desktop.
Convert l10n .json files to .html
This file contains hidden or 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
| 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