refactor: 解耦ResourceBundle和LocalizationController

This commit is contained in:
songdragon 2023-08-27 22:07:14 +08:00
parent feb364eae0
commit 6ff1af20dc
4 changed files with 13 additions and 24 deletions

View File

@ -42,9 +42,6 @@ public class LunchApp extends Application {
scene = new Scene(root, width, length);
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
ViewManager viewManager = ViewManager.getInstance(scene);
viewManager.initScreen(scene);
initUIComponents();
@ -57,8 +54,14 @@ public class LunchApp extends Application {
}
private void initUIComponents() {
//1. 加载语言
LocalizationController.initLocal();
//2. 加载组件
ViewManager viewManager = ViewManager.getInstance(scene);
viewManager.initScreen(scene);
// 使用线程池加载关联文件并创建文本区域
List<String> rawParameters = getParameters().getRaw();
controller.openAssociatedFileAndCreateTextArea(rawParameters);

View File

@ -6,7 +6,6 @@ import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
import java.util.Locale;
import java.util.ResourceBundle;
@ -34,7 +33,7 @@ public class UIResourceBundle {
}
private UIResourceBundle() {
this.resetLocal();
}
/**
@ -62,11 +61,11 @@ public class UIResourceBundle {
/**
* 重置当前local
*/
public final void resetLocal() {
if (this.currentLocale == LocalizationController.getCurrentLocal()) {
public final void resetLocal(Locale toLocal) {
if (this.currentLocale == toLocal) {
return;
}
this.currentLocale = LocalizationController.getCurrentLocal();
this.currentLocale = toLocal;
ResourceBundle resourceBundle = ResourceBundle.getBundle(BASENAME, currentLocale);
this.setResources(resourceBundle);

View File

@ -49,6 +49,7 @@ public class LocalizationController {
/**
* 设置当前语言配置
*
* @param locale 当前语言Local对象
*/
public static void setCurrentLocal(Locale locale) {
@ -64,30 +65,16 @@ public class LocalizationController {
}
Locale.setDefault(locale);
UIResourceBundle.getInstance().resetLocal();
UIResourceBundle.getInstance().resetLocal(getCurrentLocal());
LOCALIZATION_CONFIG.setLanguage(SUPPORT_LANGUAGES.get(locale));
}
/**
* 设置当前语言配置
* @param language 当前语言自定义字符形式
*/
public static void setCurrentLanguage(String language) {
Locale locale = SUPPORT_LOCALES.get(language);
if (locale != null) {
setCurrentLocal(locale);
}
}
private LocalizationController() {
this.appConfigController = AppConfigController.getInstance();
}
private final AppConfigController appConfigController;
public static LocalizationController getLocalizationConfig() {
return LOCALIZATION_CONFIG;
}
private void setLanguage(String language) {
appConfigController.updateLanguage(language);
@ -95,6 +82,7 @@ public class LocalizationController {
/**
* 查询当前语言配置
*
* @return appConfig中的当前语言配置
*/
public String getLanguage() {

View File

@ -32,7 +32,6 @@ public class JNotepadMenuBar extends MenuBar {
private static final JNotepadMenuBar MENU_BAR = new JNotepadMenuBar();
LocalizationController localizationController = LocalizationController.getLocalizationConfig();
AppConfigController appConfigController = AppConfigController.getInstance();
Logger logger = LogUtil.getLogger(this.getClass());