formatMessage method

String formatMessage(
  1. String id, {
  2. String? attribute,
  3. Map<String, Object?> args = const {},
  4. List<FluentError>? errors,
})

Format the message identified by id. If attribute is supplied, the matching attribute's pattern is formatted instead of the message's value.

Always returns a string. When the message (or attribute) is missing the literal id is returned ('welcome', or 'welcome.title' for an attribute miss) and a FluentReferenceError is recorded on the errors list. The missing id renders visibly in the UI so the gap surfaces during dev, instead of silently rendering as empty space.

To distinguish "missing message" from "successfully rendered", pass an errors list and check it after the call:

final errors = <FluentError>[];
final text = bundle.formatMessage('welcome', errors: errors);
if (errors.any((e) => e is FluentReferenceError)) {
  // Missing message — `text` is the literal id.
}

Or use hasMessage before formatting if you need to branch on presence without rendering.

For inline markup rendered to a span tree, see the formatMessageAsSpans extension in package:fluent_bundle/markup.dart.

Implementation

String formatMessage(
  String id, {
  String? attribute,
  Map<String, Object?> args = const {},
  List<FluentError>? errors,
}) {
  final msg = _messages[id];
  if (msg == null) {
    errors?.add(FluentReferenceError(id));
    return id;
  }
  final pattern = attribute == null ? msg.value : msg.attributes[attribute];
  if (pattern == null) {
    final ref = attribute == null ? id : '$id.$attribute';
    errors?.add(FluentReferenceError(ref));
    return ref;
  }
  return formatPattern(pattern, args: args, errors: errors);
}