Permalink
Browse files

feature: initLanguageSetting shortens the init-section in main

  • Loading branch information...
MikeMitterer committed Nov 13, 2018
1 parent cb0734c commit 09537d85e3a2eacbf0bf3a4e1cf2284417b30939
Showing with 81 additions and 60 deletions.
  1. +16 −13 README.md
  2. +40 −0 lib/l10n.dart
  3. +4 −22 samples/browser/web/main.dart
  4. +21 −25 samples/cmdline/bin/cmdline.dart
@@ -42,20 +42,23 @@ import 'package:intl/intl_standalone.dart';
import 'package:l10n/l10n.dart';
import 'package:<your package>/_l10n/messages_all.dart';
Future main() async {
// Determine your locale
final String locale = await findSystemLocale();
final String shortLocale = Intl.shortLocale(locale);
// Avoids error message:
// LocaleDataException: Locale data has not been initialized,
// call initializeDateFormatting(<locale>).
await initializeDateFormatting(locale);
// Initialize translation-table
await initializeMessages(shortLocale);
void main() async {
// Init section
final shortLocale = await initLanguageSettings(
() => findSystemLocale(),
(final String locale) => initializeMessages(locale)
);
// App specific code
// ...
String message() => Intl.message("First test");
print(message());
print(l10n("Second test"));
// Here comes you app-code
[ "Mike", "Gerda", "Sarh"].forEach((name) {
print(l10n("Good morning [name]",{ "name" : name }));
});
// ...
}
```
@@ -3,6 +3,7 @@ library l10n;
import "dart:collection";
import 'dart:convert';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
import "package:validate/validate.dart";
@@ -28,3 +29,42 @@ String gettext(final String msgid, [final Map<String, dynamic> vars = const {} ]
String tr(final String msgid, [final Map<String, dynamic> vars = const {} ]) {
return l10n(msgid,vars);
}
/// 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)
/// );
/// ...
/// }
///
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;
}
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:html' as dom;
import 'package:intl/date_symbol_data_local.dart';
// Include this if you run your app in the browser
import 'package:intl/intl_browser.dart';
@@ -9,7 +8,10 @@ import 'package:l10n/l10n.dart';
import 'package:browser_example/_l10n/messages_all.dart';
Future main() async {
final String locale = await _initLanguageSettings();
final String locale = await initLanguageSettings(
() => findSystemLocale(),
(final String locale) => initializeMessages(locale)
);
(dom.querySelector("head") as dom.HeadElement).lang = locale;
@@ -25,23 +27,3 @@ Future main() async {
dom.querySelector("body").classes.remove("loading");
}
/// Initialize the Intl-Framework
///
Future<String> _initLanguageSettings() async {
// Determine your locale
final String locale = await 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
await initializeMessages(shortLocale);
return shortLocale;
}
@@ -2,32 +2,28 @@ import 'package:intl/intl.dart';
// Include this if you run your app on the cmdline
import 'package:intl/intl_standalone.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:l10n/l10n.dart';
import 'package:l10n_exsample_cmdline/_l10n/messages_all.dart';
void main(List<String> arguments) {
// Determine your locale
findSystemLocale().then((final String locale) async {
String shortLocale = Intl.shortLocale(locale);
// Avoids error message:
// LocaleDataException: Locale data has not been initialized,
// call initializeDateFormatting(<locale>).
await initializeDateFormatting(locale);
// Initialize translation-table
await initializeMessages(shortLocale);
String message() => Intl.message("First test");
print(message());
print(l10n("Second test"));
[ "Mike", "Gerda", "Sarh"].forEach((name) {
print(l10n("Good morning [name]",{ "name" : name }));
});
import '../lib/_l10n/messages_all.dart';
/// You can change the language on the cmdline like this:
///
/// dart bin/cmdline.dart en
/// dart bin/cmdline.dart de
///
void main(List<String> arguments) async {
await initLanguageSettings(
() => findSystemLocale(),
(final String locale) => initializeMessages(
arguments.isNotEmpty ? arguments.first : locale)
);
String message() => Intl.message("First test");
print(message());
print(l10n("Second test"));
[ "Mike", "Gerda", "Sarh"].forEach((name) {
print(l10n("Good morning [name]",{ "name" : name }));
});
}

0 comments on commit 09537d8

Please sign in to comment.