Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add point about initializing localization objects on 'Internationalizing Flutter apps' page #6424

Closed
Tracked by #7472
akiross opened this issue Oct 27, 2021 · 1 comment · Fixed by #9916
Closed
Tracked by #7472
Assignees
Labels
e0-minutes Effort: < 60 min fix.bug Needs fix of incorrect copy, code, or visual from.page-issue Reported in a reader-filed concern p2-medium Necessary but not urgent concern. Resolve when possible. t.i18n Relates to internationalization of Flutter apps t.l10n Relates to localizing Flutter text in apps

Comments

@akiross
Copy link

akiross commented Oct 27, 2021

Page URL

https://docs.flutter.dev/accessibility-and-localization/internationalization

Page source

https://github.com/flutter/website/tree/main/src/docs/development/accessibility-and-localization/internationalization.md

Describe the problem

In the page, step 8, it is mentioned that

  1. Use AppLocalizations anywhere in your app. Here, the translated message is used in a Text widget.

but I believe this is a bit misleading, because "anywhere" led me to naively think that this code was valid:

import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() => runApp(MyWidget());

class MyWidget extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
            localizationsDelegates: AppLocalizations.localizationsDelegates,
            supportedLocales: AppLocalizations.supportedLocales,
            home: Text(AppLocalizations.of(context)!.helloWorld));
    }

But it does not, because the localization objects are not yet initialized when the AppLocalizations.of(context)!.helloWorld is evaluated, so things crash with a null exception. It's simple to understand that AppLocalizations.of(context) is null in this instance, but it's not obvious that it is because it is uninitialized.

Expected fix

I believe the documentation could be a little more clear and contextually mention (in point 7 or 8) that the material app has to actually be started to initialize the AppLocalizations and that using AppLocalizations.of(context)!.helloWorld won't work before that.

In retrospective the error is clear, but a minor fix could help avoiding this problem at all.

Additional context

No response

@danagbemava-nc danagbemava-nc added st.triage.triage-team Triage team reviewing and categorizing the issue e0-minutes Effort: < 60 min p2-medium Necessary but not urgent concern. Resolve when possible. and removed st.triage.triage-team Triage team reviewing and categorizing the issue labels Oct 28, 2021
@atsansone atsansone added from.page-issue Reported in a reader-filed concern t.i18n Relates to internationalization of Flutter apps labels May 23, 2023
@atsansone atsansone added fix.bug Needs fix of incorrect copy, code, or visual ltw-triage labels May 30, 2023
@atsansone atsansone changed the title [PAGE ISSUE]: 'Internationalizing Flutter apps' Add point about initializing localization objects on 'Internationalizing Flutter apps' page May 30, 2023
@atsansone atsansone added the t.l10n Relates to localizing Flutter text in apps label Jun 2, 2023
@sfshaza2
Copy link
Contributor

That is an excellent point, @akiross! I have updated the text.

@sfshaza2 sfshaza2 self-assigned this Dec 12, 2023
parlough added a commit that referenced this issue Dec 12, 2023
Fixes #6424

---------

Co-authored-by: Parker Lougheed <parlough@gmail.com>
atsansone pushed a commit to atsansone/website that referenced this issue Dec 12, 2023
Fixes flutter#6424

---------

Co-authored-by: Parker Lougheed <parlough@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e0-minutes Effort: < 60 min fix.bug Needs fix of incorrect copy, code, or visual from.page-issue Reported in a reader-filed concern p2-medium Necessary but not urgent concern. Resolve when possible. t.i18n Relates to internationalization of Flutter apps t.l10n Relates to localizing Flutter text in apps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants