Guide to Working with Translations (react-intl)
The project uses the react-intl library to manage multilingual content. This guide describes the basic operations with translations.
Adding a New Translation
- Add a new message to the translations definition file:
// Translations definitions file (*.intl.js)import { defineMessages } from 'react-intl';
const messages = defineMessages({ // Existing translations...
// New translation accessClosed: { id: 'AccessClosed.accessClosed', // Unique message identifier defaultMessage: 'Access closed', // Default language text (English) description: 'Access closed message', // Description for translators },});
export default messages;- Extract new messages for translation:
yarn run extract-intlAfter running this command, the new message will be automatically added to all localization files in the /lang/*.json directory.
Updating an Existing Translation
When you need to change an existing translation (e.g., fixing a typo or changing wording), follow these steps:
- Update the message in the translations definition file (messages.js)
- Update the translation in
/lang/en.json(primary language) - Clear outdated translations in other language files:
- For all non-English language files (
/lang/*.jsonexcept/lang/en.json), you need to replace the old translation value with an empty string ("") for the affected key - This ensures translators will notice that the translation needs to be updated
- Important: Do NOT delete the key entirely - just empty the value
- For all non-English language files (
Using the Helper Script
To automate step 3, you can use the provided script:
- Open
scripts/translations/remove-messages-by-key.js - Add the translation keys you want to clear to the
messagesKeysForRemovearray:const messagesKeysForRemove = ["editVehicle.totalKmIncluded","dashboard.welcomeMessage"]; - Run the script:
Terminal window node scripts/translations/remove-messages-by-key.js - The script will automatically set these translations to empty strings in all language files except
en.json
Example
If you’re updating the message with key "dashboard.welcomeMessage":
- Before in
/lang/fr.json:"dashboard.welcomeMessage": "Bienvenue" - After in
/lang/fr.json:"dashboard.welcomeMessage": ""
Deleting a Translation
- Remove the message from the translations definition file (messages.js)
- Remove the corresponding entries from all localization files
/lang/*.json
Using Translations in Components
import React from 'react';import { FormattedMessage } from 'react-intl';import messages from './messages.intl';
const MyComponent = () => ( <div> <FormattedMessage {...messages.accessClosed} /> </div>);
export default MyComponent;Using the useIntl Hook
import React from 'react';import { useIntl } from 'react-intl';import messages from './messages.intl';
const MyComponent = () => { const intl = useIntl();
// Simple string translation const translatedText = intl.formatMessage(messages.accessClosed);
// Translation with parameters const welcomeMessage = intl.formatMessage( messages.welcome, { name: 'User' } );
// Usage in event handlers const handleClick = () => { alert(intl.formatMessage(messages.clickAlert)); };
return ( <div> <p>{translatedText}</p> <p>{welcomeMessage}</p> <button onClick={handleClick}>Click me</button> </div> );};
export default MyComponent;Additional Recommendations
- Use meaningful identifiers for messages
- Always add clear descriptions for translators
- Regularly synchronize translations with the locale editor