Commit 5281eed1 authored by Jan S's avatar Jan S Committed by skylot

fix: loading of i18n resources as UTF-8 (see #363) (PR #386)

parent bedbf94b
...@@ -340,7 +340,7 @@ public class MainWindow extends JFrame { ...@@ -340,7 +340,7 @@ public class MainWindow extends JFrame {
openFile(); openFile();
} }
}; };
openAction.putValue(Action.SHORT_DESCRIPTION, NLS.str("file.open")); openAction.putValue(Action.SHORT_DESCRIPTION, NLS.str("file.open_action"));
openAction.putValue(Action.ACCELERATOR_KEY, getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK)); openAction.putValue(Action.ACCELERATOR_KEY, getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));
Action saveAllAction = new AbstractAction(NLS.str("file.save_all"), ICON_SAVE_ALL) { Action saveAllAction = new AbstractAction(NLS.str("file.save_all"), ICON_SAVE_ALL) {
......
package jadx.gui.utils; package jadx.gui.utils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.HashMap; import java.nio.charset.StandardCharsets;
import java.util.Locale; import java.util.*;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class NLS { public class NLS {
private static final Charset JAVA_CHARSET = Charset.forName("ISO-8859-1");
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private static Vector<LangLocale> i18nLocales = new Vector<>(); private static Vector<LangLocale> i18nLocales = new Vector<>();
private static Map<LangLocale, Map<String, String>> i18nMessagesMap = new HashMap<>(); private static Map<LangLocale, ResourceBundle> i18nMessagesMap = new HashMap<>();
// Use these two fields to avoid invoking Map.get() method twice. // Use these two fields to avoid invoking Map.get() method twice.
private static Map<String, String> localizedMessagesMap; private static ResourceBundle localizedMessagesMap;
private static Map<String, String> fallbackMessagesMap; private static ResourceBundle fallbackMessagesMap;
private static LangLocale currentLocale; private static LangLocale currentLocale;
private static LangLocale localLocale; private static LangLocale localLocale;
...@@ -42,37 +41,35 @@ public class NLS { ...@@ -42,37 +41,35 @@ public class NLS {
} }
private static void load(LangLocale locale) { private static void load(LangLocale locale) {
ResourceBundle bundle = ResourceBundle.getBundle("i18n/Messages", locale.get()); ResourceBundle bundle;
Map<String, String> resMap = new HashMap<>(); ClassLoader classLoader = ClassLoader.getSystemClassLoader();
for (String key : bundle.keySet()) { String resName = String.format("i18n/Messages_%s.properties", locale.get());
String str = bundle.getString(key); URL bundleUrl = classLoader.getResource(resName);
resMap.put(key, convertCharset(str)); try (Reader reader = new InputStreamReader(bundleUrl.openStream(), StandardCharsets.UTF_8)) {
bundle = new PropertyResourceBundle(reader);
} catch (IOException e) {
throw new RuntimeException("Failed to load " + resName, e);
} }
i18nMessagesMap.put(locale, resMap); i18nMessagesMap.put(locale, bundle);
}
@NotNull
private static String convertCharset(String str) {
return new String(str.getBytes(JAVA_CHARSET), UTF8_CHARSET);
} }
public static String str(String key) { public static String str(String key) {
String str = localizedMessagesMap.get(key); try {
if (str != null) { return localizedMessagesMap.getString(key);
return str; } catch (MissingResourceException e) {
return fallbackMessagesMap.getString(key); // definitely exists
} }
return fallbackMessagesMap.get(key); // definitely exists
} }
public static String str(String key, LangLocale locale) { public static String str(String key, LangLocale locale) {
Map<String, String> strings = i18nMessagesMap.get(locale); ResourceBundle bundle = i18nMessagesMap.get(locale);
if (strings != null) { if (bundle != null) {
String str = strings.get(key); try {
if (str != null) { return bundle.getString(key);
return str; } catch (MissingResourceException e) {
} }
} }
return fallbackMessagesMap.get(key); // definitely exists return fallbackMessagesMap.getString(key); // definitely exists
} }
public static void setLocale(LangLocale locale) { public static void setLocale(LangLocale locale) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment