AbstractMessageSource class abstract

The base class for message sources: it does the locale fallbacks, the formatting and the parent lookup, so that a subclass only says where the texts are.

Extend it to read messages from a place this package does not support, such as a database or a map built in code; implement resolveCode and everything else is shared. Applications usually take ReloadableResourceBundleMessageSource (files) or StaticMessageSource (code) instead.

The lookup of code c for locale ru_RU tries ru_RU, then ru, then the fallbackLocale and its own fallbacks, then the base (no locale), then commonMessages, then the parent. Locales with a script follow Java's ResourceBundle: zh_Hant_TW, zh_Hant, zh_TW, zh. Arguments that are MessageSourceResolvables are resolved by this source before the parent is asked, so a message of the parent can use arguments only the child knows. Messages are formatted with the number symbols of the requested locale.

// Messages from a map: locale, then code, then pattern.
class MapMessageSource extends AbstractMessageSource {
  MapMessageSource(this.bundles);

  /// locale → code → pattern
  final Map<String, Map<String, String>> bundles;

  @override
  String? resolveCode(String code, String locale) =>
      bundles[locale]?[code];
}

final source = MapMessageSource({
  '': {'hi': 'Hello {0}'},
  'de': {'hi': 'Hallo {0}'},
})..useCodeAsDefaultMessage = true;
source.getMessage('hi', args: ['Ada'], locale: 'de_AT'); // Hallo Ada

Spring (Java):

class MapMessageSource extends AbstractMessageSource {
  protected MessageFormat resolveCode(String code, Locale locale) {
    ...
  }
}

Unlike Spring, resolveCode returns the raw pattern (not a MessageFormat) and the default locale is defaultLocale rather than the JVM's Locale.getDefault().

Implemented types

Constructors

AbstractMessageSource()

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 pair
commonMessages ↔ Map<String, String>?
Messages that are the same in every language, by code; tried after all locales and before the parent.
getter/setter pair
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 pair
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 setter
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 pairoverride
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 pair

Methods

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.
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.
override
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.
override
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.
localeChain(String? locale) → List<String>
The locales tried for locale: its candidates, then those of fallbackLocale, ending with '' (the base).
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.
resolveArguments(List<Object?> args, String? locale) → List<Object?>
Replaces each MessageSourceResolvable in args by its message in locale; other arguments stay as they are.
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).
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.
toString() → String
A string representation of this object.
inherited

Operators

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