regionString property

String get regionString

A normalized region string you can feed into NumberFormat:

  • language-only if no countryCode
  • lowercased countryCode if present
  • guaranteed to be a locale that NumberFormat knows (falls back to first matching symbol or to 'en')

Implementation

static String get regionString {
  // 1) pick app locale via navigatorKey if available
  final context = BasfLogicLocalizationUtils.appNavigatorKey.currentContext;

  Locale locale = context != null
      ? Localizations.localeOf(context)
      : PlatformDispatcher.instance.locale;

  // 2) clamp to supported (else default to English)
  if (!BasfLogicLocalizations.supportedLocales.any(
    (l) => l.languageCode == locale.languageCode,
  )) {
    locale = const Locale('en');
  }

  // 3) derive region: prefer countryCode, else languageCode
  String region = locale.countryCode?.toLowerCase() ?? locale.languageCode;

  // 4) ensure NumberFormat knows it — otherwise try to find a close match
  if (!NumberFormat.localeExists(region)) {
    region = numberFormatSymbols.keys
        .firstWhere(
          (loc) => loc.toLowerCase().contains(region),
          orElse: () => '',
        )
        .split('_')
        .first;

    if (region.isEmpty) {
      region = locale.languageCode;
    }
  }

  return region;
}