jiffy
jiffy copied to clipboard
Where to put async locale set
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.
same problem here :/
same problem here :/
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');
.
Thanks a lot for this change. Hopefully it will be approved.
@rivella50 I was able to work around this problem temporarily by slightly delaying the locale() call via
await Future.microtask(() => {Jiffy.locale(localeName)})
.
@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.
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,
]
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.