@zextras/carbonio-shell-ui
Version:
The Zextras Carbonio web client
114 lines • 4.46 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2021 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useI18nStore = void 0;
const i18next_1 = __importDefault(require("i18next"));
const i18next_chained_backend_1 = __importDefault(require("i18next-chained-backend"));
const i18next_http_backend_1 = __importDefault(require("i18next-http-backend"));
const immer_1 = require("immer");
const lodash_1 = require("lodash");
const react_i18next_1 = require("react-i18next");
const zustand_1 = require("zustand");
const constants_1 = require("../../constants");
const account_1 = require("../account");
const addShell = (apps) => [
...apps,
{
commit: '',
description: '',
js_entrypoint: '',
name: constants_1.SHELL_APP_ID,
priority: -1,
version: '',
type: 'shell',
attrKey: '',
icon: '',
display: 'Shell'
}
];
const { settings } = account_1.useAccountStore.getState();
const defaultLng = settings?.prefs?.zimbraPrefLocale ??
settings?.attrs?.zimbraLocale ??
'en';
const defaultI18n = i18next_1.default.createInstance({ lng: defaultLng });
const defaultI18nInitOptions = {
returnEmptyString: true,
returnNull: false,
compatibilityJSON: 'v4',
lng: defaultLng,
fallbackLng: 'en',
debug: false,
interpolation: {
escapeValue: false // not needed for react as it escapes by default
},
missingKeyHandler: (_, __, key) => {
// eslint-disable-next-line no-console
console.warn(`Missing translation with key '${key}'`);
},
backend: {
loadPath: `${BASE_PATH}/i18n/{{lng}}.json`
}
};
exports.useI18nStore = (0, zustand_1.create)()((set) => ({
instances: {},
defaultI18n,
locale: 'en',
setLocale: (locale) => {
set((0, immer_1.produce)((state) => {
state.locale = locale;
(0, lodash_1.forEach)(state.instances, (i18nInst) => i18nInst.changeLanguage(locale));
}));
},
addI18n: (apps, locale) => {
const appsWithShell = addShell(apps);
set((0, immer_1.produce)((state) => {
state.instances = (0, lodash_1.reduce)(appsWithShell, (acc, app) => {
const newI18n = i18next_1.default.createInstance();
newI18n
// load translation using http -> see /public/locales (i.e. https://github.com/i18next/react-i18next/tree/master/example/react/public/locales)
// learn more: https://github.com/i18next/i18next-http-backend
.use(i18next_chained_backend_1.default)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
...defaultI18nInitOptions,
lng: locale,
backend: {
backends: [i18next_http_backend_1.default, i18next_http_backend_1.default],
backendOptions: [
{
loadPath: app.name === constants_1.SHELL_APP_ID
? `${BASE_PATH}/i18n/{{lng}}.json`
: `${(0, lodash_1.dropRight)(app.js_entrypoint.split('/')).join('/')}/i18n/{{lng}}.json`
},
{
// fallback to shell for every module
loadPath: `${BASE_PATH}/i18n/{{lng}}.json`
}
]
}
});
// eslint-disable-next-line no-param-reassign
acc[app.name] = newI18n;
return acc;
}, {});
state.defaultI18n.t = state.instances[constants_1.SHELL_APP_ID].t;
state.locale = locale;
}));
}
}));
defaultI18n
.use(i18next_http_backend_1.default)
// pass the i18n instance to react-i18next.
.use(react_i18next_1.initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init(defaultI18nInitOptions);
//# sourceMappingURL=store.js.map