Skip to content

Commit

Permalink
Preload translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcglasberg committed Dec 14, 2024
1 parent 3ab9c79 commit da79298
Show file tree
Hide file tree
Showing 31 changed files with 626 additions and 68 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,6 @@ class MyI18n {
extension Localization on String {
String get i18n => localize(this, MyI18n.translations);
String plural(value) => localizePlural(value, this, MyI18n.translations);
String fill(List<Object> params) => localizeFill(this, params);
}
```

Expand Down
7 changes: 7 additions & 0 deletions example/assets/translations/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Hello, welcome to this internationalization demo.": "Hello, welcome to this internationalization demo.",
"i18n Demo": "i18n Demo",
"Increment": "Increment",
"Change Language": "Change Language",
"You clicked the button %d times:": "You clicked the button %d times:"
}
7 changes: 7 additions & 0 deletions example/assets/translations/es-ES.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Hello, welcome to this internationalization demo.": "Hola, bienvenido a esta demostración de internacionalización.",
"i18n Demo": "Demostración i18n",
"Increment": "Incrementar",
"Change Language": "Cambiar idioma",
"You clicked the button %d times:": "Hiciste clic en el botón %d veces:"
}
7 changes: 7 additions & 0 deletions example/assets/translations/pt-BR.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Hello, welcome to this internationalization demo.": "Olá, bem-vindo a esta demonstração de internacionalização.",
"i18n Demo": "Demonstração i18n",
"Increment": "Incrementar",
"Change Language": "Mude Idioma",
"You clicked the button %d times:": "Você clicou o botão %d vezes:"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ extension Localization on String {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);

String version(Object modifier) => localizeVersion(modifier, this, _t);
Expand Down
14 changes: 3 additions & 11 deletions example/lib/1_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import 'my_screen.dart';
///
void main() async {
WidgetsFlutterBinding.ensureInitialized();

runApp(
I18n(
initialLocale: await I18n.loadLocale(),
Expand All @@ -45,12 +45,8 @@ class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
/// The initial locale for this app's [Localizations] widget is based on this
/// value. If the 'locale' is null then the system's locale value is used.
/// The value of [Localizations.locale] will equal this locale if it matches one
/// of the [supportedLocales]. Otherwise it will be the first [supportedLocales].
// locale: I18n.of(context).locale,
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
Expand All @@ -62,10 +58,7 @@ class AppCore extends StatelessWidget {
const Locale('pt', 'BR'),
const Locale('es', 'ES'),
],
home: Container(
//
child: MyHomePage(),
),
home: MyHomePage(),
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
Expand All @@ -74,7 +67,6 @@ class AppCore extends StatelessWidget {
),
),
),
debugShowCheckedModeBanner: false,
);
}
}
Expand Down
2 changes: 0 additions & 2 deletions example/lib/1_translation_example/main.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ extension Localization on String {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);

String version(Object modifier) => localizeVersion(modifier, this, _t);
Expand Down
2 changes: 0 additions & 2 deletions example/lib/1_translation_example/my_screen.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ extension Localization on String {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);

String version(Object modifier) => localizeVersion(modifier, this, _t);
Expand Down
2 changes: 0 additions & 2 deletions example/lib/1_translation_example/my_widget.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ extension Localization on String {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);

String version(Object modifier) => localizeVersion(modifier, this, _t);
Expand Down
24 changes: 15 additions & 9 deletions example/lib/2_identifier_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ import 'my_screen.dart';
/// [appbarTitle], [greetings], [increment], [changeLanguage],
/// and [youClickedThisNumberOfTimes].
///
void main() => runApp(MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();

class MyApp extends StatelessWidget {
runApp(
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
child: AppCore(),
),
);
}

class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
Expand All @@ -38,13 +50,7 @@ class MyApp extends StatelessWidget {
const Locale('en', "US"),
const Locale('pt', "BR"),
],
home: I18n(
//
// Keep it commented out to use the current system locale.
// initialLocale: 'es-ES'.asLocale,
//
child: MyHomePage(),
),
home: MyHomePage(),
);
}

Expand Down
2 changes: 0 additions & 2 deletions example/lib/2_identifier_translation_example/my.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,5 @@ extension MyLocalization on Object {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);
}
24 changes: 15 additions & 9 deletions example/lib/3_scoped_identifier_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ import 'my_screen.dart';
/// Text(MyScope.greetings.i18n);
/// MaterialButton(child: Text(OtherScope.increment.i18n));
///
void main() => runApp(MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();

class MyApp extends StatelessWidget {
runApp(
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
child: AppCore(),
),
);
}

class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
Expand All @@ -41,13 +53,7 @@ class MyApp extends StatelessWidget {
const Locale('en', "US"),
const Locale('pt', "BR"),
],
home: I18n(
//
// Keep it commented out to use the current system locale.
// initialLocale: 'es-ES'.asLocale,
//
child: MyHomePage(),
),
home: MyHomePage(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ extension MyLocalization on Object {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);
}

Expand Down
24 changes: 15 additions & 9 deletions example/lib/4_const_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ import 'my_screen.dart';

/// This example demonstrates how to use identifiers as translation keys, defined as
/// constants, using a the [ConstTranslations] constructor:
void main() => runApp(MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();

class MyApp extends StatelessWidget {
runApp(
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
child: AppCore(),
),
);
}

class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
Expand All @@ -24,13 +36,7 @@ class MyApp extends StatelessWidget {
const Locale('en', "US"),
const Locale('pt', "BR"),
],
home: I18n(
//
// Keep it commented out to use the current system locale.
// initialLocale: 'es-ES'.asLocale,
//
child: MyHomePage(),
),
home: MyHomePage(),
);
}

Expand Down
2 changes: 0 additions & 2 deletions example/lib/4_const_translation_example/my.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,5 @@ extension Localization on String {

String get i18n => localize(this, _t);

String fill(List<Object> params) => localizeFill(this, params);

String plural(value) => localizePlural(value, this, _t);
}
24 changes: 15 additions & 9 deletions example/lib/5_interpolation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ import 'my_screen.dart';
///
/// [appbarTitle], [greetings1], and [changeLanguage].
///
void main() => runApp(MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();

class MyApp extends StatelessWidget {
runApp(
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
child: AppCore(),
),
);
}

class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
Expand All @@ -37,13 +49,7 @@ class MyApp extends StatelessWidget {
const Locale('en', "US"),
const Locale('pt', "BR"),
],
home: I18n(
//
// Keep it commented out to use the current system locale.
// initialLocale: 'es-ES'.asLocale,
//
child: MyHomePage(),
),
home: MyHomePage(),
);
}

Expand Down
Binary file added example/lib/6_load_example/i18nScreen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit da79298

Please sign in to comment.