applyLinks method

Replaces every ${_root.

Implementation

String applyLinks(TranslationMetadata meta, Map<String, Object> param) {
  return replaceDartNormalizedInterpolation(replacer: (match) {
    final nodeParam = match.substring(2, match.length - 1);
    if (!nodeParam.startsWith(characteristicLinkPrefix)) {
      return match;
    }

    final path = RegexUtils.linkPathRegex.firstMatch(nodeParam)?.group(1);
    if (path == null) {
      return match;
    }

    final refInFlatMap = meta.getTranslation(path);
    if (refInFlatMap == null) {
      return match;
    }

    if (refInFlatMap is String) {
      return refInFlatMap;
    }

    if (refInFlatMap is Function) {
      final parameterList = getFunctionParameters(refInFlatMap);
      return Function.apply(
        refInFlatMap,
        const [],
        {
          for (final p in param.entries)
            if (parameterList.contains(p.key)) Symbol(p.key): p.value,
        },
      );
    }

    return match; // this should not happen (must be string or function)
  });
}