ente/packages/shared/i18n/index.ts

62 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-03-13 15:28:00 +00:00
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-http-backend';
2023-11-09 04:10:43 +00:00
import { getBestPossibleUserLocale } from './utils';
2024-02-21 14:00:32 +00:00
import { isDevBuild } from '../network/api';
2023-03-13 15:28:00 +00:00
2024-02-21 11:09:31 +00:00
/**
* Load translations.
*
* Localization and related concerns (aka "internationalization", or "i18n") for
* our apps is handled by i18n framework.
*
* In addition to the base i18next package, we use two of its plugins:
*
* - i18next-http-backend, for loading the JSON files containin the translations
* at runtime, and
*
* - react-i18next, which adds React specific APIs
*/
2023-03-15 12:01:17 +00:00
export const setupI18n = async () => {
2024-02-21 11:09:31 +00:00
// https://www.i18next.com/overview/api
2023-03-15 12:01:17 +00:00
await i18n
2024-02-21 11:09:31 +00:00
// i18next-http-backend: Asynchronously loads translations over HTTP
// https://github.com/i18next/i18next-http-backend
2023-03-15 12:01:17 +00:00
.use(Backend)
2024-02-21 11:09:31 +00:00
// react-i18next: React support
// Pass the i18n instance to react-i18next.
2023-03-15 12:01:17 +00:00
.use(initReactI18next)
2024-02-21 11:09:31 +00:00
// Initialize i18next
// Option docs: https://www.i18next.com/overview/configuration-options
2023-03-15 12:01:17 +00:00
.init({
2024-02-21 14:00:32 +00:00
debug: isDevBuild,
returnEmptyString: false,
fallbackLng: 'en',
2023-03-15 12:01:17 +00:00
lng: getBestPossibleUserLocale(),
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
react: {
2023-03-20 05:22:58 +00:00
useSuspense: false,
2023-03-15 12:01:17 +00:00
transKeepBasicHtmlNodesFor: [
'div',
'strong',
'h2',
'span',
'code',
'p',
'br',
],
},
load: 'languageOnly',
2023-03-15 12:01:17 +00:00
});
2023-03-13 15:28:00 +00:00
2023-03-15 12:01:17 +00:00
i18n.services.formatter.add('dateTime', (value, lng) => {
return new Date(value / 1000).toLocaleDateString(lng, {
year: 'numeric',
month: 'long',
day: 'numeric',
});
2023-03-14 08:46:23 +00:00
});
2023-03-15 12:01:17 +00:00
};