package.initializers.i18n.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
import i18n from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import { mergeDeepLeft } from "ramda";
import { initReactI18next } from "react-i18next";
import { LIST_FORMATS } from "./constants";
import { listFormatter, lowerCaseDynamicTextFormatter } from "./utils/customFormatters";
import { sentenceCaseProcessor } from "./utils/customPostProcessors";
import { replaceNullValuesWithGetter } from "./utils/utils";
import commonsEn from "../translations/en.json";
// eslint-disable-next-line import/no-mutable-exports
var taxonomies = {};
var initializeI18n = function initializeI18n(hostTranslations) {
var _window$globalProps;
// eslint-disable-next-line no-undef
var packageTranslations = preval.require("../configs/scripts/getPkgTranslations.js");
var commonsTranslations = {
en: {
translation: commonsEn
}
};
var resources = [hostTranslations, commonsTranslations, packageTranslations].reduce(mergeDeepLeft);
var defaultTaxonomyKeys = Object.keys(resources.en.translation.taxonomyDefaultLabels || {});
var defaultTaxonomies = Object.fromEntries(defaultTaxonomyKeys.map(function (key) {
return [key, {
singular: null,
plural: null
}];
}));
var hostTaxonomies = ((_window$globalProps = window.globalProps) === null || _window$globalProps === void 0 ? void 0 : _window$globalProps.taxonomies) || {};
taxonomies = replaceNullValuesWithGetter(mergeDeepLeft(hostTaxonomies, defaultTaxonomies));
i18n.use(LanguageDetector).use(initReactI18next).use(sentenceCaseProcessor).init({
resources: resources,
fallbackLng: "en",
interpolation: {
defaultVariables: {
taxonomies: taxonomies
},
escapeValue: false,
skipOnVariables: false,
alwaysFormat: true,
format: function format(value, _format, lng, options) {
var newValue = value;
if (LIST_FORMATS.includes(_format)) {
newValue = listFormatter({
value: newValue,
format: _format,
lng: lng,
options: options
});
return newValue;
}
return lowerCaseDynamicTextFormatter(newValue, _format);
}
},
postProcess: [sentenceCaseProcessor.name],
detection: {
order: ["querystring", "cookie", "navigator", "path"],
caches: ["cookie"],
lookupQuerystring: "lang",
lookupCookie: "lang"
}
});
};
export { taxonomies };
export default initializeI18n;
//# sourceMappingURL=i18n.js.map