From 0e07333c2851786b3b99e51200b2ae76104ba5d3 Mon Sep 17 00:00:00 2001 From: Fabio Poloni Date: Thu, 7 May 2020 20:29:49 +0200 Subject: [PATCH] add support for static catalog (fixes #262) --- README.md | 8 +++++++- i18n.js | 12 +++++++---- test/i18n.configureStaticCatalog.js | 32 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 test/i18n.configureStaticCatalog.js diff --git a/README.md b/README.md index c2582a83..0f2fd307 100644 --- a/README.md +++ b/README.md @@ -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') */ }, + } }); ``` diff --git a/i18n.js b/i18n.js index 84be8555..e165e257 100644 --- a/i18n.js +++ b/i18n.js @@ -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) { diff --git a/test/i18n.configureStaticCatalog.js b/test/i18n.configureStaticCatalog.js new file mode 100644 index 00000000..f62bb1d7 --- /dev/null +++ b/test/i18n.configureStaticCatalog.js @@ -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(); + }); + +});