1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-26 07:14:38 +02:00

Get supported languages dynamically

This commit is contained in:
Isaaku
2019-08-15 12:52:28 -05:00
parent a3bbf8198b
commit 03e8c2d797
3 changed files with 130 additions and 11 deletions

View File

@@ -139,10 +139,6 @@ public final class MainWindow implements Runnable, RipStatusHandler {
private static AbstractRipper ripper; private static AbstractRipper ripper;
// All the langs ripme has been translated into
private static String[] supportedLanges = new String[] { "de_DE", "ar_AR", "en_US", "es_ES", "fi_FI", "fr_CH",
"in_ID", "it_IT", "kr_KR", "nl_NL", "pl_PL", "porrisavvo_FI", "pt_BR", "pt_PT", "ru_RU" };
private void updateQueue(DefaultListModel<Object> model) { private void updateQueue(DefaultListModel<Object> model) {
if (model == null) if (model == null)
model = queueListModel; model = queueListModel;
@@ -483,7 +479,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
queueListModel = new DefaultListModel(); queueListModel = new DefaultListModel();
JList queueList = new JList(queueListModel); JList queueList = new JList(queueListModel);
queueList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); queueList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
queueList.addMouseListener(queueMenuMouseListener = new QueueMenuMouseListener((model) -> updateQueue(model))); queueList.addMouseListener(queueMenuMouseListener = new QueueMenuMouseListener());
JScrollPane queueListScroll = new JScrollPane(queueList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane queueListScroll = new JScrollPane(queueList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
for (String item : Utils.getConfigList("queue")) { for (String item : Utils.getConfigList("queue")) {
@@ -539,8 +535,8 @@ public final class MainWindow implements Runnable, RipStatusHandler {
configLogLevelCombobox = new JComboBox<>( configLogLevelCombobox = new JComboBox<>(
new String[] { "Log level: Error", "Log level: Warn", "Log level: Info", "Log level: Debug" }); new String[] { "Log level: Error", "Log level: Warn", "Log level: Info", "Log level: Debug" });
configSelectLangComboBox = new JComboBox<>(supportedLanges); configSelectLangComboBox = new JComboBox<>(Utils.getSupportedLanguages());
configSelectLangComboBox.setSelectedItem(Utils.getLanguage()); configSelectLangComboBox.setSelectedItem(Utils.getSelectedLanguage());
configLogLevelCombobox.setSelectedItem(Utils.getConfigString("log.level", "Log level: Debug")); configLogLevelCombobox.setSelectedItem(Utils.getConfigString("log.level", "Log level: Debug"));
setLogLevel(configLogLevelCombobox.getSelectedItem().toString()); setLogLevel(configLogLevelCombobox.getSelectedItem().toString());
configSaveDirLabel = new JLabel(); configSaveDirLabel = new JLabel();
@@ -675,8 +671,6 @@ public final class MainWindow implements Runnable, RipStatusHandler {
optionHistory.setText(Utils.getLocalizedString("History")); optionHistory.setText(Utils.getLocalizedString("History"));
optionQueue.setText(Utils.getLocalizedString("queue")); optionQueue.setText(Utils.getLocalizedString("queue"));
optionConfiguration.setText(Utils.getLocalizedString("Configuration")); optionConfiguration.setText(Utils.getLocalizedString("Configuration"));
queueMenuMouseListener.updateUI();
} }
private void setupHandlers() { private void setupHandlers() {

View File

@@ -2,6 +2,7 @@ package com.rarchives.ripme.utils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@@ -21,6 +22,8 @@ import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
@@ -40,6 +43,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 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();
@@ -745,12 +749,38 @@ public class Utils {
resourceBundle = getResourceBundle(langSelect); resourceBundle = getResourceBundle(langSelect);
} }
public static String getLanguage() { public static String getSelectedLanguage() {
return resourceBundle.getLocale().toString(); return resourceBundle.getLocale().toString();
} }
// 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() {
@Override
public boolean accept(File dir, String name) {
LOGGER.info("name: " + name);
return name.startsWith("LabelsBundle_");
}
});
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");
}
return langs;
}
public static String getLocalizedString(String key) { public static String getLocalizedString(String key) {
LOGGER.debug(String.format("Getting key %s in %s value %s",key, getLanguage(), resourceBundle.getString(key))); LOGGER.debug(String.format("Getting key %s in %s value %s", key, getSelectedLanguage(),
resourceBundle.getString(key)));
return resourceBundle.getString(key); return resourceBundle.getString(key);
} }

View File

@@ -0,0 +1,95 @@
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 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.Test;
public class LabelsBundlesTest {
private Logger logger = Logger.getLogger(Utils.class);
@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()]);
}
} 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;
}
}
private Class getClassLoader() {
return Utils.class;
//return Thread.currentThread().getContextClassLoader();
}
}