initLanguageSettings function
Initialize the Intl-Framework
// Include this if you run your app in the browser
import 'package:intl/intl_browser.dart';
import 'package:l10n/l10n.dart';
import 'package:browser_example/_l10n/messages_all.dart';
Future main() async {
final String locale = await initLanguageSettings(
() => findSystemLocale(),
(final String locale) => initializeMessages(locale)
);
...
}
Implementation
Future<String> initLanguageSettings(
Future<String> findLocale(),
Future<bool> initMessages(final String locale)) async {
// Determine your locale
final String locale = await findLocale(); // Calls platform specific "findSystemLocale"
final String shortLocale = Intl.shortLocale(Uri.base.queryParameters['lang'] ?? locale);
// Important - otherwise the Browser doesn't show the right language!
Intl.systemLocale = shortLocale;
// Avoids error message:
// LocaleDataException: Locale data has not been initialized,
// call initializeDateFormatting(<locale>).
await initializeDateFormatting(shortLocale);
// Initialize translation-table
// calls back into application specific part
await initMessages(shortLocale);
return shortLocale;
}