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

[🌎 Easy Localization] [WARNING] Localization key [key] not found #394

Closed
helloalbin opened this issue Jun 28, 2021 · 25 comments · May be fixed by #618
Closed

[🌎 Easy Localization] [WARNING] Localization key [key] not found #394

helloalbin opened this issue Jun 28, 2021 · 25 comments · May be fixed by #618
Labels
duplicate This issue or pull request already exists waiting for customer response Waiting for customer response

Comments

@helloalbin
Copy link

helloalbin commented Jun 28, 2021

easy_localization: ^3.0.0

I have migrated my application to NullSafety and I have upgraded to the latest version of easy_localization. After the migration I am getting the following error in my console and the application is not displaying the values for my keys anymore. For every key in my json file I am getting the following error

[🌎 Easy Localization] [WARNING] Localization key [key] not found

Please note that this was working fine before the migration when I was using version 2.3.2.

My code is give below.


import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

  final _navigatorKey = GlobalKey<NavigatorState>();

  runApp(EasyLocalization(
    supportedLocales: [
      const Locale.fromSubtags(languageCode: "en"),
      const Locale.fromSubtags(languageCode: "fr"),
      const Locale.fromSubtags(languageCode: 'de'), 
    ],
    path: 'assets/lang',
    fallbackLocale: Locale.fromSubtags(languageCode: 'en'),
    startLocale: Locale.fromSubtags(languageCode: 'en'),
    child: MultiProvider(
      providers: [],
      child: MaterialApp(
        navigatorKey: _navigatorKey,
        //   home: SplashScreen(),
      ),
    ),
  ));
}

Startup log statements.
[🌎 Easy Localization] [DEBUG] Localization initialized
[🌎 Easy Localization] [DEBUG] Start
[🌎 Easy Localization] [DEBUG] Init state
[🌎 Easy Localization] [INFO] Start locale loaded en
[🌎 Easy Localization] [DEBUG] Build
[🌎 Easy Localization] [DEBUG] Init Localization Delegate
[🌎 Easy Localization] [DEBUG] Init provider
[🌎 Easy Localization] [WARNING] Localization key [select_your_language] not found
[🌎 Easy Localization] [WARNING] Localization key [next_step] not found

@CaptainDario
Copy link

I am getting the same error. However not for all of my translations only for a few of them.

@CaptainDario
Copy link

CaptainDario commented Jun 29, 2021

For me the problem actually was that I was trying to access the localization before it was initialized.
It has to be run after

runApp(
    EasyLocalization(
        
        ...
        Use localization here
        ...
    )
)

@petodavid
Copy link

I'm still experiencing the same issue, could you please provide a full code solution?

@helloalbin
Copy link
Author

@petodavid Please check the following repo for the full code.

https://github.com/helloalbin/flutter_app/tree/easy_localization

@machiato32
Copy link

I had the same issue, it seems like the EasyLocalization.ensureInitialized() does not wait for all initialization to complete (I await it and it does not work).
My workaround solution to wait for 1 second (with Future.delayed().then) before using the EasyLocalization plugin, this time allows the files to be initialized too.

@CaptainDario
Copy link

@machiato32 Yes, that is what i figured out too.
I moved my code which depends on the localization to a later point in my execution orde and now it is working for me.

@machiato32
Copy link

But this is definitely a bug, which needs to be fixed.

@MattyBoy4444
Copy link

Same problem. await ensureInitialized is the first line in main.

@alibt
Copy link

alibt commented Jul 12, 2021

I get the same error too.
i'm using easy_localization: ^3.0.0
this is my main function:
`void main() async {
WidgetsFlutterBinding.ensureInitialized();

await EasyLocalization.ensureInitialized();
runApp(
EasyLocalization(
supportedLocales: [Locale('fa', 'IR')],
path: 'assets/translation',
fallbackLocale: Locale('fa', 'IR'),
startLocale: Locale('fa', 'IR'),
child: MultiProvider(
child: MyApp(),
providers: providers,
)),
);
}`

here is the output of flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.2, on Mac OS X 10.15.7 19H15 darwin-x64, locale en-IR)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.58.0)
[✓] Connected device (1 available)

• No issues found!

@Overman775 Overman775 added the bug Something isn't working label Jul 15, 2021
@alibt
Copy link

alibt commented Jul 19, 2021

solved it by adding "await" before EasyLocalizationController.initEasyLocation(); on easy_localization_app.dart line 106. i'll request the merge.

alibt pushed a commit to alibt/easy_localization that referenced this issue Jul 19, 2021
@Xezolpl
Copy link

Xezolpl commented Jul 21, 2021

solved it by adding "await" before EasyLocalizationController.initEasyLocation(); on easy_localization_app.dart line 106. i'll request the merge.

Does not work for me. How awaiting a function that returns a Future would help in awaiting it? You still have to await that future so its still that same future.

@johnnydevvcodes
Copy link

I solved it by adding the localization attributes in the MaterialApp() as stated in the readme on pub dev.



import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:easy_localization/easy_localization.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  
  runApp(
    EasyLocalization(
      supportedLocales: [Locale('en', 'US'), Locale('de', 'DE')],
      path: 'assets/translations', // <-- change the path of the translation files 
      fallbackLocale: Locale('en', 'US'),
      child: MyApp()
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      home: MyHomePage()
    );
  }
}

Overman775 added a commit that referenced this issue Jul 25, 2021
will solve issue #394 (initialization problem on V3.0.0)
@Silv4n
Copy link

Silv4n commented Aug 29, 2021

I have the same issue, unfortunatly none of the above proposed changes worked out for me.

@CripyIce
Copy link

CripyIce commented Sep 1, 2021

Same here.. None of the above proposed changes worked out for me.

@FatiMooni
Copy link

The same issue, none of the above-suggested answers worked!

@sarimk80
Copy link

If anyone still having this issue for csv files remove the easy_localization_loader. from pubspec and create a new file

import 'dart:developer';

import 'package:csv/csv.dart';
import 'package:csv/csv_settings_autodetection.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;


class CsvAssetLoader extends AssetLoader {
  CSVParser? csvParser;

  @override
  Future<Map<String, dynamic>> load(String path, Locale locale) async {
    if (csvParser == null) {
      log('easy localization loader: load csv file $path');
      csvParser = CSVParser(await rootBundle.loadString(path));
    } else {
      log('easy localization loader: CSV parser already loaded, read cache');
    }
    return csvParser!.getLanguageMap(locale.toString());
  }
}

class CSVParser {
  final String fieldDelimiter;
  final String strings;
  final List<List<dynamic>> lines;

  static final csvSettingsDetector =
      FirstOccurrenceSettingsDetector(eols: ['\r\n', '\n']);

  CSVParser(this.strings, {this.fieldDelimiter = ','})
      : lines = CsvToListConverter(csvSettingsDetector: csvSettingsDetector)
            .convert(strings, fieldDelimiter: fieldDelimiter);

  List getLanguages() {
    return lines.first.sublist(1, lines.first.length);
  }

  Map<String, dynamic> getLanguageMap(String localeName) {
    final indexLocale = lines.first.indexOf(localeName);

    var translations = <String, dynamic>{};
    for (var i = 1; i < lines.length; i++) {
      translations.addAll({lines[i][0]: lines[i][indexLocale]});
    }
    return translations;
  }
}

@abdalazeez1
Copy link

same issue any solution ??

@abdalazeez1
Copy link

i solved problem by delete all file generated and execute
flutter pub run easy_localization:generate --source-dir ./translations

flutter pub run easy_localization:generate
flutter pub run easy_localization:generate --source-dir 'translations' --output-dir 'lib/generated' --output-file 'translations.g.dart' --format keys

@Caffo17
Copy link

Caffo17 commented Jul 6, 2022

I solved wrapping the MaterialApp's home with a Builder

return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      home: Builder(
        builder: (context) {
          return MyWidget();
        },
      ),
);        

I think the problem is related to the BuildContext passed in the build method that hasn't registered yet the localization delegates of the MaterialApp. In this way the Builder use a context child of the MaterialApp

@100bhyun
Copy link

holy moly.. I solved it by adding '/' at the end of assets/translations...

@buraktahap
Copy link

I am still having this issue. Did anybody found a solution?

@Alex11144
Copy link

 I have added this line into EasyLocalization(after runApp) and everything worked : 

startLocale: Locale.fromSubtags(languageCode: 'en'),

@bw-flagship
Copy link
Collaborator

I think a lot of very different problems are merged in this issue. A lot of them will be solved by the solutions of @Caffo17 and @johnsargado. However, if someone still has this problem, please comment here and code, so maintainers can reproduce it.

@bw-flagship bw-flagship added waiting for customer response Waiting for customer response and removed bug Something isn't working labels May 16, 2023
@aissat aissat linked a pull request Sep 10, 2023 that will close this issue
3 tasks
@aissat aissat added the duplicate This issue or pull request already exists label Sep 10, 2023
@ObidjonJoraboyev
Copy link

I solved it by adding the localization attributes in the MaterialApp() as stated in the readme on pub dev.



import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:easy_localization/easy_localization.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  
  runApp(
    EasyLocalization(
      supportedLocales: [Locale('en', 'US'), Locale('de', 'DE')],
      path: 'assets/translations', // <-- change the path of the translation files 
      fallbackLocale: Locale('en', 'US'),
      child: MyApp()
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      home: MyHomePage()
    );
  }
}

thank you bro ,me too

@officialismailshah
Copy link

 I have added this line into EasyLocalization(after runApp) and everything worked : 

startLocale: Locale.fromSubtags(languageCode: 'en'),

this worked for me but none of the above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists waiting for customer response Waiting for customer response
Projects
None yet
Development

Successfully merging a pull request may close this issue.