translateErrorKey method

String? translateErrorKey(
  1. FormErrorKey errorKey
)

Translates a given error key to a localized message.

This method attempts to find a translation in the following order:

  1. Custom translation in the current locale
  2. Predefined translation in the current locale
  3. Custom translation in the fallback locale (if set)
  4. Predefined translation in the fallback locale (if set)
  5. The error key string itself

Usage:

final message = localizations.translateErrorKey(PredefinedFormErrorKey(PredefinedFormErrorType.required));

Implementation

String? translateErrorKey(FormErrorKey errorKey) {
  // Try to get translation in current locale
  String? translation =
      _getTranslation(errorKey, _currentLocale.languageCode);

  // If not found and fallback is set, try fallback locale
  if (translation == null && _fallbackLocale != null) {
    translation = _getTranslation(errorKey, _fallbackLocale!.languageCode);
  }

  // If still not found, return the error key
  return translation ?? _getErrorKeyString(errorKey);
}