Skip to content

Commit

Permalink
add AMD loader to language files
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed May 5, 2015
1 parent 4305bc7 commit 8918ff0
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 46 deletions.
97 changes: 63 additions & 34 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
var deepmerge = require('deepmerge');

function removeJshint(src) {
return src
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n\r?\n?/g, '')
.replace(/\/\*jshint -[EWI]{1}[0-9]{3} \*\/\r?\n\r?\n?/g, '');
}

module.exports = function(grunt) {
grunt.util.linefeed = '\n';

function removeJshint(src) {
return src
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n\r?\n?/g, '')
.replace(/\/\*jshint -[EWI]{1}[0-9]{3} \*\/\r?\n\r?\n?/g, '');
}

function process_lang(file, src, wrapper) {
var lang = file.split(/[\/\.]/)[2];
var content = JSON.parse(src);
wrapper = wrapper || ['',''];

grunt.config.set('lang_locale', content.__locale || lang);
grunt.config.set('lang_author', content.__author);
var header = grunt.template.process('<%= langBanner %>');

loaded_modules.forEach(function(m) {
var plugin_file = 'src/plugins/'+ m +'/i18n/'+ lang +'.json';

if (grunt.file.exists(plugin_file)) {
content = deepmerge(content, grunt.file.readJSON(plugin_file));
}
});

return header
+ '\n\n'
+ wrapper[0]
+ 'QueryBuilder.regional[\'' + lang + '\'] = '
+ JSON.stringify(content, null, 2)
+ ';\n\n'
+ 'QueryBuilder.defaults({ lang_code: \'' + lang + '\' });'
+ wrapper[1];
}


var all_modules = {},
all_langs = {},
loaded_modules = [],
Expand Down Expand Up @@ -66,7 +93,7 @@ module.exports = function(grunt) {
}

// default language
js_files_to_load.push('dist/i18n/en.js');
js_files_to_load.push('.temp/i18n/en.js');
loaded_langs.push('en');

// parse 'lang' parameter
Expand All @@ -75,7 +102,7 @@ module.exports = function(grunt) {
arg_langs.replace(/ /g, '').split(',').forEach(function(l) {
if (all_langs[l]) {
if (l !== 'en') {
js_files_to_load.push(all_langs[l].replace(/^src/, 'dist').replace(/json$/, 'js'));
js_files_to_load.push(all_langs[l].replace(/^src/, '.temp').replace(/json$/, 'js'));
loaded_langs.push(l);
}
}
Expand All @@ -100,7 +127,8 @@ module.exports = function(grunt) {
langBanner:
'/*!\n'+
' * jQuery QueryBuilder <%= pkg.version %>\n'+
' * <%= lang_copyright %>\n'+
' * Locale: <%= lang_locale %>\n'+
'<% if (lang_author) { %> * Author: <%= lang_author %>\n<% } %>'+
' * Licensed under MIT (http://opensource.org/licenses/MIT)\n'+
' */',

Expand Down Expand Up @@ -187,27 +215,22 @@ module.exports = function(grunt) {
options: {
stripBanners: false,
process: function(src, file) {
var lang = file.split(/[\/\.]/)[2];
var content = JSON.parse(src);

grunt.config.set('lang_copyright', content.__copyright || (lang + ' translation'));
var header = grunt.template.process('<%= langBanner %>');
delete content.__copyright;

loaded_modules.forEach(function(m) {
var plugin_file = 'src/plugins/'+ m +'/i18n/'+ lang +'.json';

if (grunt.file.exists(plugin_file)) {
content = deepmerge(content, grunt.file.readJSON(plugin_file));
}
});

return header
+ '\n\n'
+ 'jQuery.fn.queryBuilder.regional[\'' + lang + '\'] = '
+ JSON.stringify(content, null, 2)
+ ';\n\n'
+ 'jQuery.fn.queryBuilder.defaults({ lang_code: \'' + lang + '\' });'
var wrapper = grunt.file.read('src/i18n/.wrapper.js').replace(/\r\n/g, '\n').split(/@@js\n/);
return process_lang(file, src, wrapper);
}
}
},
lang_temp: {
files: Object.keys(all_langs).map(function(name) {
return {
src: 'src/i18n/'+ name +'.json',
dest: '.temp/i18n/' + name + '.js'
};
}),
options: {
stripBanners: false,
process: function(src, file) {
return process_lang(file, src);
}
}
},
Expand Down Expand Up @@ -317,6 +340,9 @@ module.exports = function(grunt) {
}
},

// clean build dir
clean: ['.temp'],

// jshint tests
jshint: {
lib: {
Expand Down Expand Up @@ -354,11 +380,11 @@ module.exports = function(grunt) {
pattern: /(<!-- qunit:modules -->)(?:[\s\S]*)(<!-- \/qunit:modules -->)/m,
replacement: function(match, m1, m2) {
var scripts = '\n';

grunt.file.expand('tests/*.module.js').forEach(function(file) {
scripts+= '<script src="../' + file + '"></script>\n';
});

return m1 + scripts + m2;
}
}]
Expand Down Expand Up @@ -465,16 +491,19 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-qunit-blanket-lcov');
grunt.loadNpmTasks('grunt-string-replace');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-coveralls');
grunt.loadNpmTasks('grunt-wrap');
grunt.loadNpmTasks('grunt-bump');

grunt.registerTask('build_js', [
'concat:lang_temp',
'concat:js',
'wrap:js',
'concat:js_standalone',
'uglify'
'uglify',
'clean'
]);

grunt.registerTask('build_css', [
Expand Down
2 changes: 2 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>jQuery QueryBuilder Example</title>

<link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css" id="bt-theme">
<link rel="stylesheet" href="../bower_components/bootstrap-select/dist/css/bootstrap-select.min.css">
<link rel="stylesheet" href="../bower_components/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css">
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"grunt-contrib-sass": "~0.9.0",
"grunt-qunit-blanket-lcov": "~0.3.1",
"grunt-string-replace": "~1.0.0",
"grunt-contrib-clean": "~0.6.0",
"grunt-coveralls": "~1.0.0",
"grunt-wrap": "~0.3.0",
"deepmerge": "~0.2.7",
Expand Down
15 changes: 15 additions & 0 deletions src/i18n/.wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery', 'query-builder'], factory);
}
else {
factory(root.jQuery);
}
}(this, function($) {
"use strict";

var QueryBuilder = $.fn.queryBuilder;

@@js

}));
3 changes: 2 additions & 1 deletion src/i18n/da.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Oversat af Jna Borup Coyle, [email protected]",
"__locale": "Danish (da)",
"__author": "Jna Borup Coyle, [email protected]",

"add_rule": "Tilføj regel",
"add_group": "Tilføj gruppe",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "German translation by \"raimu\"",
"__locale": "German (de)",
"__author": "\"raimu\"",

"add_rule": "neue Regel",
"add_group": "neue Gruppe",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Reference language file",
"__locale": "English (en)",
"__author": "Damien \"Mistic\" Sorel, http://www.strangeplanet.fr",

"add_rule": "Add rule",
"add_group": "Add group",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/es.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Spanish translation by \"pyarza\", \"kddlb\"",
"__locale": "Spanish (es)",
"__author": "\"pyarza\", \"kddlb\"",

"add_rule": "Añadir regla",
"add_group": "Añadir grupo",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/fr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "French translation by Damien \"Mistic\" Sorel",
"__locale": "French (fr)",
"__author": "Damien \"Mistic\" Sorel, http://www.strangeplanet.fr",

"add_rule": "Ajouter une règle",
"add_group": "Ajouter un groupe",
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/it.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"__copyright": "Italian translation",
"__locale": "Italian (it)",

"add_rule": "Aggiungi regola",
"add_group": "Aggiungi gruppo",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/nl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Dutch translation by \"Roywcm\"",
"__locale": "Dutch (nl)",
"__author": "\"Roywcm\"",

"add_rule": "Nieuwe regel",
"add_group": "Nieuwe groep",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/no.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Oversat af Jna Borup Coyle, [email protected]",
"__locale": "Norwegian (no)",
"__author": "Jna Borup Coyle, [email protected]",

"add_rule": "Legg til regel",
"add_group": "Legg til gruppe",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/pl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Polish translation by Artur Smolarek",
"__locale": "Polish (pl)",
"__author": "Artur Smolarek",

"add_rule": "Dodaj regułę",
"add_group": "Dodaj grupę",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/pt-BR.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Portuguese Brazilian translation by Leandro Gehlen ([email protected])",
"__locale": "Brazilian Portuguese (pr-BR)",
"__author": "Leandro Gehlen, [email protected]",

"add_rule": "Nova Regra",
"add_group": "Novo Gruop",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/ro.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__copyright": "Romanian translation by ArianServ",
"__locale": "Romanian (ro)",
"__author": "ArianServ",

"add_rule": "Adaugă regulă",
"add_group": "Adaugă grup",
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"__copyright": "Локализационный файл",
"__locale": "Russian (ru)",

"add_rule": "Добавить",
"add_group": "Добавить группу",
Expand Down

0 comments on commit 8918ff0

Please sign in to comment.