withLocale<T> static method

Future<T> withLocale<T>(
  1. String locale,
  2. Future<T> callback()
)

Execute code with a temporary locale.

Example:

await NyTest.withLocale('es', () async {
  expect('hello'.tr(), 'hola');
});

Implementation

static Future<T> withLocale<T>(
  String locale,
  Future<T> Function() callback,
) async {
  _originalLocale = NyLocalization.instance.languageCode;
  await NyLocalization.instance.setLocale(locale: Locale(locale));
  try {
    return await callback();
  } finally {
    if (_originalLocale != null) {
      await NyLocalization.instance.setLocale(
        locale: Locale(_originalLocale!),
      );
    }
  }
}