From 5545677cfefae6481470c51eeb9c867b74a845ac Mon Sep 17 00:00:00 2001 From: Isaaku Date: Fri, 23 Aug 2019 16:28:02 -0500 Subject: [PATCH] Fix get supported languages dynamically Remove UTF8-BOM from de_DE, pl_PL and ru_RU bundles Added test to find is there is a Key in a language bundle but not in the default bundle Added test to list keys missing from a language bundle --- .../java/com/rarchives/ripme/utils/Utils.java | 49 +++++--- .../resources/LabelsBundle_de_DE.properties | 2 +- .../resources/LabelsBundle_pl_PL.properties | 2 +- .../resources/LabelsBundle_ru_RU.properties | 2 +- .../ripme/tst/ui/LabelsBundlesTest.java | 116 +++++++----------- 5 files changed, 77 insertions(+), 94 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/utils/Utils.java b/src/main/java/com/rarchives/ripme/utils/Utils.java index e038fea0..98ed8895 100644 --- a/src/main/java/com/rarchives/ripme/utils/Utils.java +++ b/src/main/java/com/rarchives/ripme/utils/Utils.java @@ -7,14 +7,22 @@ import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Constructor; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLDecoder; import java.nio.ByteBuffer; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; @@ -24,6 +32,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; @@ -44,6 +53,7 @@ import org.apache.log4j.PropertyConfigurator; public class Utils { private static final Pattern pattern = Pattern.compile("LabelsBundle_(?[A-Za-z_]+).properties"); + private static final String DEFAULT_LANG = "en_US"; private static final String RIP_DIRECTORY = "rips"; private static final String CONFIG_FILE = "rip.properties"; private static final String OS = System.getProperty("os.name").toLowerCase(); @@ -755,27 +765,34 @@ public class Utils { // All the langs ripme has been translated into public static String[] getSupportedLanguages() { - File configFile = new File(Utils.class.getResource("/rip.properties").getFile()); - LOGGER.info("ConfigFile: " + configFile); - LOGGER.info("Parent: " + new File(configFile.getParent())); - File[] files = new File(configFile.getParent()).listFiles(new FilenameFilter() { + ArrayList filesList = new ArrayList<>(); + try { + URI uri = Utils.class.getResource("/rip.properties").toURI(); - @Override - public boolean accept(File dir, String name) { - LOGGER.info("name: " + name); - return name.startsWith("LabelsBundle_"); + Path myPath; + if (uri.getScheme().equals("jar")) { + FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap()); + myPath = fileSystem.getPath("/"); + } else { + myPath = Paths.get(uri).getParent(); } - }); + Files.walk(myPath, 1).filter(p -> p.toString().contains("LabelsBundle_")).distinct() + .forEach(filesList::add); - String[] langs = new String[files.length]; - for (int i = 0; i < files.length; i++) { - Matcher matcher = pattern.matcher(files[i].getName()); - if (matcher.find()) - langs[i] = matcher.group("lang"); + String[] langs = new String[filesList.size()]; + for (int i = 0; i < filesList.size(); i++) { + Matcher matcher = pattern.matcher(filesList.get(i).toString()); + if (matcher.find()) + langs[i] = matcher.group("lang"); + } + + return langs; + } catch (Exception e) { + e.printStackTrace(); + // On error return default language + return new String[] { DEFAULT_LANG }; } - - return langs; } public static String getLocalizedString(String key) { diff --git a/src/main/resources/LabelsBundle_de_DE.properties b/src/main/resources/LabelsBundle_de_DE.properties index a7ec74f1..61461aba 100644 --- a/src/main/resources/LabelsBundle_de_DE.properties +++ b/src/main/resources/LabelsBundle_de_DE.properties @@ -1,4 +1,4 @@ -Log = Log +Log = Log History = Verlauf created = erstellt modified = geändert diff --git a/src/main/resources/LabelsBundle_pl_PL.properties b/src/main/resources/LabelsBundle_pl_PL.properties index 2a6ba326..4ba4590e 100644 --- a/src/main/resources/LabelsBundle_pl_PL.properties +++ b/src/main/resources/LabelsBundle_pl_PL.properties @@ -1,4 +1,4 @@ -Log = Logi +Log = Logi History = Historia created = Stworzono modified = Zmodyfikowano diff --git a/src/main/resources/LabelsBundle_ru_RU.properties b/src/main/resources/LabelsBundle_ru_RU.properties index 9a97dfa6..a3100df8 100644 --- a/src/main/resources/LabelsBundle_ru_RU.properties +++ b/src/main/resources/LabelsBundle_ru_RU.properties @@ -1,4 +1,4 @@ -Log = Лог +Log = Лог History = История created = создано modified = изменено diff --git a/src/test/java/com/rarchives/ripme/tst/ui/LabelsBundlesTest.java b/src/test/java/com/rarchives/ripme/tst/ui/LabelsBundlesTest.java index 58fd5d34..6189d86a 100644 --- a/src/test/java/com/rarchives/ripme/tst/ui/LabelsBundlesTest.java +++ b/src/test/java/com/rarchives/ripme/tst/ui/LabelsBundlesTest.java @@ -1,95 +1,61 @@ package com.rarchives.ripme.tst.ui; -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.FileSystem; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Collections; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.ResourceBundle; +import java.util.Set; import com.rarchives.ripme.utils.Utils; -import org.apache.log4j.ConsoleAppender; -import org.apache.log4j.Level; import org.apache.log4j.Logger; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class LabelsBundlesTest { - private Logger logger = Logger.getLogger(Utils.class); + private static final String DEFAULT_LANG = "en_US"; @Test void testKeyCount() { - ((ConsoleAppender) Logger.getRootLogger().getAppender("stdout")).setThreshold(Level.DEBUG); - File f = new File("E:\\Downloads\\_Isaaku\\dev\\ripme-1.7.86-jar-with-dependencies.jar"); - File[] files = f.listFiles(new FilenameFilter() { - - @Override - public boolean accept(File dir, String name) { - logger.info("name: " + name); - return name.startsWith("LabelsBundle_"); - } - - }); - - for (String s : getResourcesNames("\\**")) { - logger.info(s); - } - - } - - public String[] getResourcesNames(String path) { - Class loader = getClassLoader(); - /*URL u = loader.getResource("/rip.properties"); - path = u.getFile(); - path = new File(path).getParent();*/ - - try { - URL url = loader.getResource(path); - if (url == null) { - return null; - } - - URI uri = url.toURI(); - if (uri.getScheme().equals("jar")) { // Run from jar - try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) { - Path resourcePath = fileSystem.getPath(path); - - // Get all contents of a resource (skip resource itself), if entry is a - // directory remove trailing / - List resourcesNames = Files.walk(resourcePath, 1).skip(1).map(p -> { - String name = p.getFileName().toString(); - if (name.endsWith("/")) { - name = name.substring(0, name.length() - 1); - } - return name; - }).sorted().collect(Collectors.toList()); - - return resourcesNames.toArray(new String[resourcesNames.size()]); + ResourceBundle defaultBundle = Utils.getResourceBundle(null); + HashMap> dictionary = new HashMap<>(); + for (String lang : Utils.getSupportedLanguages()) { + ResourceBundle.clearCache(); + if (lang.equals(DEFAULT_LANG)) + continue; + ResourceBundle selectedLang = Utils.getResourceBundle(lang); + for (final Enumeration keys = defaultBundle.getKeys(); keys.hasMoreElements();) { + String element = keys.nextElement(); + if (selectedLang.containsKey(element) + && !selectedLang.getString(element).equals(defaultBundle.getString(element))) { + if (dictionary.get(lang) == null) + dictionary.put(lang, new ArrayList<>()); + dictionary.get(lang).add(element); } - } else { // Run from IDE - File resource = new File(uri); - return resource.list(); } - } catch (IOException e) { - return null; - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - return null; } + + dictionary.keySet().forEach(d -> { + logger.warn(String.format("Keys missing in %s", d)); + dictionary.get(d).forEach(v -> logger.warn(v)); + logger.warn("\n"); + }); } - private Class getClassLoader() { - return Utils.class; - //return Thread.currentThread().getContextClassLoader(); + @Test + void testKeyName() { + ResourceBundle defaultBundle = Utils.getResourceBundle(null); + Set defaultSet = defaultBundle.keySet(); + for (String lang : Utils.getSupportedLanguages()) { + if (lang.equals(DEFAULT_LANG)) + continue; + for (String key : Utils.getResourceBundle(lang).keySet()) { + assertTrue(defaultSet.contains(key), + String.format("The key %s of %s is not in the default bundle", key, lang)); + } + } } } \ No newline at end of file