Dropsource / monarch

Monarch is a tool for building Flutter widgets in isolation. It makes it easy to build, test and debug complex UIs.
https://monarchapp.io
MIT License
437 stars 22 forks source link

How to use with EasyLocalizations? #18

Closed vHanda closed 3 years ago

vHanda commented 3 years ago

I use Easy Localizations in my project. The LocalizationDelegate is created dynamically. How do use monarch in this case?

fertrig commented 3 years ago

We are looking into this. We should have an update later today or tomorrow.

fertrig commented 3 years ago

To use easy_localization with Monarch you have to implement your own LocalizationsDelegate. In its load method you would then load your translations, and then use those translations to load the Localization.instance that easy_localization uses.

Example:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:monarch_annotations/monarch_annotations.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization/src/localization.dart';
import 'package:easy_localization/src/translations.dart';

class MyEasyLocalizationsDelegate extends LocalizationsDelegate<Localization> {
  const MyEasyLocalizationsDelegate();

  @override
  Future<Localization> load(Locale locale) async {
    var assetLoader = const RootBundleAssetLoader();
    // or use your own asset loader

    var data = await assetLoader.load('assets/translations', locale);
    var translations = Translations(data);

    Localization.load(locale, translations: translations);
    return Localization.instance;
  }

  @override
  bool shouldReload(covariant LocalizationsDelegate<Localization> old) => false;

  @override
  bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode);
}

@MonarchLocalizations([MonarchLocale('en', 'US'), MonarchLocale('es')])
const myEasyLocalizationsDelegate = MyEasyLocalizationsDelegate();

You can then run Monarch and you should see the locales you declared in the MonarchLocalizations annotation in the Monarch UI.

We'll create a sample repo with the code above so you can see the whole project.

fertrig commented 3 years ago

Here is a sample project using easy_localization and Monarch:

https://github.com/Dropsource/monarch_samples/tree/master/easy_localization_sample

vHanda commented 3 years ago

Thank you. This works great!