diff --git a/src/main/java/JsonFileCompare.java b/src/main/java/JsonFileCompare.java index 718eb1da7d7aeeb0da28dc9404040d24b7d1627f..3eeabf191d68bf9254b7e5960b41b4059986260b 100644 --- a/src/main/java/JsonFileCompare.java +++ b/src/main/java/JsonFileCompare.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.FileInputStream; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -23,11 +25,11 @@ public class JsonFileCompare { addKeys("", twJson, originSet, new ArrayList<>()); IntStream.range(1, args.length).forEach(i -> findMissingKeys(Paths.get(args[i]), originSet)); - } catch (Exception e) { log.error("{} is not exists or not json files.", args[0], e); System.exit(1); } + checkProperties(); } private static void addKeys( @@ -78,4 +80,64 @@ public class JsonFileCompare { System.exit(1); } } + + private static void checkProperties() { + String path = "./src/main/resources/i18n/"; + String extension = ".properties"; + + Map> map = + Arrays.stream( + Optional.ofNullable( + Paths.get(path).toFile().list((dir, name) -> name.endsWith(extension))) + .orElse(new String[0])) + .map(s -> s.split(extension)[0]) + .collect( + Collectors.groupingBy( + filename -> filename.split("_")[0], + Collectors.mapping(l -> path + l + extension, Collectors.toList()))); + + for (Map.Entry> entry : map.entrySet()) { + List twList = + entry + .getValue() + .stream() + .filter(fileName -> fileName.contains("zh_TW")) + .collect(Collectors.toList()); + List otherList = + entry + .getValue() + .stream() + .filter(fileName -> !fileName.contains("zh_TW")) + .collect(Collectors.toList()); + if (twList.isEmpty() || otherList.isEmpty()) continue; + + try { + Set originKeySet = getKeys(twList.get(0)); + for (String target : otherList) { + Set targetKeySet = getKeys(target); + Set 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 getKeys(String fileName) throws IOException { + Properties properties = new Properties(); + properties.load(new FileInputStream(fileName)); + return new HashSet(Collections.list(properties.propertyNames())); + } }