value method Null safety

T value(
  1. {String? locale}
)

Returns the default value if no locale is provided. If a locale is provided, it returns the corresponding localized value or the default value if the requested locale is not contained in the localized string.

Implementation

T value({String? locale}) {
  if (locale == defaultLocale) {
    return defaultValue;
  }
  // if a locale has been supplied, use that one
  if (locale != null) {
    if (localizedVersions.containsKey(locale)) {
      return localizedVersions[locale]!;
    }
  }

  // otherwise look at the currently set locale through Intl
  // NOTE: This assumes a two-character locale by convetion
  // at the moment. Once we encounter four-character locales
  // (de_CH for example), we need make a two-step lookup
  // including the specific and then the generic variant.
  if (localizedVersions.containsKey(Intl.defaultLocale!)) {
    return localizedVersions[Intl.defaultLocale]!;
  }

  // if all else fails, return the default string
  // (which can be the desired result if Intl.defaultLocale or
  // the supplied locale match the defaultLocale property,
  // it's a desired outcome in all cases)
  return defaultValue;
}