1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-24 14:33:17 +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.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_(?<lang>[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<Path> 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.<String, Object>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) {

View File

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

View File

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

View File

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

View File

@@ -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<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()]);
ResourceBundle defaultBundle = Utils.getResourceBundle(null);
HashMap<String, ArrayList<String>> dictionary = new HashMap<>();
for (String lang : Utils.getSupportedLanguages()) {
ResourceBundle.clearCache();
if (lang.equals(DEFAULT_LANG))
continue;
ResourceBundle selectedLang = Utils.getResourceBundle(lang);
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)
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<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));
}
}
}
}