From f11d5ad1dcb7e6cffa7ff11db7b8bf6070750cae Mon Sep 17 00:00:00 2001 From: boiram Date: Mon, 19 Aug 2019 09:00:04 +0200 Subject: [PATCH] Support prefix for language files Sometimes it is needed, that a language-file has a prefix like "webapp-en" or "locale-en" depending on a framework or a plugin. So it would be nice if the i18n-express would support loading such files. --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1692a01..27f5033 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ function getLangFromCookie (req, cookieName) { } } -function loadLangJSONFiles (langPath, defaultLang) { +function loadLangJSONFiles (langPath, defaultLang, prefix) { var i18n = []; i18n[defaultLang] = []; @@ -38,7 +38,7 @@ function loadLangJSONFiles (langPath, defaultLang) { delete require.cache[require.resolve(langPath + '/' + files[i])]; } catch (e) {} - i18n[files[i].split('.').shift().toLowerCase()] = require(langPath + '/' + files[i]); + i18n[files[i].split('.').shift().toLowerCase().replace(prefix.toLowerCase(),'')] = require(langPath + '/' + files[i]); } } } else { @@ -56,6 +56,7 @@ exports = module.exports = function (opts) { var paramLangName = opts.paramLangName || 'clang'; var siteLangs = opts.siteLangs || ['en']; var textsVarName = opts.textsVarName || 'texts'; + var prefix = opts.prefix || ''; if (siteLangs.constructor !== Array) { throw new Error('siteLangs must be an Array with supported langs.');