|
| 1 | +--- |
| 2 | +title: 'Guide: Localization' |
| 3 | +description: A guide to adding new languages and translations to the mobile client. |
| 4 | +--- |
| 5 | +import { Steps, Aside } from '@astrojs/starlight/components'; |
| 6 | + |
| 7 | +The mobile 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. |
| 8 | + |
| 9 | +### Localization Files |
| 10 | + |
| 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. |
| 15 | + |
| 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** |
| 32 | + |
| 33 | + Open `lib/l10n/arb/app_en.arb` and add a new entry for your string. Provide a clear key and a helpful description. |
| 34 | + |
| 35 | + ```json |
| 36 | + "myNewFeatureTitle": "My New Feature", |
| 37 | + "@myNewFeatureTitle": { |
| 38 | + "description": "Title for the new feature page" |
| 39 | + } |
| 40 | + ``` |
| 41 | + |
| 42 | +2. **Add Translations to Other Languages** |
| 43 | + |
| 44 | + Open the `.arb` file for each supported language (e.g., `app_ar.arb`) and add the corresponding translated string for the new key. |
| 45 | + |
| 46 | + ```json |
| 47 | + "myNewFeatureTitle": "ميزتي الجديدة", |
| 48 | + ``` |
| 49 | + |
| 50 | +3. **Generate Dart Code** |
| 51 | + |
| 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. |
| 53 | + |
| 54 | + If you need to run it manually, execute the following command in your terminal: |
| 55 | + ```bash |
| 56 | + flutter gen-l10n |
| 57 | + ``` |
| 58 | + |
| 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_mobile_client_full_source_code/l10n/l10n.dart'; |
| 65 | +
|
| 66 | + // ... inside a build method |
| 67 | + Text(context.l10n.myNewFeatureTitle); |
| 68 | + ``` |
| 69 | + |
| 70 | +</Steps> |
| 71 | + |
| 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