lookupMessage method

String lookupMessage (String message_str, { String locale, { String name, { List args, String meaning, { MessageIfAbsent ifAbsent })
inherited

Return the localized version of a message. We are passed the original version of the message, which consists of a message_str that will be translated, and which may be interpolated based on one or more variables, a desc providing a description of usage for the message_str, and a map of examples for each data element to be substituted into the message.

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.

The values of desc and examples are not used at run-time but are only made available to the translators, so they MUST be simple Strings available at compile time: no String interpolation or concatenation. The expected usage of this is inside a function that takes as parameters the variables used in the interpolated string.

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

String lookupMessage(
    String message_str, String locale, String name, List args, String meaning,
    {MessageIfAbsent ifAbsent}) {
  var notFound = false;
  var actualName = computeMessageName(name, message_str, meaning);
  if (actualName == null) notFound = true;
  var translation = this[actualName];
  notFound = notFound || (translation == null);
  if (notFound) {
    return ifAbsent == null ? message_str : ifAbsent(message_str, args);
  } else {
    args = args ?? const [];
    return evaluateMessage(translation, args);
  }
}