Flutter App Setup¶
This is the shortest path to a working Flutter integration with anas_localization.
1. Add translation files¶
Create locale files in assets/lang:
Example assets/lang/en.json:
2. Register the asset folder¶
In pubspec.yaml:
3. Generate the dictionary¶
This generates lib/generated/dictionary.dart.
4. Wrap the app¶
import 'package:anas_localization/anas_localization.dart';
import 'generated/dictionary.dart';
void main() {
runApp(const AppBootstrap());
}
class AppBootstrap extends StatelessWidget {
const AppBootstrap({super.key});
@override
Widget build(BuildContext context) {
return const AnasLocalization(
fallbackLocale: Locale('en'),
assetPath: 'assets/lang',
assetLocales: [
Locale('en'),
Locale('ar'),
Locale('tr'),
],
app: MyApp(),
);
}
}
5. Use the generated dictionary¶
import 'package:flutter/material.dart';
import 'generated/dictionary.dart';
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final dict = getDictionary();
return Scaffold(
appBar: AppBar(title: Text(dict.appName)),
body: Center(
child: Text(dict.welcomeUser(name: 'Anas')),
),
);
}
}