Files
panel/resources/scripts/lib/i18n.ts
2023-11-06 04:20:25 +00:00

40 lines
1.4 KiB
TypeScript

import i18n from 'i18next';
import I18NextHttpBackend, { HttpBackendOptions } from 'i18next-http-backend';
import I18NextMultiloadBackendAdapter from 'i18next-multiload-backend-adapter';
import { initReactI18next } from 'react-i18next';
import { z } from 'zod';
import { zodI18nMap } from 'zod-i18n-map';
// If we're using HMR use a unique hash per page reload so that we're always
// doing cache busting. Otherwise just use the builder provided hash value in
// the URL to allow cache busting to occur whenever the front-end is rebuilt.
const hash = Date.now().toString(16)
i18n.use(I18NextMultiloadBackendAdapter)
.use(initReactI18next)
.init({
// debug: process.env.DEBUG === 'true',
lng: 'en_US',
fallbackLng: 'en_US',
keySeparator: '.',
backend: {
backend: I18NextHttpBackend,
backendOption: {
loadPath:
'/locales/locale.json?locale={{lng}}&namespace={{ns}}',
queryStringParams: { hash },
allowMultiLoading: true,
} as HttpBackendOptions,
} as Record<string, any>,
interpolation: {
// Per i18n-react documentation: this is not needed since React is already
// handling escapes for us.
escapeValue: false,
},
})
i18n.loadNamespaces('zod')
z.setErrorMap(zodI18nMap)
export default i18n