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

[gen_l10n] Generate static localization keys #67092

Open
twister21 opened this issue Oct 1, 2020 · 2 comments
Open

[gen_l10n] Generate static localization keys #67092

twister21 opened this issue Oct 1, 2020 · 2 comments
Labels
a: internationalization Supporting other languages or locales. (aka i18n) c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project team-framework Owned by Framework team tool Affects the "flutter" command-line tool. See also t: labels. triaged-framework Triaged by Framework team

Comments

@twister21
Copy link

twister21 commented Oct 1, 2020

Use case

My business logic is executed in a separate class (ViewModel/BLoC pattern) that doesn't have access to Flutter's BuildContext. However, I need to pass localized messages back to the UI in a reactive way (via StreamController/PublishSubject).

ViewModel class:

PublishSubject<String> errorMessage = PublishSubject();

void submit() {
  try {
    // Execute business logic
  } catch(e) {
    // Pass error message to subscriber
    errorMessage.add("submission_error");
  }
}

Screen widget:

@override
void initState() {
  super.initState();
 
  // Subscribe to error messages
  _viewModel.errorMessage.listen((message) => _showSnackbar(message));
}

Proposal

The gen_l10n tool should generate static keys which can be resolved into localized messages when the BuildContext is available.

That's how Android basically manages all kinds of resources like strings, dimensions or colors. The resource IDs (automatically generated integer constants) can be referenced from anywhere (R.string.submission_error), but can only be converted into strings within the application context.

// Generated class
class LocaleKey {
  LocaleKey._();

  static String submissionError = "submission_error";
}

Example usage:

errorMessage.add(LocaleKey.submissionError);

...

_viewModel.errorMessage.listen((localeKey) {
    String localizedMessage = AppLocalizations.of(context).t(localeKey);
    _showSnackbar(localizedMessage);
}

Or with an extension function:
context.getString(localeKey)

@twister21 twister21 changed the title [l10n] Generate static localization keys [gen_l10n] Generate static localization keys Oct 1, 2020
@darshankawar darshankawar added a: internationalization Supporting other languages or locales. (aka i18n) c: proposal A detailed proposal for a change to Flutter tool Affects the "flutter" command-line tool. See also t: labels. labels Oct 2, 2020
@MTRNord
Copy link

MTRNord commented Oct 2, 2020

We have a similar issue that might be fixed by this. (If not feel free to tell me to open another issue). We have a dart library where we want to also localize things. Previously this has been done by letting AppLocalizations extend a class inside of the dart library which defines its keys. Now, this is not possible anymore. As its a pure dart lib we cant use BuildContext in there either so either this works for it or if you think I should add another issue about allowing classes that shall be extended that would be fine too.

@HansMuller
Copy link
Contributor

CC @shihaohong

@HansMuller HansMuller added the framework flutter/packages/flutter repository. See also f: labels. label Oct 5, 2020
@jmagman jmagman added this to Awaiting triage in Tools - framework support review via automation Oct 13, 2020
@christopherfujino christopherfujino moved this from Awaiting triage to Engineer reviewed in Tools - framework support review Oct 15, 2020
@goderbauer goderbauer added the P3 Issues that are less important to the Flutter project label Jul 26, 2022
@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-framework Owned by Framework team triaged-framework Triaged by Framework team labels Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: internationalization Supporting other languages or locales. (aka i18n) c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project team-framework Owned by Framework team tool Affects the "flutter" command-line tool. See also t: labels. triaged-framework Triaged by Framework team
Projects
Tools - framework support review
  
Engineer reviewed
Development

No branches or pull requests

7 participants