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
-
- Object
- AbstractMessageSource
- StaticMessageSource
Constructors
Properties
- alwaysUseMessageFormat ↔ bool
-
Whether messages without arguments are formatted too, so that their
quotes follow MessageFormat rules (
''becomes');falseby default, which returns them as written. When it istrue, an invalid pattern throws aFormatExceptioninstead 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.ReloadableResourceBundleMessageSourceuses the system locale when there is no default locale, as Spring'sAbstractResourceBasedMessageSource.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;
nullfor 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;
falseby default. Handy while translations are incomplete: the screen shows the code.getter/setter pairinherited
Methods
-
addMessage(
String code, String locale, String message) → void -
Adds
messageforcodeinlocale(''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) forlocale. -
formatMessage(
String pattern, List< Object?> args, [String? locale]) → String -
Fills
argsintopatternwith MessageFormat; without arguments (and with alwaysUseMessageFormat off) returnspatternas it is, like Spring.inherited -
getMessage(
String code, {List< Object?> args = const [], String? defaultMessage, String? locale}) → String -
The text for
codeinlocale, withargsfilled into its{0},{1}, ... placeholders.inherited -
getMessageFromResolvable(
MessageSourceResolvable resolvable, {String? locale}) → String -
The text for the first code of
resolvablefound inlocale, 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
codefrom this source, its common messages or its parent;nullwhen 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
argsby its message inlocale; other arguments stay as they are.inherited -
resolveCode(
String code, String locale) → String? -
The unformatted message for
codein exactlylocale(''for the base), ornull. This is the one method a subclass must write; the fallbacks to other locales happen around it.localeis already normalized (seeLocales.normalize).override -
resolveCodeInLocales(
String code, List< String> locales) → String? -
The unformatted message for
codein the first oflocales(most specific first, ending with'') that has one, ornull.inherited -
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited