StaticMessageSource class

A message source whose messages are added in code rather than read from files.

Use it in tests, or in a small program with a handful of texts. Declare it as the messageSource bean, or pass it where a MessageSource is expected.

// A base text and a Russian one; ru_RU falls back to ru.
final messages = StaticMessageSource()
  ..addMessage('greeting', '', 'Hello {0}')
  ..addMessages({'greeting': 'Привет {0}'}, 'ru');
messages.getMessage('greeting', args: ['Ada'], locale: 'ru_RU');
// Привет Ada

Spring (Java):

var messages = new StaticMessageSource();
messages.addMessage("greeting", Locale.ROOT, "Hello {0}");
messages.addMessage("greeting", Locale.of("ru"), "Привет {0}");
Inheritance

Constructors

StaticMessageSource()

Properties

alwaysUseMessageFormat ↔ bool
Whether messages without arguments are formatted too, so that their quotes follow MessageFormat rules ('' becomes '); false by default, which returns them as written. When it is true, an invalid pattern throws a FormatException instead of being returned as is.
getter/setter pairinherited
commonMessages ↔ Map<String, String>?
Messages that are the same in every language, by code; tried after all locales and before the parent.
getter/setter pairinherited
defaultLocale ↔ String
The locale tried after the requested one (Spring's default locale), for example the language most texts are written in; empty means none (see fallbackLocale).
getter/setter pairinherited
fallbackLocale → String
The locale tried when the requested one has no message ('' for none): defaultLocale here. ReloadableResourceBundleMessageSource uses the system locale when there is no default locale, as Spring's AbstractResourceBasedMessageSource.getDefaultLocale().
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
parentMessageSource ↔ MessageSource?
The source asked for codes this one does not have; null for none.
getter/setter pairinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
useCodeAsDefaultMessage ↔ bool
Whether an unknown code is returned as the message instead of throwing a NoSuchMessageException; false by default. Handy while translations are incomplete: the screen shows the code.
getter/setter pairinherited

Methods

addMessage(String code, String locale, String message) → void
Adds message for code in locale ('' for the base, used when no locale matches); a second call for the same code and locale replaces it.
addMessages(Map<String, String> messages, String locale) → void
Adds every entry of messages (code to message) for locale.
formatMessage(String pattern, List<Object?> args, [String? locale]) → String
Fills args into pattern with MessageFormat; without arguments (and with alwaysUseMessageFormat off) returns pattern as it is, like Spring.
inherited
getMessage(String code, {List<Object?> args = const [], String? defaultMessage, String? locale}) → String
The text for code in locale, with args filled into its {0}, {1}, ... placeholders.
inherited
getMessageFromResolvable(MessageSourceResolvable resolvable, {String? locale}) → String
The text for the first code of resolvable found in locale, with its arguments filled in; else its default message; else a NoSuchMessageException. Use it to show validation errors and other messages described by codes.
inherited
getMessageInternal(String code, List<Object?> args, String? locale) → String?
The formatted message for code from this source, its common messages or its parent; null when none has it. Unlike getMessage, it neither throws nor uses a default message.
inherited
localeChain(String? locale) → List<String>
The locales tried for locale: its candidates, then those of fallbackLocale, ending with '' (the base).
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
renderDefaultMessage(String defaultMessage, List<Object?> args, String? locale) → String
Formats a default message with args, resolving resolvable arguments first.
inherited
resolveArguments(List<Object?> args, String? locale) → List<Object?>
Replaces each MessageSourceResolvable in args by its message in locale; other arguments stay as they are.
inherited
resolveCode(String code, String locale) → String?
The unformatted message for code in exactly locale ('' for the base), or null. This is the one method a subclass must write; the fallbacks to other locales happen around it. locale is already normalized (see Locales.normalize).
override
resolveCodeInLocales(String code, List<String> locales) → String?
The unformatted message for code in the first of locales (most specific first, ending with '') that has one, or null.
inherited
toString() → String
A string representation of this object.
override

Operators

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