|
1 | 1 | ---
|
2 |
| -title: Adding a New Language |
3 |
| -description: A guide for developers on how to add a new language for localization. |
| 2 | +title: 'Guide: Localization' |
| 3 | +description: A guide to adding new languages and translations to the web client. |
4 | 4 | ---
|
| 5 | +import { Steps, Aside } from '@astrojs/starlight/components'; |
5 | 6 |
|
6 |
| -import { Steps } from '@astrojs/starlight/components'; |
| 7 | +The web client is fully internationalized using Flutter's built-in localization support. This guide explains how to add new translation strings and support for new languages. |
7 | 8 |
|
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 | +### Localization Files |
9 | 10 |
|
10 |
| -<Steps> |
11 |
| -1. **Create a New `.arb` File** |
| 11 | +All translation strings are stored in Application Resource Bundle (`.arb`) files located in the `lib/l10n/arb/` directory. |
| 12 | + |
| 13 | +- `app_en.arb`: Contains the English translations. This is the template file. |
| 14 | +- `app_ar.arb`: Contains the Arabic translations. |
12 | 15 |
|
13 |
| - Application strings are stored in Application Resource Bundle (`.arb`) files located in `lib/l10n/arb/`. |
| 16 | +Each entry in an `.arb` file consists of a key, the translated string, and an optional description. |
| 17 | + |
| 18 | +```json title="lib/l10n/arb/app_en.arb" |
| 19 | +{ |
| 20 | + "@@locale": "en", |
| 21 | + "accountPageTitle": "Account", |
| 22 | + "@accountPageTitle": { |
| 23 | + "description": "Title for the account page" |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### How to Add a New Translation String |
| 29 | + |
| 30 | +<Steps> |
| 31 | +1. **Add the Key to the English Template** |
14 | 32 |
|
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. |
| 33 | + Open `lib/l10n/arb/app_en.arb` and add a new entry for your string. Provide a clear key and a helpful description. |
18 | 34 |
|
19 |
| -2. **Update `l10n.yaml`** |
| 35 | + ```json |
| 36 | + "myNewFeatureTitle": "My New Feature", |
| 37 | + "@myNewFeatureTitle": { |
| 38 | + "description": "Title for the new feature page" |
| 39 | + } |
| 40 | + ``` |
20 | 41 |
|
21 |
| - The `l10n.yaml` file in the root of the project configures the code generation for localization. |
| 42 | +2. **Add Translations to Other Languages** |
22 | 43 |
|
23 |
| - - Open `l10n.yaml`. |
24 |
| - - Add your new `.arb` file to the list. |
| 44 | + Open the `.arb` file for each supported language (e.g., `app_ar.arb`) and add the corresponding translated string for the new key. |
25 | 45 |
|
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 |
| 46 | + ```json |
| 47 | + "myNewFeatureTitle": "ميزتي الجديدة", |
30 | 48 | ```
|
31 | 49 |
|
32 |
| -3. **Run Code Generation** |
| 50 | +3. **Generate Dart Code** |
33 | 51 |
|
34 |
| - Flutter's localization tool will automatically generate the necessary Dart code to support the new language. |
| 52 | + Flutter's localization tool automatically generates Dart code from your `.arb` files. This process is usually triggered automatically when you save an `.arb` file if you are using a modern IDE with the Flutter extension. |
35 | 53 |
|
36 |
| - Run the following command in your terminal: |
| 54 | + If you need to run it manually, execute the following command in your terminal: |
37 | 55 | ```bash
|
38 | 56 | flutter gen-l10n
|
39 | 57 | ```
|
40 | 58 |
|
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 |
| - } |
| 59 | +4. **Use the String in Your UI** |
| 60 | + |
| 61 | + You can now access your new string in any widget that has a `BuildContext` using the generated `AppLocalizations` class. |
| 62 | + |
| 63 | + ```dart |
| 64 | + import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart'; |
| 65 | +
|
| 66 | + // ... inside a build method |
| 67 | + Text(context.l10n.myNewFeatureTitle); |
69 | 68 | ```
|
70 | 69 |
|
71 | 70 | </Steps>
|
72 | 71 |
|
73 |
| -After completing these steps, the new language will be fully integrated into the web dashboard. |
| 72 | +### How to Add a New Language |
| 73 | + |
| 74 | +To add support for a new language, such as French: |
| 75 | +1. Create a new file: `lib/l10n/arb/app_fr.arb`. |
| 76 | +2. Set the locale at the top of the file: `"@@locale": "fr"`. |
| 77 | +3. Copy all the keys from `app_en.arb` and provide the French translations. |
| 78 | +4. Run `flutter gen-l10n` to update the generated code. |
| 79 | +5. Finally, add the new language to the list of supported languages in `lib/app/view/app.dart`. |
0 commit comments