|
| 1 | +package processing.app; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +public class I18NTest { |
| 11 | + |
| 12 | + private Set<String> loadAllI18NKeys() throws IOException { |
| 13 | + Properties properties = new Properties(); |
| 14 | + for (File file : listPropertiesFiles()) { |
| 15 | + properties.putAll(loadProperties(file)); |
| 16 | + } |
| 17 | + Set<String> keys = new HashSet<String>(); |
| 18 | + for (Object key : properties.keySet()) { |
| 19 | + keys.add(key.toString()); |
| 20 | + } |
| 21 | + return keys; |
| 22 | + } |
| 23 | + |
| 24 | + private File[] listPropertiesFiles() { |
| 25 | + return new File(I18NTest.class.getResource(".").getFile()).listFiles(new FileFilter() { |
| 26 | + @Override |
| 27 | + public boolean accept(File file) { |
| 28 | + return file.isFile() && file.getName().endsWith(".properties"); |
| 29 | + } |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + private Properties loadProperties(File file) throws IOException { |
| 34 | + Properties properties = new Properties(); |
| 35 | + InputStream is = null; |
| 36 | + try { |
| 37 | + is = new FileInputStream(file); |
| 38 | + properties.load(is); |
| 39 | + } finally { |
| 40 | + if (is != null) { |
| 41 | + is.close(); |
| 42 | + } |
| 43 | + } |
| 44 | + return properties; |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void ensureEveryTranslationIsComplete() throws Exception { |
| 49 | + Set<String> keys = loadAllI18NKeys(); |
| 50 | + |
| 51 | + Map<String, List<String>> missingTranslationsPerFile = new HashMap<String, List<String>>(); |
| 52 | + |
| 53 | + for (File file : listPropertiesFiles()) { |
| 54 | + Properties properties = loadProperties(file); |
| 55 | + for (String key : keys) { |
| 56 | + if (!properties.containsKey(key) || properties.get(key).equals("")) { |
| 57 | + addMissingTranslation(missingTranslationsPerFile, file, key); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (!missingTranslationsPerFile.isEmpty()) { |
| 63 | + for (Map.Entry<String, List<String>> entry : missingTranslationsPerFile.entrySet()) { |
| 64 | + System.out.println("Following translations in file " + entry.getKey() + " are missing:"); |
| 65 | + for (String key : entry.getValue()) { |
| 66 | + System.out.println("==> '" + key + "'"); |
| 67 | + } |
| 68 | + System.out.println(); |
| 69 | + } |
| 70 | + assertTrue(false); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void addMissingTranslation(Map<String, List<String>> missingTranslationsPerFile, File file, String key) { |
| 75 | + if (!missingTranslationsPerFile.containsKey(file.getName())) { |
| 76 | + missingTranslationsPerFile.put(file.getName(), new LinkedList<String>()); |
| 77 | + } |
| 78 | + |
| 79 | + missingTranslationsPerFile.get(file.getName()).add(key); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments