Skip to content

Commit 3909baa

Browse files
committed
Add custom function to generate lang files
1 parent 7fbab8d commit 3909baa

6 files changed

Lines changed: 42 additions & 26 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
/plugins/content/menu/versions/
2424
/plugins/content/bower/bowercache/
2525

26-
/routes/lang/en.json
27-
2826
/temp
2927
/test_frontend/img
3028
/tmp

Gruntfile.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ module.exports = function(grunt) {
44
// Project configuration.
55
grunt.initConfig({
66
pkg: grunt.file.readJSON('package.json'),
7-
"merge-json": {
8-
en: {
9-
src: [
10-
'routes/lang/en-application.json',
11-
'frontend/src/**/lang/en.json'
12-
],
13-
dest: 'routes/lang/en.json'
14-
}
15-
},
167
copy: {
178
main: {
189
files: [
@@ -194,7 +185,7 @@ module.exports = function(grunt) {
194185
},
195186
lang: {
196187
files: ['routes/lang/*.json'],
197-
tasks: ['merge-json']
188+
tasks: ['generate-lang-json']
198189
}
199190
},
200191
mochaTest: {
@@ -251,7 +242,7 @@ module.exports = function(grunt) {
251242
grunt.file.write(configFile, JSON.stringify(config, null, 2));
252243
// run the tasks
253244
var compilation = (config.isProduction) ? 'compile' : 'dev';
254-
grunt.task.run(['requireBundle', 'merge-json', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:'+ compilation]);
245+
grunt.task.run(['requireBundle', 'generate-lang-json', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:'+ compilation]);
255246
});
256247

257248
grunt.registerTask('server', "Running Server", function() {
@@ -346,7 +337,34 @@ module.exports = function(grunt) {
346337
done();
347338
}
348339
});
340+
// TODO should probably have config up there ^
341+
grunt.registerTask('generate-lang-json', function() {
342+
var _ = require('underscore');
343+
var fs = require('fs-extra');
344+
var path = require('path');
345+
346+
var langFileExt = '.json';
347+
var backendSrc = path.join('routes', 'lang', '*' + langFileExt);
348+
var frontendSrc = path.join('frontend', 'src', '**', 'lang');
349+
var dest = path.join('temp', 'lang');
350+
// load each route lang file
351+
/**
352+
* NOTE there must be a file in routes/lang for the language to be loaded,
353+
* won't work if you've only got lang files in frontend
354+
*/
355+
grunt.file.expand({}, backendSrc).forEach(function(filePath) {
356+
var lang = path.basename(filePath, langFileExt);
357+
var data = _.extend({}, fs.readJSONSync(filePath));
358+
// load all matching frontend lang files
359+
grunt.file.expand({}, path.join(frontendSrc, lang + langFileExt)).forEach(function(filePath2) {
360+
// TODO check for duplicates
361+
_.extend(data, fs.readJSONSync(filePath2));
362+
});
363+
fs.ensureDirSync(dest);
364+
fs.writeJSONSync(path.join(dest, lang + langFileExt), data, { spaces: 2 });
365+
});
366+
});
349367

350368
grunt.registerTask('test', ['mochaTest']);
351-
grunt.registerTask('default', ['merge-json', 'requireBundle', 'less:dev', 'handlebars', 'watch']);
369+
grunt.registerTask('default', ['generate-lang-json', 'requireBundle', 'less:dev', 'handlebars', 'watch']);
352370
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
"grunt-contrib-requirejs": "~0.4.4",
117117
"grunt-contrib-watch": "~0.5.1",
118118
"grunt-jscs": "^2.3.0",
119-
"grunt-merge-json": "^0.9.5",
120119
"grunt-mocha-test": "~0.7.0",
121120
"grunt-open": "~0.2.2",
122121
"handlebars": "^4.0.5",

routes/lang/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
var express = require('express');
22
var path = require('path');
33
var fs = require('fs');
4-
var server = module.exports = express();
54

6-
server.get('/lang/:lang', function (req, res, next) {
7-
var lang = req.params.lang; // ie 'en' for /lang/en
8-
var filename = path.join(__dirname, lang) + '.json';
9-
var file;
5+
var configuration = require('../../lib/configuration');
6+
var Constants = require('../../lib/outputmanager').Constants;
107

11-
fs.exists(filename, function(exists) {
12-
file = exists
13-
? require(filename)
14-
: require(path.join(__dirname, 'en') + '.json');
8+
var server = module.exports = express();
159

16-
return res.json(file);
17-
});
10+
server.get('/lang/:lang', function (req, res, next) {
11+
var lang = req.params.lang; // ie 'en' for /lang/en
12+
var filename = path.join(configuration.serverRoot, Constants.Folders.Temp, 'lang', lang + '.json');
13+
fs.exists(filename, function(exists) {
14+
if(!exists) {
15+
return res.status(404).end();
16+
}
17+
return res.json(require(filename));
18+
});
1819
});

0 commit comments

Comments
 (0)