Use configured locale as soon as possible

This commit is contained in:
yushijinhun 2018-07-29 14:21:43 +08:00
parent d9aeb0cc2b
commit add00ddba1
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
2 changed files with 12 additions and 12 deletions

View File

@ -38,7 +38,6 @@ public final class ConfigHolder {
public static final Path CONFIG_PATH = Paths.get(CONFIG_FILENAME).toAbsolutePath(); public static final Path CONFIG_PATH = Paths.get(CONFIG_FILENAME).toAbsolutePath();
private static Config configInstance; private static Config configInstance;
private static boolean initialized;
private static boolean newlyCreated; private static boolean newlyCreated;
public static Config config() { public static Config config() {
@ -48,10 +47,6 @@ public final class ConfigHolder {
return configInstance; return configInstance;
} }
public static boolean isInitialized() {
return initialized;
}
public static boolean isNewlyCreated() { public static boolean isNewlyCreated() {
if (configInstance == null) { if (configInstance == null) {
throw new IllegalStateException("Configuration hasn't been loaded"); throw new IllegalStateException("Configuration hasn't been loaded");
@ -72,8 +67,6 @@ public final class ConfigHolder {
if (newlyCreated) { if (newlyCreated) {
saveConfig(); saveConfig();
} }
initialized = true;
} }
private static Config loadConfig() { private static Config loadConfig() {

View File

@ -24,19 +24,26 @@ import java.util.ResourceBundle;
import java.util.logging.Level; import java.util.logging.Level;
import org.jackhuang.hmcl.setting.ConfigHolder; import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
public final class I18n { public final class I18n {
private I18n() {} private I18n() {}
public static ResourceBundle getResourceBundle() { private static SupportedLocale getCurrentLocale() {
if (ConfigHolder.isInitialized()) { try {
return ConfigHolder.config().getLocalization().getResourceBundle(); return ConfigHolder.config().getLocalization();
} else { } catch (IllegalStateException e) {
return Locales.DEFAULT.getResourceBundle(); // e is thrown by ConfigHolder.config(), indicating the config hasn't been loaded
// fallback to use default locale
return Locales.DEFAULT;
} }
} }
public static ResourceBundle getResourceBundle() {
return getCurrentLocale().getResourceBundle();
}
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);
} }