MessageSourceAccessor class

A wrapper around a MessageSource that remembers a locale, so that lookups need not pass it every time.

Use it where all messages are in one language chosen once, such as a command-line tool or a screen built for the user's language. A lookup can still pass its own locale.

// German by default, English on request.
final messages = MessageSourceAccessor(context, defaultLocale: 'de');
messages.getMessage('greeting', args: ['Ada']); // Hallo Ada
messages.getMessage('greeting', args: ['Ada'], locale: 'en'); // Hello Ada

Spring (Java):

var messages = new MessageSourceAccessor(context, Locale.GERMAN);
messages.getMessage("greeting", new Object[] {"Ada"});

Unlike Spring there is no LocaleContextHolder: without a defaultLocale the message source decides (its base messages and fallback locale).

Constructors

MessageSourceAccessor(MessageSource messageSource, {String? defaultLocale})
Wraps messageSource; defaultLocale is used when a lookup passes no locale.

Properties

defaultLocale → String?
The locale of lookups that pass none; null leaves the choice to the message source (its fallback locale and base messages).
final
hashCode → int
The hash code for this object.
no setterinherited
messageSource → MessageSource
The source the lookups go to.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

getMessage(String code, {List<Object?> args = const [], String? defaultMessage, String? locale}) → String
The text for code in locale, else in defaultLocale; otherwise the same as MessageSource.getMessage.
getMessageFromResolvable(MessageSourceResolvable resolvable, {String? locale}) → String
The text for resolvable in locale, else in defaultLocale; otherwise the same as MessageSource.getMessageFromResolvable.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited