The AppLocalization class

The next step is to create a class that encapsulates the app's localized values. The AppLocalizations class, for example, that would be very similar in all apps, except the string resources involved. This is what it looks like:

// part of app_localization.dart
import 'l10n/messages_all.dart';

class AppLocalizations {
static Future<AppLocalizations> load(Locale locale) {
final String name =
locale.countryCode == null ? locale.languageCode : locale.toString();
final String localeName = Intl.canonicalizedLocale(name);

return initializeMessages(localeName).then((bool _) {
Intl.defaultLocale = localeName;
return new AppLocalizations();
});
}

static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}

String get title {
return Intl.message(
'Hello Flutter',
name: 'title',
desc: 'The application title'
);
}

String get hello {
return Intl.message('Hello', name: 'hello');
}
}

AppLocalizations is used to encapsulates the resources. It can be broken down into four main pieces:

  • The load function: This will load the string resources from the desired Locale, as you can see in the parameter.
  • The of function: This will be a helper like for any other InheritedWidget to facilitate access to any string from any part of the app code.
  • get functions: These will list the available resources translated into our app. Note the Intl.message wrapper in the return; that will make the intl tool look up this class and populate the initializeMessages for us with the translations.
  • initializeMessages: This method will be generated by the intl tool. Note the import "l10n/messages_all.dart" file that will be generated in the next steps contains the method that effectively loads the translated messages.

In addition to this class, we need to create another class responsible for providing the AppLocalizations resources to the app. This is what it looks like:

class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
const AppLocalizationsDelegate();

@override
bool isSupported(Locale locale) {
return ['en', 'es', 'it'].contains(locale.languageCode);
}

@override
Future<AppLocalizations> load(Locale locale) {
return AppLocalizations.load(locale);
}

@override
bool shouldReload(LocalizationsDelegate<AppLocalizations> old) {
return false;
}
}

It also can be broken down into three main pieces:

  • The load function: Here is the information from the documentation:
"The load method must return an object that contains a collection of related resources (typically defined with one method per resource)."

We return our AppLocalizations.load class.

  • isSupported: As the name suggests, it returns true if the app has support for receivedlocale.
  • shouldReload: Basically, if this method returns true, then all of the app widgets will be rebuilt after the loading of resources. You will typically want to return true if your app changes Locale dynamically.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.97.64