|
| 1 | +--- |
| 2 | +title: Adding a New Language |
| 3 | +description: A guide for developers on how to add a new language for localization. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Steps } from '@astrojs/starlight/components'; |
| 7 | + |
| 8 | +The web dashboard is built with localization in mind, making it straightforward to add support for new languages. The process involves creating a new `.arb` file and updating a few places in the code. |
| 9 | + |
| 10 | +<Steps> |
| 11 | +1. **Create a New `.arb` File** |
| 12 | + |
| 13 | + Application strings are stored in Application Resource Bundle (`.arb`) files located in `lib/l10n/arb/`. |
| 14 | + |
| 15 | + - Duplicate the `app_en.arb` file. |
| 16 | + - Rename the new file to `app_<language_code>.arb`, where `<language_code>` is the two-letter ISO 639-1 code for the new language (e.g., `app_es.arb` for Spanish). |
| 17 | + - Translate all the string values in the new file. |
| 18 | + |
| 19 | +2. **Update `l10n.yaml`** |
| 20 | + |
| 21 | + The `l10n.yaml` file in the root of the project configures the code generation for localization. |
| 22 | + |
| 23 | + - Open `l10n.yaml`. |
| 24 | + - Add your new `.arb` file to the list. |
| 25 | + |
| 26 | + ```yaml title="l10n.yaml" |
| 27 | + arb-dir: lib/l10n/arb |
| 28 | + template-arb-file: app_en.arb |
| 29 | + output-localization-file: app_localizations.dart |
| 30 | + ``` |
| 31 | +
|
| 32 | +3. **Run Code Generation** |
| 33 | +
|
| 34 | + Flutter's localization tool will automatically generate the necessary Dart code to support the new language. |
| 35 | +
|
| 36 | + Run the following command in your terminal: |
| 37 | + ```bash |
| 38 | + flutter gen-l10n |
| 39 | + ``` |
| 40 | + |
| 41 | +4. **Update the Settings Page** |
| 42 | + |
| 43 | + To make the new language selectable by the user, you need to add it to the settings page UI. |
| 44 | + |
| 45 | + - Open `lib/settings/view/settings_page.dart`. |
| 46 | + - Locate the `_LanguageSelectionList` widget. |
| 47 | + - Add the new language code to the `_supportedLanguages` list. |
| 48 | + - Add a case to the `_getLanguageName` method to provide a display name for the new language. |
| 49 | + |
| 50 | + ```dart title="lib/settings/view/settings_page.dart" |
| 51 | + class _LanguageSelectionList extends StatelessWidget { |
| 52 | + // ... |
| 53 | +
|
| 54 | + String _getLanguageName(AppLanguage language, AppLocalizations l10n) { |
| 55 | + switch (language) { |
| 56 | + case 'en': |
| 57 | + return l10n.englishLanguage; |
| 58 | + case 'ar': |
| 59 | + return l10n.arabicLanguage; |
| 60 | + // Add your new language here |
| 61 | + case 'es': |
| 62 | + return 'Español'; // This should come from your .arb file |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + // Add your new language code here |
| 67 | + static const List<AppLanguage> _supportedLanguages = ['en', 'ar', 'es']; |
| 68 | + } |
| 69 | + ``` |
| 70 | + |
| 71 | +</Steps> |
| 72 | + |
| 73 | +After completing these steps, the new language will be fully integrated into the web dashboard. |
0 commit comments