1.0.2 • Published 7 months ago
i18n-config-mfi v1.0.2
i18n-config-mfi
The library which contains the i18next configuration and common translation files.
Features
- Contains the default configuration of the i18next library.
- Contains the common translation files for all the MFI projects.
Installation
- Install the package via npm:
npm i i18n-config-mfi
- Wrap the App component with the I18nWrapper component from the i18n-config-mfi library. Pass the required properties: defaultLang, availableLangs and projectResources:
<I18nWrapper defaultLang="en" availableLangs={['en', 'fr', 'pt', 'es']} projectResources={projectResources}>
<App />
</I18nWrapper>
The availableLangs property must also contain the default language and be the first one in the array.
Create locales folder and add translation JSON files there.
Install translation-checker-mfi package via npm:
npm i translation-checker-mfi
Create translationRunner.ts file in the root folder.
Add following code in this file:
const { updateTranslations } = require('translation-checker-mfi');
const defaultLang = 'en';
const availableLanguages = ['en', 'fr', 'pt', 'es']; // Add more languages as needed
const fileLocation = './src/locales/'; // Add the base location of translation files
updateTranslations(defaultLang, fileLocation, availableLanguages);
- In package.json add this script:
"check-i18n": "ts-node ./src/translationRunner.ts"
Usage
- Import useTranslation hook from i18n-config-mfi library:
import { useTranslation } from 'i18n-config-mfi';
- To access the common translation use common: prefix before key:
<p>{t('common:hello')}</p>
- To access the translation of the project use only the key name specified in the JSON file:
<p>{t('error')}</p>