MessageSourceAware class abstract interface

🫘 Interface for components that need access to a MessageSource.

The MessageSourceAware interface enables components to access internationalization (i18n) and localization services for resolving parameterized messages in different locales.

Key Features:

  • Message Resolution: Resolve message codes to localized strings
  • Parameter Support: Handle parameterized messages with placeholders
  • Locale Awareness: Support multiple languages and regions
  • Fallback Handling: Graceful degradation when messages are missing

Framework Integration:

  • Called during component initialization when a MessageSource is available
  • The MessageSource may be null if no message source is configured
  • Typically configured via the ApplicationContext's message source

Example Usage:

@Component
class InternationalizedService implements MessageSourceAware {
  MessageSource? _messageSource;

  @override
  void setMessageSource(MessageSource? messageSource) {
    _messageSource = messageSource;
  }

  String getWelcomeMessage(String username, Locale locale) {
    return _messageSource?.getMessage(
      'welcome.message',
      args: [username],
      locale: locale,
      defaultMessage: 'Welcome, $username!'
    ) ?? 'Welcome, $username!';
  }

  String getErrorMessage(String errorCode, Locale locale) {
    return _messageSource?.getMessage(
      'error.$errorCode',
      locale: locale,
      defaultMessage: 'An error occurred.'
    ) ?? 'An error occurred.';
  }
}

Best Practices:

  • Always provide sensible default messages
  • Handle null MessageSource gracefully in production code
  • Use consistent message code naming conventions
  • Consider locale fallback chains for better user experience

See also:

Properties

hashCode β†’ int
The hash code for this object.
no setterinherited
runtimeType β†’ Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) β†’ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setMessageSource(MessageSource? messageSource) β†’ void
Sets the MessageSource that this component can use for message resolution.
toString() β†’ String
A string representation of this object.
inherited

Operators

operator ==(Object other) β†’ bool
The equality operator.
inherited