Internationalization
Here you can learn how to setup internationalization via i18next
Last updated
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import I18nextBrowserLanguageDetector from 'i18next-browser-languagedetector';
import I18NextHttpBackend from 'i18next-http-backend';
i18next
.use(initReactI18next)
.use(I18nextBrowserLanguageDetector)
.use(I18NextHttpBackend)
.init({
fallbackLng: 'en',
load: 'languageOnly',
supportedLngs: ['en', 'uk'],
interpolation: {
escapeValue: false,
},
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json',
},
react: {
useSuspense: false,
},
defaultNS: 'common',
});{
"home": "Home",
"dashboard": "Dashboard",
"settings": "Settings",
"search": "Search",
"language": "Language",
"error": "Error",
"success": "Success",
"none": "None",
"or": "Or",
"field": "field"
}{
"home": "Головна",
"dashboard": "Панель управління",
"settings": "Налаштування",
"search": "Пошук",
"language": "Мова",
"error": "Помилка",
"success": "Операція успішна",
"none": "Немає",
"or": "Або",
"field": "поле"
}const requestNotification = ({ type, resource, action, plural }) => {
const nounType = plural ? 'plural' : 'singular';
const resourceI18n = t(`notifications:resources.${resource}.${nounType}`);
api[type]({
message: t(`general:${type}`),
description: t(`notifications:${type}`, {
resource: resourceI18n,
noun: t(`notifications:noun.${nounType}`),
action: t(`general:actions.continuous.${action}`).toLowerCase(),
}),
placement: 'top',
});
};import { useTranslation } from 'react-i18next';const { t } = useTranslation(['dashboard']);t('placeholders.businesses')useTranslation(['users', 'general', 'confirmModal', 'apiResponses']);t('placeholders.businesses'); // This will resolve to users:placeholder.businessest('general:statuses.active');{
"EMAIL_NOT_FOUND": {
"message": "To proceed, please sign up",
"description": "You don't have any accounts yet."
},
"INCORRECT_CREDENTIALS": {
"message": "Incorrect email or password",
"description": "The email or password you entered is incorrect. Please try again or contact support if you are unable to access your account."
},
"EMAIL_EXISTS": {
"message": "Error",
"description": "A user with this email is already registered."
}
}throw new HttpException(
{
message: 'Error',
description: 'A user with this email is already registered.',
code: 'EMAIL_EXISTS'
},
400
);catch (error) {
const { code } = error.response.data;
openNotification({
type: notificationType.ERROR,
message: code ? t(`apiResponses:${code}.message`) : t('notification.error.message'),
description: code ? t(`apiResponses:${code}.description`) : t('notification.error.description'),
});
}