lookupMessage method

  1. @override
String? lookupMessage(
  1. String? messageText,
  2. String? locale,
  3. String? name,
  4. List<Object>? args,
  5. String? meaning, {
  6. MessageIfAbsent? ifAbsent,
})
override

Return the localized version of a message. We are passed the original version of the message, which consists of a messageText that will be translated, and which may be interpolated based on one or more variables.

For example, if message="Hello, $name", then examples = {'name': 'Sparky'}. If not using the user's default locale, or if the locale is not easily detectable, explicitly pass locale.

Ultimately, the information about the enclosing function and its arguments will be extracted automatically but for the time being it must be passed explicitly in the name and args arguments.

Implementation

@override
String? lookupMessage(String? messageText, String? locale, String? name,
    List<Object>? args, String? meaning,
    {MessageIfAbsent? ifAbsent}) {
  // If passed null, use the default.
  final knownLocale = locale ?? localeName;

  final messages = _messages[flavorName]![knownLocale]!;
  final sentence = messages[name!];

  // sentence is not available in current locale, let's take the default one
  if (sentence == null) {
    final defaultMessages = _messages[defaultFlavorName]![knownLocale]!;
    final defaultSentence = defaultMessages[name];
    if (defaultSentence == null) {
      final defaultMessages =
          _messages[defaultFlavorName]![defaultLocaleName]!;
      final defaultSentence = defaultMessages[name];
      if (defaultSentence == null) {
        print('no message found for $name, default to $messageText');
        return messageText;
      }
      return _getString(
          knownLocale, name.toLowerCase(), defaultSentence, args ?? []);
    }
    return _getString(
        knownLocale, name.toLowerCase(), defaultSentence, args ?? []);
  }
  return _getString(knownLocale, name.toLowerCase(), sentence, args ?? []);
}