LanguageFileCompare.java 4.98 KB
Newer Older
王品堯's avatar
王品堯 committed
1 2 3
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
4 5
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
王品堯's avatar
王品堯 committed
6

王品堯's avatar
王品堯 committed
7 8
import java.io.FileInputStream;
import java.io.IOException;
王品堯's avatar
王品堯 committed
9 10 11 12 13 14 15
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

王品堯's avatar
王品堯 committed
16 17
public class LanguageFileCompare {
  private static Logger log = LoggerFactory.getLogger(LanguageFileCompare.class);
王品堯's avatar
王品堯 committed
18 19 20 21

  public static void main(String[] args) {
    try {
      ObjectMapper objectMapper = new ObjectMapper();
王品堯's avatar
王品堯 committed
22 23 24 25 26
      if (args.length > 0) {
        Path originPath = Paths.get(args[0]);
        JsonNode twJson = objectMapper.readTree(new String(Files.readAllBytes(originPath)));
        Set<String> originSet = new HashSet<>();
        addKeys("", twJson, originSet, new ArrayList<>());
王品堯's avatar
王品堯 committed
27

王品堯's avatar
王品堯 committed
28 29 30
        IntStream.range(1, args.length)
            .forEach(i -> findMissingKeys(Paths.get(args[i]), originSet));
      }
王品堯's avatar
王品堯 committed
31
    } catch (Exception e) {
32
      log.error("{} is not exists or not json files.", args[0], e);
王品堯's avatar
王品堯 committed
33 34
      System.exit(1);
    }
王品堯's avatar
王品堯 committed
35
    checkProperties();
王品堯's avatar
王品堯 committed
36 37
  }

王品堯's avatar
王品堯 committed
38 39
  private static void addKeys(
      String currentPath, JsonNode jsonNode, Set<String> set, List<Integer> suffix) {
王品堯's avatar
王品堯 committed
40 41 42 43 44 45 46 47 48 49 50
    if (jsonNode.isObject()) {
      ObjectNode objectNode = (ObjectNode) jsonNode;
      Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
      String pathPrefix = currentPath.isEmpty() ? "" : currentPath + "-";

      while (iter.hasNext()) {
        Map.Entry<String, JsonNode> entry = iter.next();
        addKeys(pathPrefix + entry.getKey(), entry.getValue(), set, suffix);
      }

    } else if (jsonNode.isValueNode()) {
51
      StringBuilder sb = new StringBuilder(currentPath);
王品堯's avatar
王品堯 committed
52
      if (currentPath.contains("-")) {
53 54
        for (Integer suffix1 : suffix) {
          sb.append("-").append(suffix1);
王品堯's avatar
王品堯 committed
55 56
        }
      }
57
      set.add(sb.toString());
王品堯's avatar
王品堯 committed
58 59 60
    }
  }

61
  private static void findMissingKeys(Path targetPath, Set<String> originSet) {
王品堯's avatar
王品堯 committed
62 63 64 65 66 67 68 69 70 71 72 73 74
    if (!targetPath.toFile().exists()) return;
    try {
      Set<String> targetSet = new HashSet<>();
      ObjectMapper objectMapper = new ObjectMapper();
      JsonNode json = objectMapper.readTree(new String(Files.readAllBytes(targetPath)));
      addKeys("", json, targetSet, new ArrayList<>());

      Set<String> missingKey =
          originSet
              .parallelStream()
              .filter(key -> !targetSet.contains(key))
              .collect(Collectors.toSet());
      if (!missingKey.isEmpty()) {
王品堯's avatar
王品堯 committed
75 76 77 78
        log.error(
            "Missing Key on {} \nMissing Keys: {}",
            targetPath.getFileName(),
            missingKey.stream().sorted().collect(Collectors.toList()));
王品堯's avatar
王品堯 committed
79 80 81
        System.exit(1);
      }
    } catch (Exception e) {
82
      log.error("{} is not json files.", targetPath, e);
王品堯's avatar
王品堯 committed
83 84 85
      System.exit(1);
    }
  }
王品堯's avatar
王品堯 committed
86 87 88 89 90 91 92

  private static void checkProperties() {
    String path = "./src/main/resources/i18n/";
    String extension = ".properties";

    Map<String, List<String>> map =
        Arrays.stream(
王品堯's avatar
王品堯 committed
93 94 95
                Optional.ofNullable(
                        Paths.get(path).toFile().list((dir, name) -> name.endsWith(extension)))
                    .orElse(new String[0]))
王品堯's avatar
王品堯 committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            .map(s -> s.split(extension)[0])
            .collect(
                Collectors.groupingBy(
                    filename -> filename.split("_")[0],
                    Collectors.mapping(l -> path + l + extension, Collectors.toList())));

    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
      List<String> twList =
          entry
              .getValue()
              .stream()
              .filter(fileName -> fileName.contains("zh_TW"))
              .collect(Collectors.toList());
      List<String> otherList =
          entry
              .getValue()
              .stream()
              .filter(fileName -> !fileName.contains("zh_TW"))
              .collect(Collectors.toList());
      if (twList.isEmpty() || otherList.isEmpty()) continue;

      try {
        Set<String> originKeySet = getKeys(twList.get(0));
        for (String target : otherList) {
          Set<String> targetKeySet = getKeys(target);
          Set<String> missingSet =
              originKeySet
                  .parallelStream()
                  .filter(k -> !targetKeySet.contains(k))
                  .collect(Collectors.toSet());
          if (!missingSet.isEmpty()) {
            log.error(
                "Missing Key on {} \nMissing Keys: {}",
                Paths.get(target).getFileName(),
                missingSet);
            System.exit(1);
          }
        }
      } catch (Exception e) {
        log.error("check i18n properties error.", e);
        System.exit(1);
      }
    }
  }

  private static Set<String> getKeys(String fileName) throws IOException {
    Properties properties = new Properties();
    properties.load(new FileInputStream(fileName));
    return new HashSet(Collections.list(properties.propertyNames()));
  }
王品堯's avatar
王品堯 committed
146
}