Can a JSON file be loaded from a dynamic source for localisation purposes?
grunt.file.readJSON('src/locales/<%= grunt.task.current.args[0] %>/i18n.json');
A sample section of the Gruntfile is as follows:
module.exports = function(grunt) {
var i18n = {
locales: ['en', 'fr', 'de', 'es'],
default: 'en',
replacements: function(locale){
var content = grunt.file.readJSON('src/locales/<%= grunt.task.current.args[0] %>/i18n.json');
var arr = [];
for(i in content){
var replacement = {
from: i,
to: content[i].value
};
arr.push(replacement);
}
return arr;
}
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
replace: {
build: {
src: ['local/en/**/*.html'],
dest: 'local/<%= grunt.task.current.args[0] %>/',
replacements: i18n.replacements('<%= grunt.task.current.args[0] %>')
}
},
When registering the task:
grunt.registerTask('localise', function(){
var tasks = [];
for(i in i18n.locales){
if(i18n.locales[i] !== i18n.default){
tasks.push('replace:build:' + i18n.locales[i]);
}
}
grunt.task.run(tasks);
});
Everything works well except for loading the JSON file for replacements.
I've also attempted:
grunt.file.readJSON('src/locales/'+locale+'/i18n.json');
Unfortunately, this did not work either, and I'm unsure of how to proceed.
Could anyone provide assistance?
Thank you