Added ability to change locale at runtime and from gui

This commit is contained in:
cyian-1756 2018-05-27 15:45:11 -04:00
parent 9d0399d648
commit 25c3196f9b
3 changed files with 93 additions and 9 deletions

View File

@ -127,6 +127,10 @@ public final class MainWindow implements Runnable, RipStatusHandler {
private static JCheckBox configSaveDescriptions; private static JCheckBox configSaveDescriptions;
private static JCheckBox configPreferMp4; private static JCheckBox configPreferMp4;
private static JCheckBox configWindowPosition; private static JCheckBox configWindowPosition;
private static JComboBox<String> configSelectLangComboBox;
private static JLabel configThreadsLabel;
private static JLabel configTimeoutLabel;
private static JLabel configRetriesLabel;
private static TrayIcon trayIcon; private static TrayIcon trayIcon;
private static MenuItem trayMenuMain; private static MenuItem trayMenuMain;
@ -136,7 +140,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
private static AbstractRipper ripper; private static AbstractRipper ripper;
private ResourceBundle rb = Utils.getResourceBundle(); private ResourceBundle rb = Utils.getResourceBundle(null);
private void updateQueueLabel() { private void updateQueueLabel() {
if (queueListModel.size() > 0) { if (queueListModel.size() > 0) {
@ -471,9 +475,9 @@ public final class MainWindow implements Runnable, RipStatusHandler {
// TODO Configuration components // TODO Configuration components
configUpdateButton = new JButton(rb.getString("check.for.updates")); configUpdateButton = new JButton(rb.getString("check.for.updates"));
configUpdateLabel = new JLabel( rb.getString("current.version") + ": " + UpdateUtils.getThisJarVersion(), JLabel.RIGHT); configUpdateLabel = new JLabel( rb.getString("current.version") + ": " + UpdateUtils.getThisJarVersion(), JLabel.RIGHT);
JLabel configThreadsLabel = new JLabel(rb.getString("max.download.threads") + ":", JLabel.RIGHT); configThreadsLabel = new JLabel(rb.getString("max.download.threads") + ":", JLabel.RIGHT);
JLabel configTimeoutLabel = new JLabel(rb.getString("timeout.mill"), JLabel.RIGHT); configTimeoutLabel = new JLabel(rb.getString("timeout.mill"), JLabel.RIGHT);
JLabel configRetriesLabel = new JLabel(rb.getString("retry.download.count"), JLabel.RIGHT); configRetriesLabel = new JLabel(rb.getString("retry.download.count"), JLabel.RIGHT);
configThreadsText = new JTextField(Integer.toString(Utils.getConfigInteger("threads.size", 3))); configThreadsText = new JTextField(Integer.toString(Utils.getConfigInteger("threads.size", 3)));
configTimeoutText = new JTextField(Integer.toString(Utils.getConfigInteger("download.timeout", 60000))); configTimeoutText = new JTextField(Integer.toString(Utils.getConfigInteger("download.timeout", 60000)));
configRetriesText = new JTextField(Integer.toString(Utils.getConfigInteger("download.retries", 3))); configRetriesText = new JTextField(Integer.toString(Utils.getConfigInteger("download.retries", 3)));
@ -492,6 +496,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
configURLHistoryCheckbox = addNewCheckbox(rb.getString("remember.url.history"), "remember.url_history", true); configURLHistoryCheckbox = addNewCheckbox(rb.getString("remember.url.history"), "remember.url_history", true);
configLogLevelCombobox = new JComboBox<>(new String[] {"Log level: Error", "Log level: Warn", "Log level: Info", "Log level: Debug"}); configLogLevelCombobox = new JComboBox<>(new String[] {"Log level: Error", "Log level: Warn", "Log level: Info", "Log level: Debug"});
configSelectLangComboBox = new JComboBox<>(new String[] {"en_US", "de_DE", "es_ES", "fr_CH", "kr_KR", "pt_PT"});
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();
@ -517,6 +522,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
addItemToConfigGridBagConstraints(gbc, 9, configSaveDescriptions, configPreferMp4); addItemToConfigGridBagConstraints(gbc, 9, configSaveDescriptions, configPreferMp4);
addItemToConfigGridBagConstraints(gbc, 10, configWindowPosition, configURLHistoryCheckbox); addItemToConfigGridBagConstraints(gbc, 10, configWindowPosition, configURLHistoryCheckbox);
addItemToConfigGridBagConstraints(gbc, 11, configSaveDirLabel, configSaveDirButton); addItemToConfigGridBagConstraints(gbc, 11, configSaveDirLabel, configSaveDirButton);
addItemToConfigGridBagConstraints(gbc, 12, configSelectLangComboBox);
@ -561,6 +567,36 @@ public final class MainWindow implements Runnable, RipStatusHandler {
gbc.gridx = 1; configurationPanel.add(thing2ToAdd, gbc); gbc.gridx = 1; configurationPanel.add(thing2ToAdd, gbc);
} }
private void addItemToConfigGridBagConstraints(GridBagConstraints gbc, int gbcYValue, JComboBox thing1ToAdd ) {
gbc.gridy = gbcYValue; gbc.gridx = 0; configurationPanel.add(thing1ToAdd, gbc);
}
private void changeLocale() {
statusLabel.setText(rb.getString("inactive"));
configUpdateButton.setText(rb.getString("check.for.updates"));
configUpdateLabel.setText(rb.getString("current.version") + ": " + UpdateUtils.getThisJarVersion());
configThreadsLabel.setText(rb.getString("max.download.threads"));
configTimeoutLabel.setText(rb.getString("timeout.mill"));
configRetriesLabel.setText(rb.getString("retry.download.count"));
configOverwriteCheckbox.setText(rb.getString("overwrite.existing.files"));
configAutoupdateCheckbox.setText(rb.getString("auto.update"));
configPlaySound.setText(rb.getString("sound.when.rip.completes"));
configShowPopup.setText(rb.getString("notification.when.rip.starts"));
configSaveOrderCheckbox.setText(rb.getString("preserve.order"));
configSaveLogs.setText(rb.getString("save.logs"));
configSaveURLsOnly.setText(rb.getString("save.urls.only"));
configSaveAlbumTitles.setText(rb.getString("save.album.titles"));
configClipboardAutorip.setText(rb.getString("autorip.from.clipboard"));
configSaveDescriptions.setText(rb.getString("save.descriptions"));
configPreferMp4.setText(rb.getString("prefer.mp4.over.gif"));
configWindowPosition.setText(rb.getString("restore.window.position"));
configURLHistoryCheckbox.setText(rb.getString("remember.url.history"));
optionLog.setText(rb.getString("Log"));
optionHistory.setText(rb.getString("History"));
optionQueue.setText(rb.getString("Queue"));
optionConfiguration.setText(rb.getString("Configuration"));
}
private void setupHandlers() { private void setupHandlers() {
ripButton.addActionListener(new RipButtonHandler()); ripButton.addActionListener(new RipButtonHandler());
ripTextfield.addActionListener(new RipButtonHandler()); ripTextfield.addActionListener(new RipButtonHandler());
@ -757,6 +793,11 @@ public final class MainWindow implements Runnable, RipStatusHandler {
String level = ((JComboBox) arg0.getSource()).getSelectedItem().toString(); String level = ((JComboBox) arg0.getSource()).getSelectedItem().toString();
setLogLevel(level); setLogLevel(level);
}); });
configSelectLangComboBox.addActionListener(arg0 -> {
String level = ((JComboBox) arg0.getSource()).getSelectedItem().toString();
rb = Utils.getResourceBundle(level);
changeLocale();
});
configSaveDirLabel.addMouseListener(new MouseAdapter() { configSaveDirLabel.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {

View File

@ -686,12 +686,18 @@ public class Utils {
* *
* @return Returns the default resource bundle using the language specified in the config file. * @return Returns the default resource bundle using the language specified in the config file.
*/ */
public static ResourceBundle getResourceBundle() { public static ResourceBundle getResourceBundle(String langSelect) {
if (langSelect == null) {
if (!getConfigString("lang", "").equals("")) { if (!getConfigString("lang", "").equals("")) {
String[] langCode = getConfigString("lang", "").split("_"); String[] langCode = getConfigString("lang", "").split("_");
logger.info("Setting locale to " + getConfigString("lang", "")); logger.info("Setting locale to " + getConfigString("lang", ""));
return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control()); return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control());
} }
} else {
String[] langCode = langSelect.split("_");
logger.info("Setting locale to " + langSelect);
return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control());
}
try { try {
ResourceBundle rb = ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), new UTF8Control()); ResourceBundle rb = ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), new UTF8Control());
return rb; return rb;

View File

@ -0,0 +1,37 @@
Log = Log
History = History
created = created
modified = modified
Queue = Queue
Configuration = Configuration
# Keys for the Configuration menu
current.version = Current version
check.for.updates = Check for updates
auto.update = Auto-update?
max.download.threads = Maximum download threads
timeout.mill = Timeout (in milliseconds):
retry.download.count = Retry download count
overwrite.existing.files = Overwrite existing files?
sound.when.rip.completes = Sound when rip completes
preserve.order = Preserve order
save.logs = Save logs
notification.when.rip.starts = Notification when rip starts
save.urls.only = Save URLs only
save.album.titles = Save album titles
autorip.from.clipboard = Autorip from Clipboard
save.descriptions = Save descriptions
prefer.mp4.over.gif = Prefer MP4 over GIF
restore.window.position = Restore window position
remember.url.history = Remember URL history
loading.history.from = Loading history from
# Misc UI keys
loading.history.from.configuration = Loading history from configuration
interrupted.while.waiting.to.rip.next.album = Interrupted while waiting to rip next album
inactive = Inactive
re-rip.checked = Re-rip Checked
remove = Remove
clear = Clear