Skip to content

Commit c85c074

Browse files
authored
更改语言应在重启后生效 (HMCL-dev#2905)
1 parent b26c478 commit c85c074

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/setting/ConfigHolder.java

+4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import org.jackhuang.hmcl.Metadata;
2222
import org.jackhuang.hmcl.util.InvocationDispatcher;
2323
import org.jackhuang.hmcl.util.Lang;
24+
import org.jackhuang.hmcl.util.i18n.I18n;
2425
import org.jackhuang.hmcl.util.io.FileUtils;
2526
import org.jackhuang.hmcl.util.io.JarUtils;
2627
import org.jackhuang.hmcl.util.platform.OperatingSystem;
2728

2829
import java.io.IOException;
2930
import java.nio.file.*;
31+
import java.util.Locale;
3032
import java.util.logging.Level;
3133

3234
import static org.jackhuang.hmcl.util.Logging.LOG;
@@ -87,6 +89,8 @@ public static void init() throws IOException {
8789
globalConfigInstance = loadGlobalConfig();
8890
globalConfigInstance.addListener(source -> markGlobalConfigDirty());
8991

92+
Locale.setDefault(config().getLocalization().getLocale());
93+
I18n.setLocale(configInstance.getLocalization());
9094
Settings.init();
9195

9296
if (newlyCreated) {

HMCL/src/main/java/org/jackhuang/hmcl/setting/Settings.java

-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.jackhuang.hmcl.util.CacheRepository;
2525
import org.jackhuang.hmcl.util.io.FileUtils;
2626

27-
import java.util.Locale;
28-
2927
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
3028

3129
public final class Settings {
@@ -47,9 +45,6 @@ static void init() {
4745
}
4846

4947
private Settings() {
50-
config().localizationProperty().addListener(unused -> updateSystemLocale());
51-
updateSystemLocale();
52-
5348
DownloadProviders.init();
5449
ProxyManager.init();
5550
Accounts.init();
@@ -81,8 +76,4 @@ public String getCommonDirectory() {
8176
return null;
8277
}
8378
}
84-
85-
private static void updateSystemLocale() {
86-
Locale.setDefault(config().getLocalization().getLocale());
87-
}
8879
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SettingsView.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jackhuang.hmcl.ui.construct.ComponentList;
3838
import org.jackhuang.hmcl.ui.construct.ComponentSublist;
3939
import org.jackhuang.hmcl.ui.construct.MultiFileItem;
40+
import org.jackhuang.hmcl.util.i18n.I18n;
4041
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
4142

4243
import java.util.Arrays;
@@ -176,8 +177,8 @@ public SettingsView() {
176177
languagePane.setLeft(left);
177178

178179
cboLanguage = new JFXComboBox<>();
179-
cboLanguage.setConverter(stringConverter(locale -> locale.getName(config().getLocalization().getResourceBundle())));
180-
FXUtils.setLimitWidth(cboLanguage, 400);
180+
cboLanguage.setConverter(stringConverter(locale -> locale.getName(I18n.getCurrentLocale().getResourceBundle())));
181+
FXUtils.setLimitWidth(cboLanguage, 300);
181182
languagePane.setRight(cboLanguage);
182183

183184
settingsPane.getContent().add(languagePane);

HMCL/src/main/java/org/jackhuang/hmcl/util/i18n/I18n.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package org.jackhuang.hmcl.util.i18n;
1919

20-
import org.jackhuang.hmcl.setting.ConfigHolder;
2120
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
2221

2322
import java.time.Instant;
@@ -34,14 +33,14 @@ public final class I18n {
3433
private I18n() {
3534
}
3635

36+
private static volatile SupportedLocale locale = Locales.DEFAULT;
37+
38+
public static void setLocale(SupportedLocale locale) {
39+
I18n.locale = locale;
40+
}
41+
3742
public static SupportedLocale getCurrentLocale() {
38-
try {
39-
return ConfigHolder.config().getLocalization();
40-
} catch (IllegalStateException e) {
41-
// e is thrown by ConfigHolder.config(), indicating the config hasn't been loaded
42-
// fallback to use default locale
43-
return Locales.DEFAULT;
44-
}
43+
return locale;
4544
}
4645

4746
public static ResourceBundle getResourceBundle() {

HMCL/src/main/resources/assets/lang/I18N.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ settings.launcher.download_source.auto=Auto Choose Download Mirror
11181118
settings.launcher.enable_game_list=Show version list in home page
11191119
settings.launcher.font=Font
11201120
settings.launcher.general=General
1121-
settings.launcher.language=Language
1121+
settings.launcher.language=Language (applies after restart)
11221122
settings.launcher.launcher_log.export=Export launcher logs
11231123
settings.launcher.launcher_log.export.failed=Unable to export logs
11241124
settings.launcher.launcher_log.export.success=Logs have been exported to %s

HMCL/src/main/resources/assets/lang/I18N_zh.properties

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

HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties

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

0 commit comments

Comments
 (0)