1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-25 14:51:11 +02:00

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
This commit is contained in:
Isaaku
2019-08-23 16:28:02 -05:00
parent 2447a5a917
commit 5545677cfe
5 changed files with 77 additions and 94 deletions

View File

@@ -7,14 +7,22 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.ByteBuffer; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@@ -24,6 +32,7 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
@@ -44,6 +53,7 @@ import org.apache.log4j.PropertyConfigurator;
public class Utils { public class Utils {
private static final Pattern pattern = Pattern.compile("LabelsBundle_(?<lang>[A-Za-z_]+).properties"); private static final Pattern pattern = Pattern.compile("LabelsBundle_(?<lang>[A-Za-z_]+).properties");
private static final String DEFAULT_LANG = "en_US";
private static final String RIP_DIRECTORY = "rips"; private static final String RIP_DIRECTORY = "rips";
private static final String CONFIG_FILE = "rip.properties"; private static final String CONFIG_FILE = "rip.properties";
private static final String OS = System.getProperty("os.name").toLowerCase(); 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 // All the langs ripme has been translated into
public static String[] getSupportedLanguages() { public static String[] getSupportedLanguages() {
File configFile = new File(Utils.class.getResource("/rip.properties").getFile()); ArrayList<Path> filesList = new ArrayList<>();
LOGGER.info("ConfigFile: " + configFile); try {
LOGGER.info("Parent: " + new File(configFile.getParent())); URI uri = Utils.class.getResource("/rip.properties").toURI();
File[] files = new File(configFile.getParent()).listFiles(new FilenameFilter() {
@Override Path myPath;
public boolean accept(File dir, String name) { if (uri.getScheme().equals("jar")) {
LOGGER.info("name: " + name); FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
return name.startsWith("LabelsBundle_"); 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]; String[] langs = new String[filesList.size()];
for (int i = 0; i < files.length; i++) { for (int i = 0; i < filesList.size(); i++) {
Matcher matcher = pattern.matcher(files[i].getName()); Matcher matcher = pattern.matcher(filesList.get(i).toString());
if (matcher.find()) if (matcher.find())
langs[i] = matcher.group("lang"); 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) { public static String getLocalizedString(String key) {

View File

@@ -1,4 +1,4 @@
Log = Log Log = Log
History = Verlauf History = Verlauf
created = erstellt created = erstellt
modified = geändert modified = geändert

View File

@@ -1,4 +1,4 @@
Log = Logi Log = Logi
History = Historia History = Historia
created = Stworzono created = Stworzono
modified = Zmodyfikowano modified = Zmodyfikowano

View File

@@ -1,4 +1,4 @@
Log = Лог Log = Лог
History = История History = История
created = создано created = создано
modified = изменено modified = изменено

View File

@@ -1,95 +1,61 @@
package com.rarchives.ripme.tst.ui; package com.rarchives.ripme.tst.ui;
import java.io.File; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.FilenameFilter;
import java.io.IOException; import java.util.ArrayList;
import java.net.URI; import java.util.Enumeration;
import java.net.URISyntaxException; import java.util.HashMap;
import java.net.URL; import java.util.ResourceBundle;
import java.nio.file.FileSystem; import java.util.Set;
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 com.rarchives.ripme.utils.Utils; import com.rarchives.ripme.utils.Utils;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class LabelsBundlesTest { public class LabelsBundlesTest {
private Logger logger = Logger.getLogger(Utils.class); private Logger logger = Logger.getLogger(Utils.class);
private static final String DEFAULT_LANG = "en_US";
@Test @Test
void testKeyCount() { void testKeyCount() {
((ConsoleAppender) Logger.getRootLogger().getAppender("stdout")).setThreshold(Level.DEBUG); ResourceBundle defaultBundle = Utils.getResourceBundle(null);
File f = new File("E:\\Downloads\\_Isaaku\\dev\\ripme-1.7.86-jar-with-dependencies.jar"); HashMap<String, ArrayList<String>> dictionary = new HashMap<>();
File[] files = f.listFiles(new FilenameFilter() { for (String lang : Utils.getSupportedLanguages()) {
ResourceBundle.clearCache();
@Override if (lang.equals(DEFAULT_LANG))
public boolean accept(File dir, String name) { continue;
logger.info("name: " + name); ResourceBundle selectedLang = Utils.getResourceBundle(lang);
return name.startsWith("LabelsBundle_"); for (final Enumeration<String> 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)
for (String s : getResourcesNames("\\**")) { dictionary.put(lang, new ArrayList<>());
logger.info(s); dictionary.get(lang).add(element);
}
}
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<String> 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()]);
} }
} 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() { @Test
return Utils.class; void testKeyName() {
//return Thread.currentThread().getContextClassLoader(); ResourceBundle defaultBundle = Utils.getResourceBundle(null);
Set<String> 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));
}
}
} }
} }