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

Language not changing #448

Closed
mohamed-elmarakby opened this issue Jan 25, 2022 · 11 comments
Closed

Language not changing #448

mohamed-elmarakby opened this issue Jan 25, 2022 · 11 comments

Comments

@mohamed-elmarakby
Copy link

I've went through other issues and this isn't like #370.

Application changes the direction of the language and the locale gets changed but the words of the application don't get changed they remain on the same language until I restart the application myself, it was working fine on the ^2.3.3 version before null safety and it changed instantly now I don't know what happened!

I've tried it on a normal application, using provider, and using bloc and it's the same issue

@11dj
Copy link

11dj commented Jan 26, 2022

I got same issue too.
In my case I want to change language from model class by passing context from StatefulWidget to Model class to call context.locale. And it doesn't work.

@aissat
Copy link
Owner

aissat commented Mar 1, 2022

Please more details ??!

@kechankrisna
Copy link

Please test with MaterialApp.router, you will see the language not refresh in UI when we change the language. It works fine in MaterialApp but not MaterialApp.router

@Faaatman
Copy link

Faaatman commented Mar 7, 2022

The solution that worked for me is the following:
My app has a bottom navigation bar where the tabs are in IndexedStack I was supplying the IndexedStack children list with const

children: [
  const ProfilePage();
  const HomePage();
]

The fix is to remove the const as follows:

children: [
    // ignore: prefer_const_constructors
   ProfilePage();
    // ignore: prefer_const_constructors
   HomePage();
]

Every other widget inside HomePage or ProfilePage can be const.

@burakkurtarir
Copy link

The solution that worked for me is the following: My app has a bottom navigation bar where the tabs are in IndexedStack I was supplying the IndexedStack children list with const

children: [
  const ProfilePage();
  const HomePage();
]

The fix is to remove the const as follows:

children: [
    // ignore: prefer_const_constructors
   ProfilePage();
    // ignore: prefer_const_constructors
   HomePage();
]

Every other widget inside HomePage or ProfilePage can be const.

Yes you are awesome, saved my day, thank you.

@tijanirf
Copy link

The solution that worked for me is the following: My app has a bottom navigation bar where the tabs are in IndexedStack I was supplying the IndexedStack children list with const

children: [
  const ProfilePage();
  const HomePage();
]

The fix is to remove the const as follows:

children: [
    // ignore: prefer_const_constructors
   ProfilePage();
    // ignore: prefer_const_constructors
   HomePage();
]

Every other widget inside HomePage or ProfilePage can be const.

Do we have better workaround so we don't need to remove the const keyword? Yes, i am aware of what const does but it is kind of hard to slice the widget if it is so deep in the widget tree. With current default linter settings, it's kind of odd to ignore it manually as it is not intuitive at least for me.

@AlexanderFarkas
Copy link

Easy Localization uses bad practice - it doesn't utilize BuildContext. That's why your translations will never be updated if they are const.

@Harits19
Copy link

The solution that worked for me is the following: My app has a bottom navigation bar where the tabs are in IndexedStack I was supplying the IndexedStack children list with const

children: [
  const ProfilePage();
  const HomePage();
]

The fix is to remove the const as follows:

children: [
    // ignore: prefer_const_constructors
   ProfilePage();
    // ignore: prefer_const_constructors
   HomePage();
]

Every other widget inside HomePage or ProfilePage can be const.

Do we have better workaround so we don't need to remove the const keyword? Yes, i am aware of what const does but it is kind of hard to slice the widget if it is so deep in the widget tree. With current default linter settings, it's kind of odd to ignore it manually as it is not intuitive at least for me.

don't forget to delete the 'const' on MaterialApp child/home widget if it exists

@mauryagaurav947
Copy link

Same issue context.setLocale does not effect. Please help.

@andreamainella98
Copy link

andreamainella98 commented Feb 24, 2023

To update the translations, since Easy_Localization doesn't use the context, just update the context of the EasyLocalization instance.

With this solution I managed to fix this problem.

My Code:

Padding(
  padding: const EdgeInsets.all(18.0),
  child: DropdownButton(
    items: context.supportedLocales
        .map(
          (e) => DropdownMenuItem(
            value: e,
            child: Text(
              context.localizables.localeTitle(e.languageCode),
              style: context.styles.h2Lato,
            ),
          ),
        )
        .toList(),
    value: context.locale,
    onChanged: (value) => value != null ? context.setLocale(value) : null,
  ),
),
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';

extension LanguageLoader on BuildContext {
  Localizable get localizables => Localizable(this);
}

class Localizable {
  final BuildContext context;

  Localizable(this.context);

  String get appTitle => ltr('appTitle');

  String get yes => ltr('yes');

  String get no => ltr('no');

  String get quoteAnime => ltr('quoteAnime');

  String darkModeTitle(bool enabled) => ltr('darkModeTitle', args: [enabled ? yes : no]);

  String localeTitle(String locale) => ltr('locale.$locale');

  String ltr(
    String key, {
    List<String>? args,
    Map<String, String>? namedArgs,
    String? gender,
  }) {
    EasyLocalization.of(context);
    return tr(key, args: args, namedArgs: namedArgs, gender: gender);
  }
}

aissat added a commit that referenced this issue Feb 24, 2023
@aissat
Copy link
Owner

aissat commented Feb 24, 2023

Duplicate of #552

@aissat aissat marked this as a duplicate of #552 Feb 24, 2023
@aissat aissat closed this as completed Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests