withLocale<T> method
Null safety
- String? locale,
- T function(
Run function
with the default locale set to locale
and
return the result.
This is run in a zone, so async operations invoked
from within function
will still have the locale set.
In simple usage function
might be a single
Intl.message()
call or number/date formatting operation. But it can
also be an arbitrary function that calls multiple Intl operations.
For example
Intl.withLocale('fr', () => NumberFormat.format(123456));
or
hello(name) => Intl.message(
'Hello $name.',
name: 'hello',
args: [name],
desc: 'Say Hello');
Intl.withLocale('zh', Timer(Duration(milliseconds:10),
() => print(hello('World')));
Implementation
static dynamic withLocale<T>(String? locale, T Function() function) {
// TODO(alanknight): Make this return T. This requires work because T might
// be Future and the caller could get an unawaited Future. Which is
// probably an error in their code, but the change is semi-breaking.
var canonical = Intl.canonicalizedLocale(locale);
return runZoned(function, zoneValues: {#Intl.locale: canonical});
}