refactor: 删除重复功能代码

This commit is contained in:
songdragon 2023-08-27 21:58:44 +08:00
parent 50aea7774e
commit feb364eae0
2 changed files with 25 additions and 22 deletions

View File

@ -40,11 +40,22 @@ public class LocalizationController {
return Locale.getDefault(); return Locale.getDefault();
} }
/**
* 初始化语言配置
*/
public static void initLocal() { public static void initLocal() {
setCurrentLocal(null); setCurrentLocal(null);
} }
/**
* 设置当前语言配置
* @param locale 当前语言Local对象
*/
public static void setCurrentLocal(Locale locale) { public static void setCurrentLocal(Locale locale) {
if (locale != null && locale.equals(getCurrentLocal())) {
// 要更新的语言与当前语言一致则不执行
return;
}
if (locale == null) { if (locale == null) {
locale = SUPPORT_LOCALES.get(LOCALIZATION_CONFIG.getLanguage()); locale = SUPPORT_LOCALES.get(LOCALIZATION_CONFIG.getLanguage());
} }
@ -57,6 +68,10 @@ public class LocalizationController {
LOCALIZATION_CONFIG.setLanguage(SUPPORT_LANGUAGES.get(locale)); LOCALIZATION_CONFIG.setLanguage(SUPPORT_LANGUAGES.get(locale));
} }
/**
* 设置当前语言配置
* @param language 当前语言自定义字符形式
*/
public static void setCurrentLanguage(String language) { public static void setCurrentLanguage(String language) {
Locale locale = SUPPORT_LOCALES.get(language); Locale locale = SUPPORT_LOCALES.get(language);
if (locale != null) { if (locale != null) {
@ -78,6 +93,10 @@ public class LocalizationController {
appConfigController.updateLanguage(language); appConfigController.updateLanguage(language);
} }
/**
* 查询当前语言配置
* @return appConfig中的当前语言配置
*/
public String getLanguage() { public String getLanguage() {
return appConfigController.getLanguage(); return appConfigController.getLanguage();
} }

View File

@ -285,16 +285,14 @@ public class JNotepadMenuBar extends MenuBar {
// 设置窗口为置顶 // 设置窗口为置顶
primaryStage.setAlwaysOnTop(after); primaryStage.setAlwaysOnTop(after);
}); });
englishItem.setOnAction(actionEvent -> { englishItem.setOnAction(this::toggleLanguage);
setCurrentLanguage(ENGLISH); chineseItem.setOnAction(this::toggleLanguage);
toggleLanguage(actionEvent);
});
chineseItem.setOnAction(actionEvent -> {
setCurrentLanguage(CHINESE);
toggleLanguage(actionEvent);
});
} }
/**
* 切换语言
* @param actionEvent 点击事件
*/
private void toggleLanguage(ActionEvent actionEvent) { private void toggleLanguage(ActionEvent actionEvent) {
if (actionEvent == null) { if (actionEvent == null) {
return; return;
@ -306,20 +304,6 @@ public class JNotepadMenuBar extends MenuBar {
LocalizationController.setCurrentLocal((Locale) languageItem.getUserData()); LocalizationController.setCurrentLocal((Locale) languageItem.getUserData());
} }
/**
* 设置当前语言<br>
*
* @param language 要设置的语言
* @since 2023/8/26 16:16
*/
private void setCurrentLanguage(String language) {
// 如果当前已是该语言则不执行该方法
if (localizationController.getLanguage().equals(language)) {
return;
}
LocalizationController.setCurrentLanguage(language);
AppConfigController.getInstance().updateLanguage(language);
}
/** /**
* 根据当前选中tab更新菜单选项 * 根据当前选中tab更新菜单选项