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();
private static Config configInstance;
private static boolean initialized;
private static boolean newlyCreated;
public static Config config() {
@ -48,10 +47,6 @@ public final class ConfigHolder {
return configInstance;
}
public static boolean isInitialized() {
return initialized;
}
public static boolean isNewlyCreated() {
if (configInstance == null) {
throw new IllegalStateException("Configuration hasn't been loaded");
@ -72,8 +67,6 @@ public final class ConfigHolder {
if (newlyCreated) {
saveConfig();
}
initialized = true;
}
private static Config loadConfig() {

View File

@ -24,19 +24,26 @@ import java.util.ResourceBundle;
import java.util.logging.Level;
import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
public final class I18n {
private I18n() {}
public static ResourceBundle getResourceBundle() {
if (ConfigHolder.isInitialized()) {
return ConfigHolder.config().getLocalization().getResourceBundle();
} else {
return Locales.DEFAULT.getResourceBundle();
private static SupportedLocale getCurrentLocale() {
try {
return ConfigHolder.config().getLocalization();
} catch (IllegalStateException e) {
// 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) {
return String.format(i18n(key), formatArgs);
}