Internationalization
Here you can learn how to setup internationalization via i18next
Install dependencies
To use i18next with React and the specified plugins, you need to install the following dependencies:
i18next: The core internationalization library.
yarn add i18nextreact-i18next: The React bindings for i18next.
yarn add react-i18nexti18next-browser-languagedetector: A language detector for the browser.
yarn add i18next-browser-languagedetectori18next-http-backend: A backend for loading translations via HTTP.
yarn add i18next-http-backend
Initialize i18next
This code initializes i18next, setting up internationalization with several configurations:
.use(initReactI18next): Initializes i18next for React, providing React-specific features..use(I18nextBrowserLanguageDetector): Adds automatic browser-based language detection, which selects the user's language based on their browser settings..use(I18NextHttpBackend): Configures the backend to load translation files from an HTTP endpoint, specified in theloadPathof the maininitconfiguration.
fallbackLng: 'en': English is the default language if no preference is detected.load: 'languageOnly': Only the base language (e.g.,en) is loaded, without region variants.supportedLngs: ['en', 'uk']: Supports English and Ukrainian.backend.loadPath: Specifies the path to translation JSON files for language and namespace.defaultNS: 'common': Sets the default namespace.useSuspense: false: Disables suspense in React for smoother integration without loading delays.
Directory Structure ( Resource setup )
Create
localesDirectory Inside thepublicfolder of your UI, create a directory namedlocales.Language Directories Within the
localesdirectory, create separate directories for each language you will support, using abbreviations such as:ukfor Ukrainianenfor English
Resource Files Inside each language directory, create a JSON file with resource name like
general.json. Populate these files with structured translations.
English (en/general.json)
en/general.json)Ukrainian (uk/general.json)
uk/general.json)Notifications configuration
The requestNotification method is designed to trigger notifications with dynamic content based on a set of parameters:
Parameters: It takes an object with keys
type(notification type),resource(subject),action(performed action), andplural(boolean for noun plurality).Message Structure: Based on
plural, it selects either the singular or plural form of the resource (resourceI18n).API Call: The notification API is called with a title and a description, both localized with
i18next. The description includesresource,noun, andaction, where the action is continuous and lowercase.
This setup ensures adaptable, internationalized notifications for various actions and resources.
UI Usage
Import
useTranslationhook forreact-i18nextlibrary
Initialize translations variable in your component
Use your translations by providing translation path and name
API Usage
Create the
apiResponsestranslations file with the following content:
When throwing exceptions, set the error code as follows:
To manage API responses using the
openNotificationconfiguration, implement error handling as follows:
Last updated