Adding New Languages
This guide explains how to add support for new languages to your application using the internationalization package.Overview
The internationalization package currently supports English (en), Turkish (tr), Spanish (es), and German (de). You can add support for additional languages by following these steps:- Update configuration files
- Create translation files
- Test the new language
Step 1: Update Configuration Files
First, you need to update the configuration files to include the new language:Update languine.json
Add the new language code to thetargets array in languine.json:
Update i18nConfig.ts
Add the new language code to thelocales array in i18nConfig.ts:
Step 2: Create Translation Files
You need to create translation files for the new language in both thedictionaries and locales directories:
Create Dictionary File
Create a new file in thedictionaries directory with the name of your language code:
Create Locale File
Create a new file in thelocales directory with the name of your language code:
Step 3: Generate Translations (Optional)
If you have a large number of translations, you can use the built-in translation script to generate translations automatically:Step 4: Test the New Language
After adding the new language, test it to make sure everything works correctly:-
Start your development server:
-
Visit your application with the new locale path:
- Check that the content is displayed in the new language.
- Test navigation, forms, and other interactive elements to ensure translations are applied correctly.
Handling Special Characters and RTL Languages
Special Characters
Some languages use special characters that may require specific handling:Right-to-Left (RTL) Languages
For languages that are read from right to left (like Arabic or Hebrew), you need to add additional support:- Create a helper function to identify RTL languages:
- Update your layout to apply RTL direction when needed:
- Add CSS to handle RTL styling:
Best Practices for Multilingual Content
Use Variables for Dynamic Content
Use Variables for Dynamic Content
Instead of hardcoding values in your translations, use variables:This makes it easier to maintain consistent branding across all languages.
Consider Cultural Differences
Consider Cultural Differences
Be aware that some phrases or concepts may not translate directly between cultures:
- Date formats (MM/DD/YYYY vs. DD/MM/YYYY)
- Number formats (1,000.00 vs. 1.000,00)
- Currency symbols and positions
- Cultural references and idioms
Plan for Text Expansion
Plan for Text Expansion
Some languages require more space than English. German words can be 30% longer, while some Asian languages may be more compact.Design your UI to accommodate text of varying lengths:
- Avoid fixed-width containers for text
- Test your UI with the longest translations
- Use ellipsis or truncation for very long text
Keep Translation Keys Organized
Keep Translation Keys Organized
Organize your translation keys logically by feature or page:This makes it easier to find and update translations.
Examples
Adding French Language Support
Here’s a complete example of adding French language support:- Update
languine.json:
- Update
i18nConfig.ts:
- Create
dictionaries/fr.jsonwith translated content:
- Create
locales/fr.json:
- Update your language switcher to include French: