Fallback to use Locales.DEFAULT when Settings has not been initialized
This commit is contained in:
@@ -17,9 +17,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl;
|
package org.jackhuang.hmcl;
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
public final class Main {
|
public final class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ public final class FXUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void loadFXML(Node node, String absolutePath) {
|
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.setRoot(node);
|
||||||
loader.setController(node);
|
loader.setController(node);
|
||||||
Lang.invoke((ExceptionalSupplier<Object, IOException>) loader::load);
|
Lang.invoke((ExceptionalSupplier<Object, IOException>) loader::load);
|
||||||
|
|||||||
@@ -17,27 +17,42 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.util.i18n;
|
package org.jackhuang.hmcl.util.i18n;
|
||||||
|
|
||||||
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import org.jackhuang.hmcl.setting.Settings;
|
import org.jackhuang.hmcl.setting.Settings;
|
||||||
import org.jackhuang.hmcl.util.Logging;
|
|
||||||
|
|
||||||
public final class I18n {
|
public final class I18n {
|
||||||
|
|
||||||
public static final ResourceBundle RESOURCE_BUNDLE = Settings.INSTANCE.getLocale().getResourceBundle();
|
|
||||||
|
|
||||||
private I18n() {}
|
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) {
|
public static String i18n(String key, Object... formatArgs) {
|
||||||
return String.format(i18n(key), formatArgs);
|
return String.format(i18n(key), formatArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String i18n(String key) {
|
public static String i18n(String key) {
|
||||||
try {
|
try {
|
||||||
return RESOURCE_BUNDLE.getString(key);
|
return getResourceBundle().getString(key);
|
||||||
} catch (Exception e) {
|
} 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;
|
return key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user