Skip to content

Commit

Permalink
I18n supportedLocales etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcglasberg committed Dec 18, 2024
1 parent 382e5ca commit 2fda07c
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 94 deletions.
43 changes: 43 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@ Sponsored by [MyText.ai](https://mytext.ai)

[![](./example/SponsoredByMyTextAi.png)](https://mytext.ai)

## 15.0.0-dev.1

* Optionally, you can now set the `supportedLocales` of your app in the `I18n` widget.
For example, if your app supports American English and Standard Spanish, you'd use:
`supportedLocales: [Locale('en', 'US'), Locale('es')]`,
or `supportedLocales: ['en-US'.asLocale, 'es'.asLocale]`.

If you do set `I18n.supportedLocales`, you must add the line
`supportedLocales: I18n.supportedLocales` to your `MaterialApp` (or `CupertinoApp`)
widget, like this:

```dart
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(I18n(
initialLocale: ...,
supportedLocales: ['en-US'.asLocale, 'es'.asLocale], // Here!
child: AppCore(),
));
}
}
class AppCore extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
locale: I18n.locale,
supportedLocales: I18n.supportedLocales, // Here!
...
),
```

If you provide `I18n.supportedLocales`, only those supported locales will be
considered when recording **missing translations**. In other words, unsupported locales
will not be recorded as missing translations.


* **Breaking Change**: The `Translations.missingTranslationCallback` signature has
changed. This will only affect you if you have defined your own callback, which is
unlikely. If your code does break, update it to the new signature, which is an easy fix.
Note that it now returns a boolean. Only if it returns `true` will the missing
translation be added to the `Translations.missingTranslations` map.

## 14.1.0

Version 14 brings important improvements, like new interpolation methods, useful
Expand Down
25 changes: 13 additions & 12 deletions example/lib/1_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // Could also be 'en-US'.asLocale,
const Locale('pt', 'BR'), // Could also be 'pt-BR'.asLocale,
const Locale('es', 'ES'), // Could also be 'es-ES'.asLocale,
],
child: AppCore(),
),
);
Expand All @@ -45,19 +55,10 @@ class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'),
const Locale('pt', 'BR'),
const Locale('es', 'ES'),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
Expand Down
23 changes: 12 additions & 11 deletions example/lib/2_identifier_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // Could also be 'en-US'.asLocale,
const Locale('pt', 'BR'), // Could also be 'pt-BR'.asLocale,
],
child: AppCore(),
),
);
Expand All @@ -38,18 +47,10 @@ void main() async {
class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
);
}
Expand Down
23 changes: 12 additions & 11 deletions example/lib/3_scoped_identifier_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
child: AppCore(),
),
);
Expand All @@ -41,18 +50,10 @@ void main() async {
class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
);
}
Expand Down
23 changes: 12 additions & 11 deletions example/lib/4_const_translation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
child: AppCore(),
),
);
Expand All @@ -24,18 +33,10 @@ void main() async {
class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
);
}
Expand Down
23 changes: 12 additions & 11 deletions example/lib/5_interpolation_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
child: AppCore(),
),
);
Expand All @@ -37,18 +46,10 @@ void main() async {
class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
);
}
Expand Down
25 changes: 13 additions & 12 deletions example/lib/6_load_by_file_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // Could also be 'en-US'.asLocale,
const Locale('pt', 'BR'), // Could also be 'pt-BR'.asLocale,
const Locale('es', 'ES'), // Could also be 'es-ES'.asLocale,
],
child: AppCore(),
),
);
Expand All @@ -43,19 +53,10 @@ class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'),
const Locale('pt', 'BR'),
const Locale('es', 'ES'),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
Expand Down
25 changes: 13 additions & 12 deletions example/lib/7_load_by_http_example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ void main() async {
I18n(
initialLocale: await I18n.loadLocale(),
autoSaveLocale: true,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // Could also be 'en-US'.asLocale,
const Locale('pt', 'BR'), // Could also be 'pt-BR'.asLocale,
const Locale('es', 'ES'), // Could also be 'es-ES'.asLocale,
],
child: AppCore(),
),
);
Expand All @@ -52,19 +62,10 @@ class AppCore extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: I18n.locale,
debugShowCheckedModeBanner: false,
//
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'),
const Locale('pt', 'BR'),
const Locale('es', 'ES'),
],
locale: I18n.locale,
localizationsDelegates: I18n.localizationsDelegates,
supportedLocales: I18n.supportedLocales,
home: MyHomePage(),
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
Expand Down
Loading

0 comments on commit 2fda07c

Please sign in to comment.