Skip to content

Commit 2d3ee07

Browse files
Merge pull request #1721 from adaptlearning/issue/1715
Add custom function to generate lang files
2 parents 81fa161 + 5340ce7 commit 2d3ee07

6 files changed

Lines changed: 45 additions & 22 deletions

File tree

.gitignore

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

26-
/routes/lang/en.json
27-
2826
/temp
2927
/test/.testcache
3028
/tmp

Gruntfile.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ 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'
7+
'generate-lang-json': {
8+
options: {
9+
langFileExt: '.json',
10+
src: {
11+
backend: 'routes/lang',
12+
frontend: 'frontend/src/**/lang'
13+
},
14+
dest: 'temp/lang'
1415
}
1516
},
1617
copy: {
@@ -269,6 +270,30 @@ module.exports = function(grunt) {
269270
done();
270271
}
271272
});
273+
grunt.registerTask('generate-lang-json', function() {
274+
const fs = require('fs-extra');
275+
const path = require('path');
276+
277+
const options = this.options();
278+
const backendGlob = path.join(options.src.backend, `*${options.langFileExt}`);
279+
const dest = options.dest;
280+
// load each route lang file
281+
/**
282+
* NOTE there must be a file in routes/lang for the language to be loaded,
283+
* won't work if you've only got lang files in frontend
284+
*/
285+
grunt.file.expand({}, path.join(backendGlob)).forEach(backendPath => {
286+
const basename = path.basename(backendPath);
287+
const frontendGlob = path.join(options.src.frontend, basename);
288+
let data = { ...fs.readJSONSync(backendPath) };
289+
// load all matching frontend lang files
290+
grunt.file.expand({}, frontendGlob).forEach(frontendPath => {
291+
data = { ...data, ...fs.readJSONSync(frontendPath) };
292+
});
293+
fs.ensureDirSync(dest);
294+
fs.writeJSONSync(path.join(dest, basename), data, { spaces: 2 });
295+
});
296+
});
272297

273298
grunt.registerTask('default', ['build:dev']);
274299
grunt.registerTask('test', ['mochaTest']);
@@ -290,7 +315,7 @@ module.exports = function(grunt) {
290315
config.isProduction = isProduction;
291316
grunt.file.write(configFile, JSON.stringify(config, null, 2));
292317
// run the task
293-
grunt.task.run(['migration-conf', 'requireBundle', 'merge-json', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:'+ compilation]);
318+
grunt.task.run(['migration-conf', 'requireBundle', 'generate-lang-json', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:'+ compilation]);
294319

295320
} catch(e) {
296321
grunt.task.run(['requireBundle', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:' + compilation]);

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"grunt-contrib-copy": "^1.0.0",
4848
"grunt-contrib-handlebars": "^2.0.0",
4949
"grunt-contrib-requirejs": "^1.0.0",
50-
"grunt-merge-json": "^0.9.7",
5150
"grunt-mocha-test": "^0.13.3",
5251
"handlebars": "^4.4.0",
5352
"handlebars-form-helpers": "^0.1.4",

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)