dart-lang / i18n

A general mono-repo for Dart i18n and l10n packages.
BSD 3-Clause "New" or "Revised" License
64 stars 39 forks source link

Unhandled Exception: Invalid argument(s): Invalid locale "yue_Hant_HK" #752

Open louislamlam opened 11 months ago

louislamlam commented 11 months ago

Which locale is missing? yue_Hant_HK

Which locale have you considered of using instead, but was not sufficient? Nothing

Error appears when try calling NumberFormat.compact() in iOS 17

`[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Invalid locale "yue_Hant_HK"

0 _throwLocaleError (package:intl/src/intl_helpers.dart:204:3)

1 verifiedLocale (package:intl/src/intl_helpers.dart:198:42)

2 verifiedLocale (package:intl/src/intl_helpers.dart:183:12)

3 new DateFormat (package:intl/src/intl/date_format.dart:267:27)`

kinex commented 1 month ago

Same crash e.g. with "sco_AU" and "nap_IT".

Could you please provide a function like "isLocaleSupported" to avoid these unnecessary crashes. We could then fall back to some default locale on app startup if needed.

I do this on app startup currently:

    Locale locale = WidgetsBinding.instance.platformDispatcher.locale;

    Intl.systemLocale = locale.toString();
    // workaround to https://github.com/dart-lang/intl/issues/280
    Intl.defaultLocale ??= Intl.systemLocale;

And would like to change it to something like:

    Locale locale = WidgetsBinding.instance.platformDispatcher.locale;

    // Check if the locale is supported, otherwise use a fallback
    if (!Intl.isLocaleSupported(locale)) {
      locale = Locale('en', 'US'); // Fallback locale
    }

    Intl.systemLocale = locale.toString();
    // workaround to https://github.com/dart-lang/intl/issues/280
    Intl.defaultLocale ??= Intl.systemLocale;
mosuem commented 1 month ago

Additionally to missing supportedLocales, package:intl also doesn't have great fallback logic. If it's any help, both of these features are present in the experimental package:intl4x.