withLocale<T> static method
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!),
);
}
}
}