msayed-net / localize_and_translate

Flutter localization in easy steps
MIT License
52 stars 22 forks source link

UI is partially re-created after calling setNewLanguage. #27

Closed TaranenkoDenis closed 1 year ago

TaranenkoDenis commented 2 years ago

We again ran into the problem that after calling method setNewLanguage(context, newLanguage: newLang, restart: true) part of the UI is updated with new translates, but part is not, including the previous screen in the back stack. If I open a new screen, then it is displayed with a new translation. We managed to fix the problem some time ago, but now it has reappeared.

A rough sketch of the creation of LocalizedApp:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final locale = Preferences().language;
  await translator.init(
    language: locale,
    languagesList: ['en', 'ru'],
    assetsDirectory: 'assets/i18n/',
  );

  runZonedGuarded(
    () => runApp(
      LocalizedApp(
        child: DependenciesWidget( // Providers
          environment: environment,
          child: ConfiguratorWidget( // BlocListeners without using UI staff
              child: BlocListener<LocalizationBloc, LocalizationState>(
                listener: (context, state) {
                  final localeCode = state.language.localizationPackageValue;
                  var newLang = localeCode.length > 2
                      ? localeCode.substring(0, 2)
                      : localeCode;
                  translator.setNewLanguage(
                    context,
                    newLanguage: newLang,
                    restart: true,
                  );
                },
                child: MaterialAppWrapperWidget(),
              ),
          ),
        ),
      ),
    ),
    FirebaseCrashlytics.instance.recordError,
  );
}