diff --git a/examples/get-started/flutter-for/android_devs/l10n.yaml b/examples/get-started/flutter-for/android_devs/l10n.yaml new file mode 100644 index 00000000000..a0b6dee988a --- /dev/null +++ b/examples/get-started/flutter-for/android_devs/l10n.yaml @@ -0,0 +1,3 @@ +arb-dir: lib +template-arb-file: arb_examples.arb +output-localization-file: app_localizations.dart diff --git a/examples/get-started/flutter-for/android_devs/lib/arb_examples.arb b/examples/get-started/flutter-for/android_devs/lib/arb_examples.arb new file mode 100644 index 00000000000..90a585ec989 --- /dev/null +++ b/examples/get-started/flutter-for/android_devs/lib/arb_examples.arb @@ -0,0 +1,13 @@ +{ + "@@locale": "en", + "hello":"Hello {userName}", + "@hello":{ + "description":"A message with a single parameter", + "placeholders":{ + "userName":{ + "type":"String", + "example":"Bob" + } + } + } +} diff --git a/examples/get-started/flutter-for/android_devs/lib/localization_examples.dart b/examples/get-started/flutter-for/android_devs/lib/localization_examples.dart new file mode 100644 index 00000000000..3054760e519 --- /dev/null +++ b/examples/get-started/flutter-for/android_devs/lib/localization_examples.dart @@ -0,0 +1,14 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +class MyWidget extends StatelessWidget { + const MyWidget({super.key}); + + @override + Widget build(BuildContext context) { + return + // #docregion AccessString + Text(AppLocalizations.of(context)!.hello('John')); + // #enddocregion AccessString + } +} diff --git a/examples/get-started/flutter-for/android_devs/pubspec.yaml b/examples/get-started/flutter-for/android_devs/pubspec.yaml index 3a6b41a2df3..828de8280d8 100644 --- a/examples/get-started/flutter-for/android_devs/pubspec.yaml +++ b/examples/get-started/flutter-for/android_devs/pubspec.yaml @@ -11,10 +11,13 @@ environment: dependencies: flutter: sdk: flutter + flutter_localizations: + sdk: flutter cupertino_icons: ^1.0.6 http: ^1.1.0 shared_preferences: ^2.2.0 + intl: any dev_dependencies: example_utils: @@ -23,4 +26,5 @@ dev_dependencies: sdk: flutter flutter: + generate: true uses-material-design: true diff --git a/src/get-started/flutter-for/android-devs.md b/src/get-started/flutter-for/android-devs.md index a87ffb0b3e2..54d499d92f8 100644 --- a/src/get-started/flutter-for/android-devs.md +++ b/src/get-started/flutter-for/android-devs.md @@ -1312,28 +1312,36 @@ Widget build(BuildContext context) { ### Where do I store strings? How do I handle localization? Flutter currently doesn't have a dedicated resources-like system for strings. -At the moment, the best practice is to hold your copy text in a class as -static fields and accessing them from there. For example: - - -```dart -class Strings { - static String welcomeMessage = 'Welcome To Flutter'; +The best and recommended practice is to hold your strings in a `.arb` file as key-value pairs For example: + + +```arb +{ + "@@locale": "en", + "hello":"Hello {userName}", + "@hello":{ + "description":"A message with a single parameter", + "placeholders":{ + "userName":{ + "type":"String", + "example":"Bob" + } + } + } } ``` Then in your code, you can access your strings as such: - + ```dart -Text(Strings.welcomeMessage); +Text(AppLocalizations.of(context)!.hello('John')); ``` Flutter has basic support for accessibility on Android, though this feature is a work in progress. -Flutter developers are encouraged to use the -[intl package][] for internationalization and localization. +See [Internationalizing Flutter apps][] for more information on this. ### What is the equivalent of a Gradle file? How do I add dependencies? @@ -2413,3 +2421,4 @@ see the [`firebase_messaging`][] plugin documentation. [SQFlite]: {{site.pub}}/packages/sqflite [StackOverflow]: {{site.so}}/questions/44396075/equivalent-of-relativelayout-in-flutter [widget catalog]: {{site.url}}/ui/widgets/layout +[Internationalizing Flutter apps]: {{site.url}}/ui/accessibility-and-internationalization/internationalization