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

BuildContext locale and setLocale #37

Closed
leoshusar opened this issue Oct 2, 2021 · 3 comments
Closed

BuildContext locale and setLocale #37

leoshusar opened this issue Oct 2, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@leoshusar
Copy link

Hi! I am using this package for my app but I also have older package with localization using intl - like MyPackageLocalizations.of(context).blah.
I have noticed that when I use LocaleSettings.setLocale(), it doesn't set context's locale and so strings provided by that package are not translated.
Confirmed when I set MaterialApp(locale: myLocale), it works.

Do I need to set context's locale by myself?

@Tienisto Tienisto added the bug Something isn't working label Oct 3, 2021
@Tienisto
Copy link
Member

Tienisto commented Oct 3, 2021

I played around a little bit. This should work:

void main() {
  // wrap your app with generated TranslationProvider
  runApp(TranslationProvider(child: MyApp()));
}
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    Translations.of(context); // this will trigger a rebuild of current widget on locale change
    return MaterialApp(
      locale: LocaleSettings.currentLocale.flutterLocale, // which rerenders the MaterialApp with updated locale
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: LocaleSettings.supportedLocales,
      home: MyHomePage(),
    );
  }
}

I will look for a more elegant solution. I am open for any ideas :)

Edit 1: Yeah, it seems that you have to manage it yourself (by adding the locale attribute to MaterialApp)

@leoshusar
Copy link
Author

Thanks! I tried to put Translations.of(context) before the MaterialApp and it works - but not exactly 100 % for my app.

For context: I have my own page manager and the pages are defined like this:

class UsersPage extends StatefulWidget with NamedPage {
  const UserRolesPage({
    Key? key,
  }) : super(key: key);

  @override
  _UserRolesPageState createState() => _UserRolesPageState();

  @override
  String get header => i18n.pages.users.header;
}

and then I have list of those pages and I feed them into TabBarView or PageView etc. And what doesn't get translated are those headers, when I create TabBar like this:

TabBar(
  controller: tabController,
  tabs: pages.map((p) => Tab(text: p.header)).toList(),
),

It got translated when I put Translations.of(context) to that Scaffold too.

But I found this kinda hack answer on SO, where you can mark all children elements for rebuild.
So I have created Translatable wrapper around MaterialApp which has the method from SO and now when I do

onPressed: () {
  LocaleSettings.setLocale(AppLocale.cs);
  Translatable.of(context).rebuildAllChildren();
},

everything gets translated!

I don't know if you'd actually like to implement these "hacks" in your library :) I just wanted to share what I've found.

@Tienisto
Copy link
Member

Tienisto commented Oct 3, 2021

It got translated when I put Translations.of(context) to that Scaffold too.

Yep, if you expect the user to change the locale, then it is a better practice to get the translations via Translations.of.
You only need to use this method for widgets that are visible during locale change. But of course, you can use your "hack" to rebuild all widgets :)

final t = Translations.of(context);

return Column(
  children: [
    Text(t.myPage.title),
    Text(t.myPage.subTitle),
  ]
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants