parseI18nMetadata function

I18nMetadataBundle parseI18nMetadata(
  1. List<AnnotationAst> annotations
)

Parses all internationalization metadata from a node's annotations.

Implementation

I18nMetadataBundle parseI18nMetadata(List<AnnotationAst> annotations) {
  if (annotations.isEmpty) {
    return I18nMetadataBundle.empty();
  }
  // Map metadata builders by attribute name, except for the children metadata
  // builder which has a null key.
  final builders = <String?, _I18nMetadataBuilder>{};
  for (final annotation in annotations) {
    final match = i18nRegExp.matchAsPrefix(annotation.name);
    if (match == null) {
      continue;
    }
    // If `annotation` doesn't specify an attribute name, `attribute` is null
    // which indicates this metadata internationalizes children.
    final attribute = match[_i18nIndexForAttribute];
    final builder = builders[attribute] ??= _I18nMetadataBuilder();
    if (match[_i18nIndexForLocale] != null) {
      builder.locale = annotation;
    } else if (match[_i18nIndexForMeaning] != null) {
      builder.meaning = annotation;
    } else if (match[_i18nIndexForSkip] != null) {
      builder.skip = annotation;
    } else {
      builder.description = annotation;
    }
  }
  final childrenMetadata = builders.remove(null)?.build();
  final attributeMetadata = <String, I18nMetadata>{};
  for (final attribute in builders.keys) {
    final metadata = builders[attribute]!.build();
    // Omit any invalid metadata.
    if (metadata != null) {
      attributeMetadata[attribute!] = metadata;
    }
  }
  return I18nMetadataBundle(childrenMetadata, attributeMetadata);
}