From 3cee5b1bbb939d20034e7493bca88ac3c2e7a513 Mon Sep 17 00:00:00 2001 From: Syed Murtaza <44249868+syedmurtaza108@users.noreply.github.com> Date: Wed, 14 Feb 2024 07:42:44 +0500 Subject: [PATCH] Update android-devs.md (#10102) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _Description of what this PR is changing or adding, and why:_ Updated https://github.com/flutter/website/tree/main/src/get-started/flutter-for/android-devs.md page as it is containing some outdated information. _Issues fixed by this PR (if any):_ https://github.com/flutter/website/issues/10101 ## Presubmit checklist - [x] This PR doesn’t contain automatically generated corrections (Grammarly or similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer. --------- Co-authored-by: Murtaza Hussain --- .../flutter-for/android_devs/l10n.yaml | 3 ++ .../android_devs/lib/arb_examples.arb | 13 ++++++++ .../lib/localization_examples.dart | 14 +++++++++ .../flutter-for/android_devs/pubspec.yaml | 4 +++ src/get-started/flutter-for/android-devs.md | 31 ++++++++++++------- 5 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 examples/get-started/flutter-for/android_devs/l10n.yaml create mode 100644 examples/get-started/flutter-for/android_devs/lib/arb_examples.arb create mode 100644 examples/get-started/flutter-for/android_devs/lib/localization_examples.dart 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