maybeNameTranslation method

  1. @protected
String? maybeNameTranslation(
  1. T item,
  2. BuildContext context
)

Returns the translated name of an item by searching in order of priority: direct maps, theme PickersThemeData.maps, and global TypedLocaleDelegate translation cache.

Returns null if no translation is found in any source, or if all sources are null.

Asserts in debug mode when a translation is missing from a non-null source, suggesting to add the item to the translation cache or use TypedLocaleDelegate for automatic translations.

Priority order:

  1. Direct maps parameter (highest priority).
  2. Theme PickersThemeData.maps.
  3. Global TypedLocaleDelegate translation cache (lowest priority).

Implementation

@protected
String? maybeNameTranslation(T item, BuildContext context) {
  final direct = maps;
  final theme = context.pickersTheme?.maps;
  final global = context.maybeLocale?.maps;

  if (direct == null && theme == null && global == null) return null;
  String? result;

  if (direct != null) {
    result = nameTranslationCache(item, direct);
    if (result != null) return result;
  }

  if (theme != null) {
    result = nameTranslationCache(item, theme);
    if (result != null) return result;
  }

  if (global != null) {
    result = nameTranslationCache(item, global);
    if (result != null) return result;
  }

  assert(
    direct == null,
    "The $IsoMaps passed to the `maps` parameter in the "
    "$this lacks a translation for item: $item. Verify that the provided "
    "${TranslationMap<T>} translations map includes a key value pair for the "
    "{${item.runtimeType}(): '${item.internationalName} translation'} there."
    " Consider adding `localizationsDelegates: const [TypedLocaleDelegate()]`"
    " in the app to enable device locale-based auto translation maps cache.",
  );

  assert(
    theme == null,
    "The $IsoMaps passed to the `maps` parameter in "
    "$PickersThemeData lacks a translation for item: $item. Verify that the "
    "${TranslationMap<T>} translations map includes a key value pair for the "
    "{${item.runtimeType}(): '${item.internationalName} translation'} there.",
  );

  assert(
    global == null,
    "The $TypedLocaleDelegate passed to the app's `localizationsDelegates` "
    "parameter provided IsoMaps without a translation for item: $item. "
    "Verify that the translation cache includes a key value pair for the "
    "{${item.runtimeType}(): '${item.internationalName} translation'} there.",
  );

  return result;
}