更改语言应在重启后生效 (#2905)

This commit is contained in:
Glavo 2024-03-08 15:04:40 +08:00 committed by GitHub
parent b26c478d4a
commit c85c074eee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 17 additions and 22 deletions

View File

@ -21,12 +21,14 @@ import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.Metadata; import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.util.InvocationDispatcher; import org.jackhuang.hmcl.util.InvocationDispatcher;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.JarUtils; import org.jackhuang.hmcl.util.io.JarUtils;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.IOException; import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
import java.util.Locale;
import java.util.logging.Level; import java.util.logging.Level;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.Logging.LOG;
@ -87,6 +89,8 @@ public final class ConfigHolder {
globalConfigInstance = loadGlobalConfig(); globalConfigInstance = loadGlobalConfig();
globalConfigInstance.addListener(source -> markGlobalConfigDirty()); globalConfigInstance.addListener(source -> markGlobalConfigDirty());
Locale.setDefault(config().getLocalization().getLocale());
I18n.setLocale(configInstance.getLocalization());
Settings.init(); Settings.init();
if (newlyCreated) { if (newlyCreated) {

View File

@ -24,8 +24,6 @@ import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.util.CacheRepository; import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.util.Locale;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
public final class Settings { public final class Settings {
@ -47,9 +45,6 @@ public final class Settings {
} }
private Settings() { private Settings() {
config().localizationProperty().addListener(unused -> updateSystemLocale());
updateSystemLocale();
DownloadProviders.init(); DownloadProviders.init();
ProxyManager.init(); ProxyManager.init();
Accounts.init(); Accounts.init();
@ -81,8 +76,4 @@ public final class Settings {
return null; return null;
} }
} }
private static void updateSystemLocale() {
Locale.setDefault(config().getLocalization().getLocale());
}
} }

View File

@ -37,6 +37,7 @@ import org.jackhuang.hmcl.ui.SVG;
import org.jackhuang.hmcl.ui.construct.ComponentList; import org.jackhuang.hmcl.ui.construct.ComponentList;
import org.jackhuang.hmcl.ui.construct.ComponentSublist; import org.jackhuang.hmcl.ui.construct.ComponentSublist;
import org.jackhuang.hmcl.ui.construct.MultiFileItem; import org.jackhuang.hmcl.ui.construct.MultiFileItem;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale; import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
import java.util.Arrays; import java.util.Arrays;
@ -176,8 +177,8 @@ public abstract class SettingsView extends StackPane {
languagePane.setLeft(left); languagePane.setLeft(left);
cboLanguage = new JFXComboBox<>(); cboLanguage = new JFXComboBox<>();
cboLanguage.setConverter(stringConverter(locale -> locale.getName(config().getLocalization().getResourceBundle()))); cboLanguage.setConverter(stringConverter(locale -> locale.getName(I18n.getCurrentLocale().getResourceBundle())));
FXUtils.setLimitWidth(cboLanguage, 400); FXUtils.setLimitWidth(cboLanguage, 300);
languagePane.setRight(cboLanguage); languagePane.setRight(cboLanguage);
settingsPane.getContent().add(languagePane); settingsPane.getContent().add(languagePane);

View File

@ -17,7 +17,6 @@
*/ */
package org.jackhuang.hmcl.util.i18n; package org.jackhuang.hmcl.util.i18n;
import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale; import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
import java.time.Instant; import java.time.Instant;
@ -34,14 +33,14 @@ public final class I18n {
private I18n() { private I18n() {
} }
private static volatile SupportedLocale locale = Locales.DEFAULT;
public static void setLocale(SupportedLocale locale) {
I18n.locale = locale;
}
public static SupportedLocale getCurrentLocale() { public static SupportedLocale getCurrentLocale() {
try { return locale;
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() { public static ResourceBundle getResourceBundle() {

View File

@ -1118,7 +1118,7 @@ settings.launcher.download_source.auto=Auto Choose Download Mirror
settings.launcher.enable_game_list=Show version list in home page settings.launcher.enable_game_list=Show version list in home page
settings.launcher.font=Font settings.launcher.font=Font
settings.launcher.general=General settings.launcher.general=General
settings.launcher.language=Language settings.launcher.language=Language (applies after restart)
settings.launcher.launcher_log.export=Export launcher logs settings.launcher.launcher_log.export=Export launcher logs
settings.launcher.launcher_log.export.failed=Unable to export logs settings.launcher.launcher_log.export.failed=Unable to export logs
settings.launcher.launcher_log.export.success=Logs have been exported to %s settings.launcher.launcher_log.export.success=Logs have been exported to %s

View File

@ -984,7 +984,7 @@ settings.launcher.download_source.auto=自動選擇下載來源
settings.launcher.enable_game_list=在首頁內顯示遊戲列表 settings.launcher.enable_game_list=在首頁內顯示遊戲列表
settings.launcher.font=字體 settings.launcher.font=字體
settings.launcher.general=通用 settings.launcher.general=通用
settings.launcher.language=語言 settings.launcher.language=語言 (重啟後生效)
settings.launcher.launcher_log.export=匯出啟動器日誌 settings.launcher.launcher_log.export=匯出啟動器日誌
settings.launcher.launcher_log.export.failed=無法匯出日誌 settings.launcher.launcher_log.export.failed=無法匯出日誌
settings.launcher.launcher_log.export.success=日誌已儲存到 %s settings.launcher.launcher_log.export.success=日誌已儲存到 %s

View File

@ -992,7 +992,7 @@ settings.launcher.download_source.auto=自动选择下载源
settings.launcher.enable_game_list=在主页内显示版本列表 settings.launcher.enable_game_list=在主页内显示版本列表
settings.launcher.font=字体 settings.launcher.font=字体
settings.launcher.general=通用 settings.launcher.general=通用
settings.launcher.language=语言 settings.launcher.language=语言 (重启后生效)
settings.launcher.launcher_log.export=导出启动器日志 settings.launcher.launcher_log.export=导出启动器日志
settings.launcher.launcher_log.export.failed=无法导出日志 settings.launcher.launcher_log.export.failed=无法导出日志
settings.launcher.launcher_log.export.success=日志已保存到 %s settings.launcher.launcher_log.export.success=日志已保存到 %s