gen_i18n 1.0.1
gen_i18n: ^1.0.1 copied to clipboard
Library to help you add internationalization in your project dart generating classes and translation files.
example/lib/main.dart
import 'package:example/i18n/i18n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Future<void> main() async {
/// This is required
WidgetsFlutterBinding.ensureInitialized();
/// Initial Locate is critical to initializing the internationalization feature
await I18n.initialize(
defaultLocale: Locale('en'), /// This is required
supportLocales: [Locale('en'), Locale('es')] /// This is optional
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
locale: I18n.currentLocate,
supportedLocales: I18n.supportedLocales,
localizationsDelegates: const [
const I18nDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'lib'.translate),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'customMessage'.translate,
),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}