withLocale<T> static method

dynamic withLocale<T>(
  1. String? locale,
  2. 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));


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


    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});