Fallback to use Locales.DEFAULT when Settings has not been initialized

This commit is contained in:
yushijinhun
2018-07-01 21:24:46 +08:00
parent 9743cf8351
commit 13e931cda4
3 changed files with 23 additions and 7 deletions

View File

@@ -17,9 +17,10 @@
*/
package org.jackhuang.hmcl;
import javax.swing.*;
import java.io.File;
import javax.swing.JOptionPane;
public final class Main {
public static void main(String[] args) {

View File

@@ -203,7 +203,7 @@ public final class FXUtils {
}
public static void loadFXML(Node node, String absolutePath) {
FXMLLoader loader = new FXMLLoader(node.getClass().getResource(absolutePath), I18n.RESOURCE_BUNDLE);
FXMLLoader loader = new FXMLLoader(node.getClass().getResource(absolutePath), I18n.getResourceBundle());
loader.setRoot(node);
loader.setController(node);
Lang.invoke((ExceptionalSupplier<Object, IOException>) loader::load);

View File

@@ -17,27 +17,42 @@
*/
package org.jackhuang.hmcl.util.i18n;
import static org.jackhuang.hmcl.util.Logging.LOG;
import java.util.ResourceBundle;
import java.util.logging.Level;
import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.util.Logging;
public final class I18n {
public static final ResourceBundle RESOURCE_BUNDLE = Settings.INSTANCE.getLocale().getResourceBundle();
private I18n() {}
private static ResourceBundle RESOURCE_BUNDLE;
static {
try {
RESOURCE_BUNDLE = Settings.INSTANCE.getLocale().getResourceBundle();
} catch (Throwable e) {
LOG.log(Level.SEVERE, "Settings cannot be initialized", e);
// fallback
RESOURCE_BUNDLE = Locales.DEFAULT.getResourceBundle();
}
}
public static ResourceBundle getResourceBundle() {
return RESOURCE_BUNDLE;
}
public static String i18n(String key, Object... formatArgs) {
return String.format(i18n(key), formatArgs);
}
public static String i18n(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
return getResourceBundle().getString(key);
} catch (Exception e) {
Logging.LOG.log(Level.SEVERE, "Cannot find key " + key + " in resource bundle", e);
LOG.log(Level.SEVERE, "Cannot find key " + key + " in resource bundle", e);
return key;
}
}