Skip to content

Commit

Permalink
Add yaml support to localiseation files
Browse files Browse the repository at this point in the history
  • Loading branch information
HughePaul committed May 12, 2021
1 parent 4411627 commit cd95a13
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 27 deletions.
29 changes: 21 additions & 8 deletions lib/backends/fs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* istanbul ignore file */

var glob = require('glob'),
yaml = require('js-yaml'),
path = require('path'),
fs = require('fs'),
callsites = require('callsites'),
Expand Down Expand Up @@ -39,12 +40,12 @@ module.exports = {
}

options = Object.assign({
path: 'locales/__lng__/__ns__.json'
path: 'locales/__lng__/__ns__.__ext__'
}, options);
options.path = path.normalize(options.path);

var glb = options.path.replace('__lng__', '[a-zA-Z\-_]*').replace('__ns__', '[a-zA-Z\-_]*');
var rgx = options.path.replace('__lng__', '([a-zA-Z\-_]*)').replace('__ns__', '([a-zA-Z\-_]*)');
var glb = options.path.replace('__lng__', '[a-zA-Z\-_]*').replace('__ns__', '[a-zA-Z\-_]*').replace('__ext__', '@(json|yml|yaml)');
var rgx = options.path.replace('__lng__', '([a-zA-Z\-_]*)').replace('__ns__', '([a-zA-Z\-_]*)').replace('__ext__', '([a-zA-Z\-_]*)');
rgx = rgx.split('\\').join('\\\\'); // Windows hack, noop for unix file paths
rgx = new RegExp(rgx);

Expand Down Expand Up @@ -90,10 +91,11 @@ module.exports = {

var lng = parts[lngIndex];
var ns = parts[nsIndex];
var ext = path.extname(filename).substr(1);

if (lng && ns) {
filename = path.resolve(dir, filename);
files.push({ filename, lng, ns });
files.push({ filename, lng, ns, ext });
}
});
done();
Expand All @@ -110,16 +112,27 @@ module.exports = {

fs.readFile(file.filename, function (err, buffer) {
if (err) return done(err);

let data;
try {
datastore[file.lng] = datastore[file.lng] || {};
let data = JSON.parse(buffer.toString());
datastore[file.lng][file.ns] = deepCloneMerge(data, datastore[file.lng][file.ns]);
if (file.ext === 'json') {
data = JSON.parse(buffer.toString());
}
else if (file.ext === 'yaml' || file.ext === 'yml') {
data = yaml.load(buffer.toString());
} else {
throw new Error('Unknown localisation file format: ' + file.filename);
}
} catch(e) {
if (e instanceof SyntaxError) {
if (e instanceof SyntaxError || e instanceof yaml.YAMLException) {
e.message = 'Localisation file syntax error: ' + file.filename + ': ' + e.message;
}
return done(e);
}

datastore[file.lng] = datastore[file.lng] || {};
datastore[file.lng][file.ns] = deepCloneMerge(data, datastore[file.lng][file.ns]);

done();
});
}, done);
Expand Down
77 changes: 64 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"deep-clone-merge": "^1.5.2",
"findup": "^0.1.5",
"glob": "^7.1.7",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21"
},
"devDependencies": {
Expand Down
6 changes: 0 additions & 6 deletions test/spec/backends/locales/de/test.json

This file was deleted.

3 changes: 3 additions & 0 deletions test/spec/backends/locales/de/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: Hans
deep:
object: German

0 comments on commit cd95a13

Please sign in to comment.