Easy_localization: After updating to 1.4.0+ translates don't refresh immediately after setLocale().

Created on 29 Feb 2020  路  6Comments  路  Source: aissat/easy_localization

I have 4 languages in app.
After update on 1.4.0 and 1.4.1 the tr() methods not update the texts after setLocale() is called. Texts updates only after change screen to another. Even if i use StreamBuilder and refresh page by itself texts not changes.

But, when i change (for example) from EN to RU (nothing is updates) but if after that i change language from RU to FR (texts updates to RU but locale setted to FR) and next if change back to EN (text updates to FR). I dont know how explain it clearer.
Maybe a "key" property helps?

P.S. On version 1.3.5 all works fine and languages updates immediately after setLocale().

bug

Most helpful comment

@aissat @danilofuchs I think this issue exists in master also. By removing context from Localization and making it singleton, while it is less verbose and can be used in any place without context, it has the issue of not updating since EasyLocalizationProvider InheritedWidget will not rebuild widgets.

Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state.
https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html

All 6 comments

@aissat @danilofuchs I think this issue exists in master also. By removing context from Localization and making it singleton, while it is less verbose and can be used in any place without context, it has the issue of not updating since EasyLocalizationProvider InheritedWidget will not rebuild widgets.

Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state.
https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html

We should provide a new api for this that can handle translations without context and with context.

String tr(resource, {context}) => context != null ? Localizations.of(context).tr(resource) : Localization.instance.tr(resource);

When context is passed to tr, widgets will rebuild on locale change
and when not passed widgets will not rebuild on locale change.

Or maybe create two apis one with context and one without to avoid confusion and possible mistakes.

@aissat @danilofuchs

@spiritinlife Yes, I noticed it and agreed with you

We should provide a new api for this that can handle translations without context and with context.

String tr(resource, {context}) => context != null ? Localizations.of(context).tr(resource) : Localization.instance.tr(resource);

When context is passed to tr, widgets will rebuild on locale change
and when not passed widgets will not rebuild on locale change.

Or maybe create two apis one with context and one without to avoid confusion and possible mistakes.

@aissat @danilofuchs
@spiritinlife @danilofuchs take a look on this repo flutter_restart

@aissat This can be done with a ValueKey(locale) on EasyLocalization top widget. But this is not nice behaviour, changing locale shouldn't restart the whole widget tree.

We should provide a new api for this that can handle translations without context and with context.

String tr(resource, {context}) => context != null ? Localizations.of(context).tr(resource) : Localization.instance.tr(resource);

When context is passed to tr, widgets will rebuild on locale change
and when not passed widgets will not rebuild on locale change.

Or maybe create two apis one with context and one without to avoid confusion and possible mistakes.

@aissat @danilofuchs

just used context paramete once

Text("title").tr(context: context)
// or 
tr("title",context: context)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdelMahmoudHussein picture AdelMahmoudHussein  路  7Comments

VictorCamargo picture VictorCamargo  路  6Comments

Overman775 picture Overman775  路  6Comments

fatimahossny picture fatimahossny  路  4Comments

LugonjaAleksandar picture LugonjaAleksandar  路  5Comments