2022-10-21 04:29:03 -05:00
|
|
|
import i18n, { BackendModule } from 'i18next';
|
2022-10-06 10:34:04 -05:00
|
|
|
import React from 'react';
|
2022-10-07 05:46:28 -05:00
|
|
|
import { Trans as I18NextTrans, initReactI18next } from 'react-i18next'; // eslint-disable-line no-restricted-imports
|
2022-06-17 07:41:03 -05:00
|
|
|
|
2022-11-22 06:18:34 -06:00
|
|
|
import { DEFAULT_LANGUAGE, LANGUAGES, VALID_LANGUAGES } from './constants';
|
2022-07-22 09:50:00 -05:00
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
const loadTranslations: BackendModule = {
|
|
|
|
type: 'backend',
|
|
|
|
init() {},
|
|
|
|
async read(language, namespace, callback) {
|
2022-11-22 06:18:34 -06:00
|
|
|
const localeDef = LANGUAGES.find((v) => v.code === language);
|
2022-10-21 04:29:03 -05:00
|
|
|
|
|
|
|
if (!localeDef) {
|
2022-10-06 10:34:04 -05:00
|
|
|
return callback(new Error('No message loader available for ' + language), null);
|
|
|
|
}
|
2021-12-15 10:00:37 -06:00
|
|
|
|
2022-10-21 04:29:03 -05:00
|
|
|
const messages = await localeDef.loader();
|
2022-10-06 10:34:04 -05:00
|
|
|
callback(null, messages);
|
|
|
|
},
|
|
|
|
};
|
2021-12-15 10:00:37 -06:00
|
|
|
|
2022-11-22 06:18:34 -06:00
|
|
|
export function initializeI18n(language: string) {
|
|
|
|
const validLocale = VALID_LANGUAGES.includes(language) ? language : DEFAULT_LANGUAGE;
|
2022-08-26 04:27:14 -05:00
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
i18n
|
|
|
|
.use(loadTranslations)
|
|
|
|
.use(initReactI18next) // passes i18n down to react-i18next
|
|
|
|
.init({
|
|
|
|
lng: validLocale,
|
2022-07-22 09:50:00 -05:00
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
// We don't bundle any translations, we load them async
|
|
|
|
partialBundledLanguages: true,
|
|
|
|
resources: {},
|
2021-12-15 10:00:37 -06:00
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
// If translations are empty strings (no translation), fall back to the default value in source code
|
|
|
|
returnEmptyString: false,
|
2022-10-14 05:55:15 -05:00
|
|
|
|
|
|
|
pluralSeparator: '__',
|
2021-12-15 10:00:37 -06:00
|
|
|
});
|
2022-10-12 06:51:16 -05:00
|
|
|
|
|
|
|
// This is a placeholder so we can put a 'comment' in the message json files.
|
|
|
|
// Starts with an underscore so it's sorted to the top of the file
|
|
|
|
t(
|
|
|
|
'_comment',
|
|
|
|
'Do not manually edit this file, or update these source phrases in Crowdin. The source of truth for English strings are in the code source'
|
|
|
|
);
|
2021-12-15 10:00:37 -06:00
|
|
|
}
|
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
export function changeLanguage(locale: string) {
|
2022-11-22 06:18:34 -06:00
|
|
|
const validLocale = VALID_LANGUAGES.includes(locale) ? locale : DEFAULT_LANGUAGE;
|
2022-10-06 10:34:04 -05:00
|
|
|
return i18n.changeLanguage(validLocale);
|
2021-12-15 10:00:37 -06:00
|
|
|
}
|
2022-06-17 07:41:03 -05:00
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
export const Trans: typeof I18NextTrans = (props) => {
|
|
|
|
return <I18NextTrans {...props} />;
|
|
|
|
};
|
2021-12-15 10:00:37 -06:00
|
|
|
|
2023-01-04 09:28:07 -06:00
|
|
|
// Reassign t() so i18next-parser doesn't warn on dynamic key, and we can have 'failOnWarnings' enabled
|
|
|
|
const tFunc = i18n.t;
|
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
export const t = (id: string, defaultMessage: string, values?: Record<string, unknown>) => {
|
2023-01-04 09:28:07 -06:00
|
|
|
return tFunc(id, defaultMessage, values);
|
2022-10-06 10:34:04 -05:00
|
|
|
};
|
2022-07-25 07:44:11 -05:00
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
export const i18nDate = (value: number | Date | string, format: Intl.DateTimeFormatOptions = {}): string => {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return i18nDate(new Date(value), format);
|
|
|
|
}
|
2022-11-22 06:18:34 -06:00
|
|
|
const locale = i18n.options.lng ?? DEFAULT_LANGUAGE;
|
2022-10-06 10:34:04 -05:00
|
|
|
|
|
|
|
const dateFormatter = new Intl.DateTimeFormat(locale, format);
|
|
|
|
return dateFormatter.format(value);
|
|
|
|
};
|