Skip to content

Commit

Permalink
add support for static catalog (fixes #262)
Browse files Browse the repository at this point in the history
  • Loading branch information
einfallstoll committed May 7, 2020
1 parent a1fa83b commit 0e07333
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ i18n.configure({
// Downcase locale when passed on queryParam; e.g. lang=en-US becomes
// en-us. When set to false, the queryParam value will be used as passed;
// e.g. lang=en-US remains en-US.
preserveLegacyCase: true
preserveLegacyCase: true,

// set the language catalog statically
// also overrides locales
staticCatalog: {
de: { /* require('de.json') */ },
}
});
```

Expand Down
12 changes: 8 additions & 4 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,18 @@ module.exports = (function() {
true : opt.preserveLegacyCase;

// when missing locales we try to guess that from directory
opt.locales = opt.locales || guessLocales(directory);
opt.locales = opt.staticCatalog ? Object.keys(opt.staticCatalog) : opt.locales || guessLocales(directory);

// implicitly read all locales
if (Array.isArray(opt.locales)) {

opt.locales.forEach(function(l) {
read(l);
});
if (opt.staticCatalog) {
locales = opt.staticCatalog;
} else {
opt.locales.forEach(function(l) {
read(l);
});
}

// auto reload locale files when changed
if (autoReload) {
Expand Down
32 changes: 32 additions & 0 deletions test/i18n.configureStaticCatalog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var i18n = require('../i18n'),
should = require("should");

describe('staticCatalog configuration', function() {

it('should take locales from static catalog if set', function(done) {
i18n.configure({
staticCatalog: {
'en': {}
}
});

var expected = ['en'].sort();
should.deepEqual(i18n.getLocales(), expected);

done();
});

it('should use static locale definitions from static catalog if set', function(done) {
i18n.configure({
staticCatalog: {
'en': {}
}
});

var expected = new Object();
should.deepEqual(i18n.getCatalog('en'), expected);

done();
});

});

0 comments on commit 0e07333

Please sign in to comment.