28
loading...
This website collects cookies to deliver better user experience
dependencies:
flutter:
sdk: flutter
#This lines are new
flutter_localizations:
sdk: flutter
cupertino_icons: ^1.0.0
#This line is new
intl: ^0.17.0
LocalizationsDelegates
are classes which define a series of localized values by loading them. On our main class, as parameters for the MaterialApp we need to set our delegates:localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLanguages
and which language to load. So we'll need to create also an abstract class called Languages, that will extend our subclassed for all our languages:abstract class Languages {
static Languages of(BuildContext context) {
return Localizations.of<Languages>(context, Languages);
}
}
class AppLocalizationsDelegate extends LocalizationsDelegate<Languages> {
const AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) =>
['es', 'en', 'de', 'it'].contains(locale.languageCode);
@override
Future<Languages> load(Locale locale) => _load(locale);
static Future<Languages> _load(Locale locale) async {
switch (locale.languageCode) {
case 'it':
return LanguageIt();
case 'es':
return LanguageEs();
case 'de':
return LanguageDe();
case 'en':
return LanguageEn();
default:
return LanguageEn();
}
}
@override
bool shouldReload(LocalizationsDelegate<Languages> old) => false;
}
abstract class Languages {
static Languages of(BuildContext context) {
return Localizations.of<Languages>(context, Languages);
}
String get aString;
}
class LanguageIt extends Languages {
@override
String get aString => "Sono un testo";
}
class LanguageEs extends Languages {
@override
String get aString => "Soy un texto";
}
class LanguageEn extends Languages {
@override
String get aString => "I am a text";
}
class LanguageDe extends Languages {
@override
String get aString => "Ich bin ein Text";
}
localeResolutionCallback
to our MaterialApp
. This will determine which language is the one our app should be displayed. Also we'll need to add the supportedLocales property, to let our MaterialApp
know which ones are supported.supportedLocales: [
const Locale('it', ''),
const Locale('en', ''),
const Locale('de', ''),
const Locale('es', '')
],
localeResolutionCallback: (locale, supportedLocales) {
for (var supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale?.languageCode) {
return supportedLocale;
}
}
return supportedLocales.first;
},
Languages.of(context).aString
ChangeNotifier
which will contain the current locale and initialize it as the system one:class LocalizationManager extends ChangeNotifier {
static Locale get defaultLocale => Locale('it', 'IT');
Locale _currentLocale;
final locales = [
const Locale('it', ''),
const Locale('en', ''),
const Locale('de', ''),
const Locale('es', '')
];
Future<Locale> locale(BuildContext context) async {
if (_currentLocale != null) {
return _currentLocale;
}
String platformLocaleName = Platform.localeName;
if (platformLocaleName.contains('_')) {
platformLocaleName = platformLocaleName.split('_').first;
}
if (platformLocaleName.contains('-')) {
platformLocaleName = platformLocaleName.split('-').first;
}
if (platformLocaleName != null) {
for (Locale configurationLocale in locales) {
if (platformLocaleName.toLowerCase() ==
configurationLocale.languageCode.toLowerCase()) {
_currentLocale = configurationLocale;
break;
}
}
}
if (_currentLocale == null) {
_currentLocale = locales.first ?? defaultLocale;
}
return _currentLocale;
}
Future<void> setLocale(Locale locale) async {
_currentLocale = locale;
notifyListeners();
}
}
notifyListeners()
in order to let our app know when to redraw itself:Future<void> setLocale(Locale locale) async {
_currentLocale = locale;
notifyListeners();
}
MultiProvider(
providers:[
ChangeNotifierProvider<LocalizationManager>(create: (_) => LocalizationManager()),
],
child: Consumer<LocalizationManager>(
builder: (context, localizationManager, child) {
return FutureBuilder<Locale>(
future: localizationManager.locale(context),
builder: (context, snapshot) {
return MaterialApp(
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('it', ''),
const Locale('en', ''),
const Locale('de', ''),
const Locale('es', '')
],
locale: snapshot.data,
localeResolutionCallback: (locale, supportedLocales) {
return locale;
},
home: Scaffold(
backgroundColor: Colors.black,
body: DemoApp(),
),
);
},
);
},
),
),
CupertinoButton(
child: Text('Ita'),
onPressed: () {
Provider.of<LocalizationManager>(context, listen: false).setLocale(Locale('it', ''));
},
),
CupertinoButton(
child: Text('En'),
onPressed: () {
Provider.of<LocalizationManager>(context, listen: false).setLocale(Locale('en', ''));
},
),
CupertinoButton(
child: Text('Es'),
onPressed: () {
Provider.of<LocalizationManager>(context, listen: false).setLocale(Locale('es', ''));
},
),
CupertinoButton(
child: Text('De'),
onPressed: () {
Provider.of<LocalizationManager>(context, listen: false).setLocale(Locale('de', ''));
},
),