jiffy icon indicating copy to clipboard operation
jiffy copied to clipboard

Where to put async locale set

Open rivella50 opened this issue 2 years ago • 8 comments

Hi there, where do i have to put await Jiffy.locale('de'); in order that the locale is set app wide? When inserting in my async main method i get this error: Unsupported operation: Cannot modify unmodifiable map This could be since i init MaterialApp (with localizationsDelegates and supportedLocales) after this call, but this is not async anymore there.

rivella50 avatar Mar 31 '22 13:03 rivella50

same problem here :/

nsalleron-omedo avatar Apr 14 '22 16:04 nsalleron-omedo

same problem here :/

hao-li avatar Apr 25 '22 13:04 hao-li

change pubspec.yaml:

dependencies:
  jiffy:
    git:
      url: https://github.com/hao-li/jiffy.git
      ref: develop

Format Dates and Relative Time will support locale by parameter.

Jiffy('20191016', null, 'de').format('yMMMd') => 'Mi., 16. Okt. 2019'

No need await Jiffy.locale('de');.

hao-li avatar Apr 25 '22 17:04 hao-li

Thanks a lot for this change. Hopefully it will be approved.

rivella50 avatar Apr 25 '22 18:04 rivella50

@rivella50 I was able to work around this problem temporarily by slightly delaying the locale() call via await Future.microtask(() => {Jiffy.locale(localeName)}).

dastein1 avatar Jul 11 '22 07:07 dastein1

@dastein1 Thanks a lot for your input. This call then also is placed inside the main function, right? When adding your line of code there i still get the same error as mentioned above.

rivella50 avatar Jul 11 '22 07:07 rivella50

I created a separate localization-delegate like so:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:jiffy/jiffy.dart';

// for demo only ... depends on your app
const supportedLocales = [
  Locale('en', ''),
  Locale('de', ''),
];

class JiffyLocalizationsDelegate
    extends LocalizationsDelegate<JiffyLocalizations> {
  @override
  @override
  bool isSupported(Locale locale) {
    return supportedLocales
        .map((l) => l.languageCode)
        .contains(locale.languageCode);
  }

  @override
  load(Locale locale) {
    return JiffyLocalizations.load(locale);
  }

  @override
  bool shouldReload(covariant LocalizationsDelegate old) {
    return false;
  }
}

class JiffyLocalizations {
  static Future<JiffyLocalizations> load(Locale locale) async {
    final name = (locale.countryCode?.isEmpty ?? false)
        ? locale.languageCode
        : locale.toString();
    final localeName = Intl.canonicalizedLocale(name);
    // Note: the delegate races against AppLocalizationDelegate
    // both rely on Intl and that may cause some 'Cannot modify unmodifiable map' exception
    await Future.microtask(() => {Jiffy.locale(localeName)});
    return JiffyLocalizations();
  }

  static JiffyLocalizationsDelegate delegate = JiffyLocalizationsDelegate();
}

Then add JiffyLocalizations.delegate in your localizationsDelegates:

localizationsDelegates: [
  // your other localization-delegates
  JiffyLocalizations.delegate,
]

dastein1 avatar Jul 11 '22 11:07 dastein1

That's a nice way of handling it. Thanks for your contribution. I got it working now by placing your one-liner in an async function after MaterialApp has been initialized.

rivella50 avatar Jul 11 '22 11:07 rivella50