generateLocalizationReadme static method
String
generateLocalizationReadme(
)
Implementation
static String generateLocalizationReadme() {
return '''
# Localization Guide
This app supports multiple languages using Flutter's internationalization (l10n).
## Supported Languages
### International Languages
- 🇬🇧 English (en)
- 🇪🇸 Spanish (es)
- 🇫🇷 French (fr)
- 🇩🇪 German (de)
- 🇮🇹 Italian (it)
- 🇵🇹 Portuguese (pt)
- 🇷🇺 Russian (ru)
- 🇨🇳 Chinese (zh)
- 🇯🇵 Japanese (ja)
- 🇸🇦 Arabic (ar)
### Indian Languages
- 🇮🇳 Hindi (hi)
- 🇮🇳 Bengali (bn)
- 🇮🇳 Telugu (te)
- 🇮🇳 Marathi (mr)
- 🇮🇳 Tamil (ta)
- 🇮🇳 Gujarati (gu)
- 🇮🇳 Kannada (kn)
- 🇮🇳 Malayalam (ml)
- 🇮🇳 Punjabi (pa)
- 🇮🇳 Odia (or)
## Setup Instructions
### 1. Add Dependencies
Add these to your `pubspec.yaml`:
```yaml
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: any
provider: ^6.0.0
shared_preferences: ^2.0.0
flutter:
generate: true
```
### 2. Create l10n.yaml
Create `l10n.yaml` in your project root:
```yaml
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
output-class: AppLocalizations
```
### 3. Create ARB Files
Create folder `lib/l10n/` and add ARB files:
- `app_en.arb` (template)
- `app_es.arb`, `app_fr.arb`, etc. for other languages
### 4. Generate Localization Files
Run this command:
```bash
flutter gen-l10n
```
This will generate the localization files in `.dart_tool/flutter_gen/gen_l10n/`.
### 5. Setup Main App
Update your `main.dart`:
```dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'core/providers/locale_provider.dart';
void main() {
runApp(
ChangeNotifierProvider(
create: (_) => LocaleProvider(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final localeProvider = Provider.of<LocaleProvider>(context);
return MaterialApp(
title: 'My App',
locale: localeProvider.locale,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en'), // English
Locale('es'), // Spanish
Locale('fr'), // French
Locale('de'), // German
Locale('it'), // Italian
Locale('pt'), // Portuguese
Locale('ru'), // Russian
Locale('zh'), // Chinese
Locale('ja'), // Japanese
Locale('ar'), // Arabic
Locale('hi'), // Hindi
Locale('bn'), // Bengali
Locale('te'), // Telugu
Locale('mr'), // Marathi
Locale('ta'), // Tamil
Locale('gu'), // Gujarati
Locale('kn'), // Kannada
Locale('ml'), // Malayalam
Locale('pa'), // Punjabi
Locale('or'), // Odia
],
home: const HomeScreen(),
);
}
}
```
## Usage
### In Code
```dart
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
// Use in widgets
Text(AppLocalizations.of(context)!.welcome)
Text(AppLocalizations.of(context)!.login)
Text(AppLocalizations.of(context)!.chatbot)
// Or with a shorter syntax
final l10n = AppLocalizations.of(context)!;
Text(l10n.hello)
Text(l10n.settings)
```
### Change Language
Use the `LanguageSelector` widget:
```dart
import 'package:your_app/widgets/language_selector.dart';
// In your settings screen or anywhere
ElevatedButton(
onPressed: () => LanguageSelector.show(context),
child: Text('Change Language'),
)
// Or use the widget directly
IconButton(
icon: const Icon(Icons.language),
onPressed: () => LanguageSelector.show(context),
)
```
### Access Current Locale
```dart
import 'package:provider/provider.dart';
import 'package:your_app/core/providers/locale_provider.dart';
// Get current locale
final currentLocale = Provider.of<LocaleProvider>(context).locale;
// Change locale programmatically
Provider.of<LocaleProvider>(context, listen: false)
.setLocale(Locale('es'));
```
## File Structure
```
lib/
├── l10n/
│ ├── app_en.arb
│ ├── app_es.arb
│ ├── app_fr.arb
│ ├── app_de.arb
│ ├── app_hi.arb
│ └── ... (other language files)
├── core/
│ └── providers/
│ └── locale_provider.dart
├── widgets/
│ └── language_selector.dart
└── main.dart
l10n.yaml (in project root)
```
## Adding New Translations
1. Open the English template file `lib/l10n/app_en.arb`
2. Add new keys with descriptions:
```json
{
"newKey": "New Text",
"@newKey": {
"description": "Description of this text"
}
}
```
3. Add translations to other language files
4. Run `flutter gen-l10n` to regenerate
5. Use in code: `AppLocalizations.of(context)!.newKey`
## Pluralization Example
For texts that need pluralization:
```json
{
"itemCount": "{count, plural, =0{No items} =1{1 item} other{{count} items}}",
"@itemCount": {
"description": "Number of items",
"placeholders": {
"count": {
"type": "int"
}
}
}
}
```
Usage:
```dart
Text(AppLocalizations.of(context)!.itemCount(5))
```
## Parameters Example
For texts with parameters:
```json
{
"greeting": "Hello {name}!",
"@greeting": {
"description": "Greeting with name",
"placeholders": {
"name": {
"type": "String"
}
}
}
}
```
Usage:
```dart
Text(AppLocalizations.of(context)!.greeting('John'))
```
## Testing
Test different languages:
```dart
// In your tests
testWidgets('Test localization', (tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('es'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: YourWidget(),
),
);
// Verify Spanish text appears
expect(find.text('Hola'), findsOneWidget);
});
```
## Best Practices
1. **Always use localization keys** - Never hardcode strings in UI
2. **Keep keys descriptive** - Use clear, meaningful key names
3. **Add descriptions** - Help translators understand context
4. **Test all languages** - Especially RTL languages like Arabic
5. **Handle long text** - Some translations may be longer
6. **Use parameters** - For dynamic content within translations
7. **Regenerate after changes** - Run `flutter gen-l10n` after ARB updates
## RTL Support
For Arabic and other RTL languages, Flutter automatically handles text direction. Ensure your layouts work with both LTR and RTL:
```dart
// Use these for proper RTL support
Directionality.of(context)
TextDirection.ltr
TextDirection.rtl
// Avoid hardcoded left/right, use start/end instead
EdgeInsets.symmetric(horizontal: 16) // Good
EdgeInsets.only(left: 16) // Avoid
```
## Troubleshooting
**Problem**: Generated files not found
**Solution**: Run `flutter gen-l10n` or `flutter pub get`
**Problem**: Locale not changing
**Solution**: Check LocaleProvider is properly set up with Provider
**Problem**: Missing translations
**Solution**: Ensure all ARB files have the same keys
**Problem**: Build errors after adding new language
**Solution**: Add the locale to `supportedLocales` in MaterialApp
## Resources
- [Flutter Internationalization Guide](https://docs.flutter.dev/development/accessibility-and-localization/internationalization)
- [ARB File Format](https://github.com/google/app-resource-bundle/wiki/ApplicationResourceBundleSpecification)
- [Intl Package](https://pub.flutter-io.cn/packages/intl)
## Contributing Translations
To contribute translations:
1. Fork the repository
2. Add/update ARB files in `lib/l10n/`
3. Test the translations
4. Submit a pull request
Ensure translations are:
- Accurate and natural
- Culturally appropriate
- Consistent with app tone
- Tested on actual devices
''';
}