Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to put async locale set #153

Open
rivella50 opened this issue Mar 31, 2022 · 8 comments
Open

Where to put async locale set #153

rivella50 opened this issue Mar 31, 2022 · 8 comments

Comments

@rivella50
Copy link

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.

@nsalleron-omedo
Copy link

same problem here :/

1 similar comment
@hao-li
Copy link

hao-li commented Apr 25, 2022

same problem here :/

@hao-li
Copy link

hao-li commented Apr 25, 2022

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');.

@rivella50
Copy link
Author

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

@dastein1
Copy link

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

@rivella50
Copy link
Author

rivella50 commented Jul 11, 2022

@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.

@dastein1
Copy link

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,
]

@rivella50
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants