translateErrorKey method
Translates a given error key to a localized message.
This method attempts to find a translation in the following order:
- Custom translation in the current locale
- Predefined translation in the current locale
- Custom translation in the fallback locale (if set)
- Predefined translation in the fallback locale (if set)
- 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);
}