putVarsInMap function

void putVarsInMap(
  1. Map<String, Map<String, String>> map
)

Implementation

void putVarsInMap(Map<String, Map<String, String>> map) {
  if (!entryDataHasVars) {
    trace('No placeholders detected.');
    return;
  }
  var varsContent = openString(config.inputVarsFile);
  if (varsContent.trim().isEmpty) return;
  var varsYaml = loadYaml(varsContent);
  if (varsYaml is! YamlMap) return;
  //// convert to regular map.
  final varsMap = <String, Map<String, String>>{};
  varsYaml.forEach((key, value) {
    varsMap['$key'] =
        Map.from(value).map((key, value) => MapEntry('$key', '$value'));
  });
  var linkedKeys = [];
  for (var localeKey in map.keys) {
    final localeMap = map[localeKey]!;
    for (var key in localeMap.keys) {
      if (varsMap.containsKey(key)) {
        var text = localeMap[key]!;
        text = replaceVars(VarsCap(text, varsMap[key]!));
        varsByKeys[key] = varsMap[key];
        if (config.resolveLinkedKeys && text.contains('@:')) {
          linkedKeys.add(key);
        }
        localeMap[key] = text;
      }
    }

    if (config.resolveLinkedKeys && linkedKeys.isNotEmpty) {
      for (var key in linkedKeys) {
        var text = localeMap[key]!;
        RegExp(r'@:(.*?)#').allMatches(text).forEach((match) {
          final toReplace = match.group(0)!;
          var findKey = match.group(1)!;
          if (!localeMap.containsKey(findKey)) {
            error(
                'Can\'t find linked key "$findKey" to replace, make sure is correct.');
            exit(3);
          }
          text = text.replaceAll(toReplace, localeMap[findKey]!);
        });
        localeMap[key] = text;
      }
    }
  }
}