@winstrike/next-i18next-koa
Version:
The easiest way to translate your NextJs apps. With Koa support only. Forked from 'next-i18next'.
81 lines (59 loc) • 3.13 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
var _defaultConfig = _interopRequireWildcard(require("./default-config"));
var deepMergeObjects = ['backend', 'detection'];
var _default = function _default(userConfig) {
if (typeof userConfig.localeSubpaths === 'boolean') {
throw new Error('The localeSubpaths option has been changed to a string: "none", "foreign", or "all"');
} // Initial merge of default and user-provided config
var combinedConfig = (0, _objectSpread2.default)({}, _defaultConfig.default, userConfig); // Sensible defaults to prevent user duplication
combinedConfig.allLanguages = combinedConfig.otherLanguages.concat([combinedConfig.defaultLanguage]);
combinedConfig.whitelist = combinedConfig.allLanguages;
var allLanguages = combinedConfig.allLanguages,
defaultLanguage = combinedConfig.defaultLanguage,
localeExtension = combinedConfig.localeExtension,
localePath = combinedConfig.localePath,
localeStructure = combinedConfig.localeStructure;
if (_defaultConfig.isServer) {
var fs = eval("require('fs')");
var path = require('path'); // Set server side backend
combinedConfig.backend = {
loadPath: path.join(process.cwd(), "".concat(localePath, "/").concat(localeStructure, ".").concat(localeExtension)),
addPath: path.join(process.cwd(), "".concat(localePath, "/").concat(localeStructure, ".missing.").concat(localeExtension)) // Set server side preload (languages and namespaces)
};
combinedConfig.preload = allLanguages;
if (!combinedConfig.ns) {
var getAllNamespaces = function getAllNamespaces(p) {
return fs.readdirSync(p).map(function (file) {
return file.replace(".".concat(localeExtension), '');
});
};
combinedConfig.ns = getAllNamespaces(path.join(process.cwd(), "".concat(localePath, "/").concat(defaultLanguage)));
}
} else {
// Set client side backend
combinedConfig.backend = {
loadPath: "/".concat(localePath, "/").concat(localeStructure, ".").concat(localeExtension),
addPath: "/".concat(localePath, "/").concat(localeStructure, ".missing.").concat(localeExtension)
};
combinedConfig.ns = [combinedConfig.defaultNS];
} // Set fallback language to defaultLanguage in production
if (!userConfig.fallbackLng) {
combinedConfig.fallbackLng = process.env.NODE_ENV === 'production' ? combinedConfig.defaultLanguage : false;
} // Deep merge with overwrite - goes last
deepMergeObjects.forEach(function (obj) {
if (userConfig[obj]) {
combinedConfig[obj] = (0, _objectSpread2.default)({}, _defaultConfig.default[obj], userConfig[obj]);
}
});
return combinedConfig;
};
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;