aissat / easy_localization

Easy and Fast internationalizing your Flutter Apps
https://pub.dev/packages/easy_localization
MIT License
908 stars 325 forks source link

Need callback finish EasyLocalization -> Load asset from assets/translations #650

Open DmitriySimonov opened 7 months ago

DmitriySimonov commented 7 months ago

easy_localization: ^3.0.4

There is a problem, I need to know at what point the final initialization of Easy Localization will take place.

Why? When I start the application, I create a notification (+background service) and it starts BEFORE the final initialization of Easy Localization, because of this, instead of translations, I see keys....

And to create my notification with translations, I need to know the moment at which translations will be available (finish init EasyLocalization).

изображение

Code example:

main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();

  //await EasyLocalization.ensureInitialized();
  //-------------------
  //LocaleKeys.name.tr(); // <--- Not working!
  // [34m[🌎 Easy Localization] [WARNING] Localization key [name] not found
  //-------------------

  // Need await Widget app = EasyLocalization(...

  Widget app = EasyLocalization(
    supportedLocales: LocalizationConfiguration().supportedLocales,
    path: LocalizationConfiguration().path,
    fallbackLocale: LocalizationConfiguration().fallbackLocale,
    saveLocale: true,
    child: const App(),
  );

  runApp(app);

  /*
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Start\
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Init state
  I/flutter (13538): [🌎 Easy Localization] [INFO] Saved locale loaded ru
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Build
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Init Localization Delegate
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Init provider
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Load Localization Delegate
  I/flutter (13538): [🌎 Easy Localization] [DEBUG] Load asset from assets/translations
   */

  // and only then can we use LocaleKeys.name.tr();
}

flutter doctor

[✓] Flutter (Channel stable, 3.19.0, on Ubuntu 22.04.3 LTS 5.15.0-94-generic, locale ru_RU.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc1)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2023.1)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.2)
[✓] Connected device (3 available)
[✓] Network resources