Skip to content

Commit

Permalink
feature: initLanguageSetting shortens the init-section in main
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMitterer committed Nov 13, 2018
1 parent cb0734c commit 09537d8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 60 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
});
// ...
}
```
Expand Down
40 changes: 40 additions & 0 deletions lib/l10n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
}
26 changes: 4 additions & 22 deletions samples/browser/web/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand All @@ -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;
}
46 changes: 21 additions & 25 deletions samples/cmdline/bin/cmdline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.