easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

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

Open helloalbin opened this issue 4 years ago • 23 comments

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

helloalbin avatar Jun 28 '21 07:06 helloalbin

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

CaptainDario avatar Jun 28 '21 11:06 CaptainDario

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
        ...
    )
)

CaptainDario avatar Jun 29 '21 13:06 CaptainDario

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

petodavid avatar Jul 02 '21 12:07 petodavid

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

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

helloalbin avatar Jul 03 '21 06:07 helloalbin

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.

sszajbely avatar Jul 06 '21 13:07 sszajbely

@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.

CaptainDario avatar Jul 06 '21 14:07 CaptainDario

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

sszajbely avatar Jul 06 '21 16:07 sszajbely

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

MattyBoy4444 avatar Jul 09 '21 15:07 MattyBoy4444

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!

alibt avatar Jul 12 '21 05:07 alibt

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

alibt avatar Jul 19 '21 12:07 alibt

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.

mateusz-pietras avatar Jul 21 '21 16:07 mateusz-pietras

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

johnnydevvcodes avatar Jul 23 '21 05:07 johnnydevvcodes

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

Silv4n avatar Aug 29 '21 11:08 Silv4n

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

CripyIce avatar Sep 01 '21 13:09 CripyIce

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

FatiMooni avatar Sep 13 '21 11:09 FatiMooni

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

sarimk80 avatar Oct 15 '21 11:10 sarimk80

got the same problem , need help 😞

YouSour avatar Nov 24 '21 06:11 YouSour

same issue any solution ??

abdalazeez1 avatar Jun 30 '22 16:06 abdalazeez1

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

abdalazeez1 avatar Jun 30 '22 16:06 abdalazeez1

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

Caffo17 avatar Jul 06 '22 14:07 Caffo17

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

100bhyun avatar Nov 21 '22 18:11 100bhyun

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

buraktahap avatar Jan 05 '23 14:01 buraktahap

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

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

Alex11144 avatar Feb 28 '23 15:02 Alex11144

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 avatar May 16 '23 07:05 bw-flagship