getMessageSource method

  1. @override
MessageSource getMessageSource()
override

Returns the configured MessageSource.

Provides access to the internationalization infrastructure for resolving localized messages throughout the application.

Throws:

  • IllegalStateException if the message source has not been initialized

Example:

// Access message source directly
final messages = context.getMessageSource();

// Resolve messages with different strategies
final welcomeMsg = messages.getMessage(
  'user.welcome',
  args: ['John'],
  locale: Locale('en', 'US')
);

final errorMsg = messages.getMessage(
  'validation.email.invalid',
  defaultMessage: 'Please enter a valid email address'
);

// Use in exception messages
throw ValidationException(
  messages.getMessage('error.validation.failed')
);

// Use in logging with localization
logger.info(messages.getMessage('app.startup.complete'));

Implementation

@override
MessageSource getMessageSource() {
  if(_messageSource == null) {
    throw IllegalStateException("Cannot access message source since it has not been initialized yet.");
  }

  return _messageSource!;
}