diff --git a/examples/internationalization/gen_l10n_example/lib/examples.dart b/examples/internationalization/gen_l10n_example/lib/examples.dart index 00c83aa5320..80f964e0e5b 100644 --- a/examples/internationalization/gen_l10n_example/lib/examples.dart +++ b/examples/internationalization/gen_l10n_example/lib/examples.dart @@ -1,6 +1,6 @@ // ignore_for_file: unused_local_variable -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; import 'l10n/app_localizations.dart'; diff --git a/examples/internationalization/gen_l10n_example/lib/main.dart b/examples/internationalization/gen_l10n_example/lib/main.dart index a9a725c2ec0..9ab8eb6ed41 100644 --- a/examples/internationalization/gen_l10n_example/lib/main.dart +++ b/examples/internationalization/gen_l10n_example/lib/main.dart @@ -1,7 +1,8 @@ -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; // #docregion localization-delegates-import -import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_localizations/flutter_localizations.dart' + as flutter_localizations; // #enddocregion localization-delegates-import // #docregion app-localizations-import @@ -19,9 +20,7 @@ class MyApp extends StatelessWidget { title: 'Localizations Sample App', localizationsDelegates: [ AppLocalizations.delegate, // Add this line - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, + ...GlobalMaterialLocalizations.delegate, ], supportedLocales: [ Locale('en'), // English diff --git a/examples/internationalization/gen_l10n_example/lib/no_app_localizations.dart b/examples/internationalization/gen_l10n_example/lib/no_app_localizations.dart new file mode 100644 index 00000000000..ddbe150b6d2 --- /dev/null +++ b/examples/internationalization/gen_l10n_example/lib/no_app_localizations.dart @@ -0,0 +1,89 @@ +import 'package:material_ui/material_ui.dart'; + +import 'package:flutter_localizations/flutter_localizations.dart' + as flutter_localizations; + +import 'l10n/app_localizations.dart'; + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + @override + Widget build(BuildContext context) { + // #docregion material-app + return const MaterialApp( + title: 'Localizations Sample App', + localizationsDelegates: GlobalMaterialLocalizations.delegates, + supportedLocales: [ + Locale('en'), // English + Locale('es'), // Spanish + ], + home: MyHomePage(), + ); + // #enddocregion material-app + } +} + +class MyHomePage extends StatefulWidget { + const MyHomePage({super.key}); + + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + // The [AppBar] title text should update its message + // according to the system locale of the target platform. + // Switching between English and Spanish locales should + // cause this text to update. + title: Text(AppLocalizations.of(context)!.helloWorld), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + // Add the following code + Localizations.override( + context: context, + locale: const Locale('en'), + // Using a Builder here to get the correct BuildContext. + // Alternatively, you can create a new widget and Localizations.override + // will pass the updated BuildContext to the new widget. + child: Builder( + builder: (context) { + // Examples of internationalized strings. + return Column( + children: [ + // Returns 'Hello John' + Text(AppLocalizations.of(context)!.hello('John')), + // Returns 'no wombats' + Text(AppLocalizations.of(context)!.nWombats(0)), + // Returns '1 wombat' + Text(AppLocalizations.of(context)!.nWombats(1)), + // Returns '5 wombats' + Text(AppLocalizations.of(context)!.nWombats(5)), + // Returns 'he' + Text(AppLocalizations.of(context)!.pronoun('male')), + // Returns 'she' + Text(AppLocalizations.of(context)!.pronoun('female')), + // Returns 'they' + Text(AppLocalizations.of(context)!.pronoun('other')), + ], + ); + }, + ), + ), + ], + ), + ), + ); + } +} + +void main() { + runApp(const MyApp()); +} diff --git a/sites/docs/src/content/ui/internationalization/index.md b/sites/docs/src/content/ui/internationalization/index.md index 69180da0649..32af665f405 100644 --- a/sites/docs/src/content/ui/internationalization/index.md +++ b/sites/docs/src/content/ui/internationalization/index.md @@ -57,9 +57,17 @@ in a directory of your choice with the `flutter create` command. $ flutter create ``` -To use `flutter_localizations`, -add the package as a dependency to your `pubspec.yaml` file, -as well as the `intl` package: +In the near future, Flutter projects will be encouraged to use the `material_ui` +and `cupertino_ui` packages, but the `flutter create` command has not yet been +updated to do this by default. You can get ahead of things and migrate now by +running the following command: + +```console +$ dart fix --apply --code=migrate_design_widgets +``` + +To use `flutter_localizations`, add the package as a dependency to your +`pubspec.yaml` file, as well as the `intl` package: ```console $ flutter pub add flutter_localizations --sdk=flutter @@ -84,18 +92,15 @@ your `MaterialApp` or `CupertinoApp`: ```dart -import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_localizations/flutter_localizations.dart' + as flutter_localizations; ``` - + ```dart return const MaterialApp( title: 'Localizations Sample App', - localizationsDelegates: [ - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], + localizationsDelegates: GlobalMaterialLocalizations.delegates, supportedLocales: [ Locale('en'), // English Locale('es'), // Spanish @@ -104,19 +109,17 @@ return const MaterialApp( ); ``` -After introducing the `flutter_localizations` package -and adding the previous code, -the `Material` and `Cupertino` -packages should now be correctly localized in -one of the supported locales. -Widgets should be adapted to the localized messages, -along with correct left-to-right or right-to-left layout. +After introducing the `flutter_localizations` package and adding the previous +code, the `Material` and `Cupertino` libraries should now be correctly localized +in one of the supported locales. Widgets should be adapted to the localized +messages, along with correct left-to-right or right-to-left layout. Try switching the target platform's locale to Spanish (`es`) and the messages should be localized. -Apps based on `WidgetsApp` are similar except that the -`GlobalMaterialLocalizations.delegate` isn't needed. +Apps that are not based on `MaterialApp` should instead use +`GlobalCupertinoLocalizations.delegates` or +`flutter_localizations.GlobalWidgetsLocalizations.delegate`. The full `Locale.fromSubtags` constructor is preferred as it supports [`scriptCode`][], though the `Locale` default @@ -128,7 +131,7 @@ The elements of the `localizationsDelegates` list are factories that produce collections of localized values. `GlobalMaterialLocalizations.delegate` provides localized strings and other values for the Material Components -library. `GlobalWidgetsLocalizations.delegate` +library. `flutter_localizations.GlobalWidgetsLocalizations.delegate` defines the default text direction, either left-to-right or right-to-left, for the widgets library. @@ -190,18 +193,29 @@ widget should re-render in Spanish. ### Adding your own localized messages - -After adding the `flutter_localizations` package, -you can configure localization. To add localized text to your application, complete the following instructions: -1. Add the `intl` package as a dependency, pulling - in the version pinned by `flutter_localizations`: +1. Add the package `flutter_localizations` as a dependency to your + `pubspec.yaml` file, as well as the `intl` package, at the version pinned by + `flutter_localizations`: - ```console - $ flutter pub add intl:any - ``` +```console +$ flutter pub add flutter_localizations --sdk=flutter +$ flutter pub add intl:any +``` + +This creates the following entries in the `pubspec.yml` file: + + +```yaml +dependencies: + flutter: + sdk: flutter + flutter_localizations: + sdk: flutter + intl: any +``` 2. Open the `pubspec.yaml` file and enable the `generate` flag. This flag is found in the `flutter` section in the pubspec file. @@ -277,9 +291,7 @@ complete the following instructions: title: 'Localizations Sample App', localizationsDelegates: [ AppLocalizations.delegate, // Add this line - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, + ...GlobalMaterialLocalizations.delegate, ], supportedLocales: [ Locale('en'), // English