Skip to content

Commit 8918ff0

Browse files
committed
add AMD loader to language files
1 parent 4305bc7 commit 8918ff0

File tree

16 files changed

+103
-46
lines changed

16 files changed

+103
-46
lines changed

Gruntfile.js

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
var deepmerge = require('deepmerge');
2-
3-
function removeJshint(src) {
4-
return src
5-
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n\r?\n?/g, '')
6-
.replace(/\/\*jshint -[EWI]{1}[0-9]{3} \*\/\r?\n\r?\n?/g, '');
7-
}
8-
92
module.exports = function(grunt) {
103
grunt.util.linefeed = '\n';
114

5+
function removeJshint(src) {
6+
return src
7+
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n\r?\n?/g, '')
8+
.replace(/\/\*jshint -[EWI]{1}[0-9]{3} \*\/\r?\n\r?\n?/g, '');
9+
}
10+
11+
function process_lang(file, src, wrapper) {
12+
var lang = file.split(/[\/\.]/)[2];
13+
var content = JSON.parse(src);
14+
wrapper = wrapper || ['',''];
15+
16+
grunt.config.set('lang_locale', content.__locale || lang);
17+
grunt.config.set('lang_author', content.__author);
18+
var header = grunt.template.process('<%= langBanner %>');
19+
20+
loaded_modules.forEach(function(m) {
21+
var plugin_file = 'src/plugins/'+ m +'/i18n/'+ lang +'.json';
22+
23+
if (grunt.file.exists(plugin_file)) {
24+
content = deepmerge(content, grunt.file.readJSON(plugin_file));
25+
}
26+
});
27+
28+
return header
29+
+ '\n\n'
30+
+ wrapper[0]
31+
+ 'QueryBuilder.regional[\'' + lang + '\'] = '
32+
+ JSON.stringify(content, null, 2)
33+
+ ';\n\n'
34+
+ 'QueryBuilder.defaults({ lang_code: \'' + lang + '\' });'
35+
+ wrapper[1];
36+
}
37+
38+
1239
var all_modules = {},
1340
all_langs = {},
1441
loaded_modules = [],
@@ -66,7 +93,7 @@ module.exports = function(grunt) {
6693
}
6794

6895
// default language
69-
js_files_to_load.push('dist/i18n/en.js');
96+
js_files_to_load.push('.temp/i18n/en.js');
7097
loaded_langs.push('en');
7198

7299
// parse 'lang' parameter
@@ -75,7 +102,7 @@ module.exports = function(grunt) {
75102
arg_langs.replace(/ /g, '').split(',').forEach(function(l) {
76103
if (all_langs[l]) {
77104
if (l !== 'en') {
78-
js_files_to_load.push(all_langs[l].replace(/^src/, 'dist').replace(/json$/, 'js'));
105+
js_files_to_load.push(all_langs[l].replace(/^src/, '.temp').replace(/json$/, 'js'));
79106
loaded_langs.push(l);
80107
}
81108
}
@@ -100,7 +127,8 @@ module.exports = function(grunt) {
100127
langBanner:
101128
'/*!\n'+
102129
' * jQuery QueryBuilder <%= pkg.version %>\n'+
103-
' * <%= lang_copyright %>\n'+
130+
' * Locale: <%= lang_locale %>\n'+
131+
'<% if (lang_author) { %> * Author: <%= lang_author %>\n<% } %>'+
104132
' * Licensed under MIT (http://opensource.org/licenses/MIT)\n'+
105133
' */',
106134

@@ -187,27 +215,22 @@ module.exports = function(grunt) {
187215
options: {
188216
stripBanners: false,
189217
process: function(src, file) {
190-
var lang = file.split(/[\/\.]/)[2];
191-
var content = JSON.parse(src);
192-
193-
grunt.config.set('lang_copyright', content.__copyright || (lang + ' translation'));
194-
var header = grunt.template.process('<%= langBanner %>');
195-
delete content.__copyright;
196-
197-
loaded_modules.forEach(function(m) {
198-
var plugin_file = 'src/plugins/'+ m +'/i18n/'+ lang +'.json';
199-
200-
if (grunt.file.exists(plugin_file)) {
201-
content = deepmerge(content, grunt.file.readJSON(plugin_file));
202-
}
203-
});
204-
205-
return header
206-
+ '\n\n'
207-
+ 'jQuery.fn.queryBuilder.regional[\'' + lang + '\'] = '
208-
+ JSON.stringify(content, null, 2)
209-
+ ';\n\n'
210-
+ 'jQuery.fn.queryBuilder.defaults({ lang_code: \'' + lang + '\' });'
218+
var wrapper = grunt.file.read('src/i18n/.wrapper.js').replace(/\r\n/g, '\n').split(/@@js\n/);
219+
return process_lang(file, src, wrapper);
220+
}
221+
}
222+
},
223+
lang_temp: {
224+
files: Object.keys(all_langs).map(function(name) {
225+
return {
226+
src: 'src/i18n/'+ name +'.json',
227+
dest: '.temp/i18n/' + name + '.js'
228+
};
229+
}),
230+
options: {
231+
stripBanners: false,
232+
process: function(src, file) {
233+
return process_lang(file, src);
211234
}
212235
}
213236
},
@@ -317,6 +340,9 @@ module.exports = function(grunt) {
317340
}
318341
},
319342

343+
// clean build dir
344+
clean: ['.temp'],
345+
320346
// jshint tests
321347
jshint: {
322348
lib: {
@@ -354,11 +380,11 @@ module.exports = function(grunt) {
354380
pattern: /(<!-- qunit:modules -->)(?:[\s\S]*)(<!-- \/qunit:modules -->)/m,
355381
replacement: function(match, m1, m2) {
356382
var scripts = '\n';
357-
383+
358384
grunt.file.expand('tests/*.module.js').forEach(function(file) {
359385
scripts+= '<script src="../' + file + '"></script>\n';
360386
});
361-
387+
362388
return m1 + scripts + m2;
363389
}
364390
}]
@@ -465,16 +491,19 @@ module.exports = function(grunt) {
465491
grunt.loadNpmTasks('grunt-contrib-watch');
466492
grunt.loadNpmTasks('grunt-qunit-blanket-lcov');
467493
grunt.loadNpmTasks('grunt-string-replace');
494+
grunt.loadNpmTasks('grunt-contrib-clean');
468495
grunt.loadNpmTasks('grunt-contrib-sass');
469496
grunt.loadNpmTasks('grunt-coveralls');
470497
grunt.loadNpmTasks('grunt-wrap');
471498
grunt.loadNpmTasks('grunt-bump');
472499

473500
grunt.registerTask('build_js', [
501+
'concat:lang_temp',
474502
'concat:js',
475503
'wrap:js',
476504
'concat:js_standalone',
477-
'uglify'
505+
'uglify',
506+
'clean'
478507
]);
479508

480509
grunt.registerTask('build_css', [

examples/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66

7+
<title>jQuery QueryBuilder Example</title>
8+
79
<link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css" id="bt-theme">
810
<link rel="stylesheet" href="../bower_components/bootstrap-select/dist/css/bootstrap-select.min.css">
911
<link rel="stylesheet" href="../bower_components/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css">

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"grunt-contrib-sass": "~0.9.0",
2020
"grunt-qunit-blanket-lcov": "~0.3.1",
2121
"grunt-string-replace": "~1.0.0",
22+
"grunt-contrib-clean": "~0.6.0",
2223
"grunt-coveralls": "~1.0.0",
2324
"grunt-wrap": "~0.3.0",
2425
"deepmerge": "~0.2.7",

src/i18n/.wrapper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(function(root, factory) {
2+
if (typeof define === 'function' && define.amd) {
3+
define(['jquery', 'query-builder'], factory);
4+
}
5+
else {
6+
factory(root.jQuery);
7+
}
8+
}(this, function($) {
9+
"use strict";
10+
11+
var QueryBuilder = $.fn.queryBuilder;
12+
13+
@@js
14+
15+
}));

src/i18n/da.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"__copyright": "Oversat af Jna Borup Coyle, [email protected]",
2+
"__locale": "Danish (da)",
3+
"__author": "Jna Borup Coyle, [email protected]",
34

45
"add_rule": "Tilføj regel",
56
"add_group": "Tilføj gruppe",

src/i18n/de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"__copyright": "German translation by \"raimu\"",
2+
"__locale": "German (de)",
3+
"__author": "\"raimu\"",
34

45
"add_rule": "neue Regel",
56
"add_group": "neue Gruppe",

src/i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"__copyright": "Reference language file",
2+
"__locale": "English (en)",
3+
"__author": "Damien \"Mistic\" Sorel, http://www.strangeplanet.fr",
34

45
"add_rule": "Add rule",
56
"add_group": "Add group",

src/i18n/es.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"__copyright": "Spanish translation by \"pyarza\", \"kddlb\"",
2+
"__locale": "Spanish (es)",
3+
"__author": "\"pyarza\", \"kddlb\"",
34

45
"add_rule": "Añadir regla",
56
"add_group": "Añadir grupo",

src/i18n/fr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"__copyright": "French translation by Damien \"Mistic\" Sorel",
2+
"__locale": "French (fr)",
3+
"__author": "Damien \"Mistic\" Sorel, http://www.strangeplanet.fr",
34

45
"add_rule": "Ajouter une règle",
56
"add_group": "Ajouter un groupe",

src/i18n/it.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"__copyright": "Italian translation",
2+
"__locale": "Italian (it)",
33

44
"add_rule": "Aggiungi regola",
55
"add_group": "Aggiungi gruppo",

0 commit comments

Comments
 (0)