Skip to content

Instantly share code, notes, and snippets.

@rohan20
Last active September 4, 2018 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohan20/4fd4d0fc7709dd758abbe7f5cd7c63c7 to your computer and use it in GitHub Desktop.
Save rohan20/4fd4d0fc7709dd758abbe7f5cd7c63c7 to your computer and use it in GitHub Desktop.
flutter-localizations
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:your_app_package/services/localization/app_translations_delegate.dart';
import 'package:your_app_package/screens/home_page.dart';
import 'application.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
AppTranslationsDelegate _newLocaleDelegate;
@override
void initState() {
super.initState();
_newLocaleDelegate = AppTranslationsDelegate(newLocale: null);
application.onLocaleChanged = onLocaleChange;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
localizationsDelegates: [
_newLocaleDelegate,
const AppTranslationsDelegate(),
//provides localised strings
GlobalMaterialLocalizations.delegate,
//provides RTL support
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: application.supportedLocales(),
);
}
void onLocaleChange(Locale locale) {
setState(() {
_newLocaleDelegate = AppTranslationsDelegate(newLocale: locale);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment