withLocale method

dynamic withLocale (String locale, dynamic 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", () => new NumberFormat.format(123456));

or

  hello(name) => Intl.message(
      "Hello $name.",
      name: 'hello',
      args: [name],
      desc: 'Say Hello');
  Intl.withLocale("zh", new Timer(new Duration(milliseconds:10),
      () => print(hello("World")));

Implementation

static withLocale(String locale, function()) {
  var canonical = Intl.canonicalizedLocale(locale);
  return runZoned(function, zoneValues: {#Intl.locale: canonical});
}