easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

Language not changing

Open mohamed-elmarakby opened this issue 3 years ago • 8 comments

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

mohamed-elmarakby avatar Jan 25 '22 15:01 mohamed-elmarakby

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.

11dj avatar Jan 26 '22 03:01 11dj

Please more details ??!

aissat avatar Mar 01 '22 23:03 aissat

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

kechankrisna avatar Mar 06 '22 17:03 kechankrisna

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.

Faaatman avatar Mar 07 '22 09:03 Faaatman

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.

burakkurtarir avatar Mar 09 '22 11:03 burakkurtarir

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.

tijanirf avatar Apr 26 '22 21:04 tijanirf

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

AlexanderFarkas avatar May 20 '22 14:05 AlexanderFarkas

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

Harits19 avatar Sep 16 '22 07:09 Harits19

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

mauryagaurav947 avatar Feb 10 '23 07:02 mauryagaurav947

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);
  }
}

andreamainella98 avatar Feb 24 '23 11:02 andreamainella98

Duplicate of #552

aissat avatar Feb 24 '23 13:02 aissat