增加多语言配置文件

This commit is contained in:
许轲 2023-08-24 01:49:13 +08:00
parent 5aea9e96a9
commit 362e63d781
7 changed files with 148 additions and 47 deletions

View File

@ -1,12 +0,0 @@
package org.jcnc.jnotepad.Interface;
import java.util.Properties;
/**
* @author 许轲
*/
public interface ConfigInterface {
void showErrorAlert();
Properties readPropertiesFromFile();
void initializePropertiesFile();
}

View File

@ -29,6 +29,7 @@ public class AppConstants {
/**
* 配置文件名
*/
public static final String PROPERTY_FILE_NAME = "project.txt";
public static final String CH_LANGUAGE_PACK_NAME = "ch_language_pack.txt";
public static final String EN_LANGUAGE_PACK_NAME = "en_language_pack.txt";
}

View File

@ -2,12 +2,15 @@ package org.jcnc.jnotepad.controller.event.handler;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import org.jcnc.jnotepad.init.Config;
import org.jcnc.jnotepad.ui.LineNumberTextArea;
import org.jcnc.jnotepad.ui.status.JNotepadStatusBox;
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
import org.jcnc.jnotepad.view.manager.ViewManager;
import java.util.Properties;
/**
* 新建文件事件的事件处理程序
@ -17,7 +20,12 @@ import org.jcnc.jnotepad.view.manager.ViewManager;
* @author 许轲
*/
public class NewFile implements EventHandler<ActionEvent> {
Config config = new Config();
Properties properties = config.readPropertiesFromFile();
String NEW_TEXT = properties.getProperty("NEW_TEXT");
/**
*
* 处理新建文件事件
*
* @param event 事件对象
@ -31,7 +39,7 @@ public class NewFile implements EventHandler<ActionEvent> {
// TODO: refactor统一TextArea新建绑定监听器入口
ViewManager viewManager = ViewManager.getInstance();
// 将Tab页添加到TabPane中
JNotepadTabPane.getInstance().addNewTab(new JNotepadTab("新建文本 "
JNotepadTabPane.getInstance().addNewTab(new JNotepadTab(NEW_TEXT
+ viewManager.selfIncreaseAndGetTabIndex(),
textArea));

View File

@ -3,6 +3,7 @@ package org.jcnc.jnotepad.controller.manager;
import javafx.application.Platform;
import javafx.concurrent.Task;
import org.jcnc.jnotepad.Interface.ControllerInterface;
import org.jcnc.jnotepad.init.Config;
import org.jcnc.jnotepad.tool.EncodingDetector;
import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.ui.LineNumberTextArea;
@ -16,6 +17,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Properties;
/**
* 控制器类实现ControllerInterface接口用于管理文本编辑器的各种操作和事件处理
@ -24,6 +26,12 @@ import java.util.List;
* @author 许轲
*/
public class Controller implements ControllerInterface {
Config config = new Config();
Properties properties = config.readPropertiesFromFile();
String NEW_FILE = properties.getProperty("NEW_FILE","新建文件");
private static final Controller INSTANCE = new Controller();
private Controller() {
@ -110,7 +118,7 @@ public class Controller implements ControllerInterface {
@Override
public void updateUiWithNewTextArea(LineNumberTextArea textArea) {
ViewManager viewManager = ViewManager.getInstance();
String tabTitle = "新建文件 " + viewManager.selfIncreaseAndGetTabIndex();
String tabTitle = NEW_FILE + viewManager.selfIncreaseAndGetTabIndex();
JNotepadTabPane.getInstance().addNewTab(new JNotepadTab(tabTitle, textArea));
}

View File

@ -1,43 +1,106 @@
package org.jcnc.jnotepad.init;
import javafx.scene.control.Alert;
import org.jcnc.jnotepad.Interface.ConfigInterface;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import static org.jcnc.jnotepad.constants.AppConstants.PROPERTY_FILE_NAME;
import static org.jcnc.jnotepad.constants.AppConstants.CH_LANGUAGE_PACK_NAME;
import static org.jcnc.jnotepad.constants.AppConstants.EN_LANGUAGE_PACK_NAME;
/**
* @author 许轲
* 该类负责配置文件的读取和初始化操作
*/
public class Config implements ConfigInterface {
public class Config {
String LANGUAGE_PACK_NAME;
/**
* 从文件中读取属性配置
*
* @return 包含从文件中读取的属性的 Properties 对象
*/
public Properties readPropertiesFromFile() {
Properties properties = new Properties();
try (InputStream inputStream = new FileInputStream(PROPERTY_FILE_NAME)) {
properties.load(inputStream);
LANGUAGE_PACK_NAME = EN_LANGUAGE_PACK_NAME;
try (InputStream inputStream = new FileInputStream(LANGUAGE_PACK_NAME)) {
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
properties.load(reader);
} catch (IOException e) {
// 如果读取出错则调用初始化方法
initializePropertiesFile();
}
return properties;
}
/**
* 初始化属性配置文件
* 如果属性文件不存在将创建一个新的属性文件并设置默认属性
*/
public void initializePropertiesFile() {
Properties defaultProperties = new Properties();
defaultProperties.setProperty("title", "JNotepad");
Properties chLanguagePack = getCHLanguagePack();
try (OutputStream outputStream = new FileOutputStream(PROPERTY_FILE_NAME)) {
defaultProperties.store(outputStream, "JNotepad Properties");
} catch (IOException e) {
showErrorAlert();
Properties enLanguagePack = getENLanguagePack();
try (OutputStream outputStream = new FileOutputStream(CH_LANGUAGE_PACK_NAME)) {
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
chLanguagePack.store(writer, "JNotepad ch_language_pack");
} catch (IOException ignored) {
}
try (OutputStream outputStream = new FileOutputStream(EN_LANGUAGE_PACK_NAME)) {
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
enLanguagePack.store(writer, "JNotepad en_language_pack");
} catch (IOException ignored) {
}
}
public void showErrorAlert() {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText("文件读写错误");
alert.setContentText("文件读写错误");
alert.showAndWait();
private static Properties getCHLanguagePack() {
Properties properties = new Properties();
properties.setProperty("TITLE", "JNotepad"); // 设置默认属性
properties.setProperty("NEW_TEXT", "新建文件");
properties.setProperty("SAVA", "保存");
properties.setProperty("FILE", "文件");
properties.setProperty("NEW", "新建");
properties.setProperty("OPEN", "打开");
properties.setProperty("SAVA_AS", "另存为");
properties.setProperty("SET", "设置");
properties.setProperty("WORD_WRAP", "自动换行");
properties.setProperty("PLUGIN", "插件");
properties.setProperty("ADD_PLUGIN", "增加插件");
properties.setProperty("STATISTICS", "统计字数");
properties.setProperty("ROW", "行数");
properties.setProperty("COLUMN", "列数");
properties.setProperty("WORD_COUNT", "字数");
properties.setProperty("ENCODE", "编码");
properties.setProperty("NEW_FILE", "新建文件");
return properties;
}
private static Properties getENLanguagePack() {
Properties properties = new Properties();
properties.setProperty("TITLE", "JNotepad");
properties.setProperty("NEW_TEXT", "New File");
properties.setProperty("SAVA", "Save");
properties.setProperty("FILE", "File");
properties.setProperty("NEW", "New");
properties.setProperty("OPEN", "Open");
properties.setProperty("SAVA_AS", "Save As");
properties.setProperty("SET", "Settings");
properties.setProperty("WORD_WRAP", "Word Wrap");
properties.setProperty("PLUGIN", "Plugins");
properties.setProperty("ADD_PLUGIN", "Add Plugin");
properties.setProperty("STATISTICS", "Word Count");
properties.setProperty("ROW", "Row");
properties.setProperty("COLUMN", "Column");
properties.setProperty("WORD_COUNT", "Word Count");
properties.setProperty("ENCODE", "Encoding");
properties.setProperty("NEW_FILE", "New File");
return properties;
}
}

View File

@ -6,11 +6,13 @@ import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import org.jcnc.jnotepad.app.config.GlobalConfig;
import org.jcnc.jnotepad.controller.event.handler.*;
import org.jcnc.jnotepad.init.Config;
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 封装菜单栏组件
@ -19,6 +21,24 @@ import java.util.Map;
*/
public class JNotepadMenuBar extends MenuBar {
Config config = new Config();
Properties properties = config.readPropertiesFromFile();
String SAVA = properties.getProperty("SAVA");
String FILE = properties.getProperty("FILE");
String NEW = properties.getProperty("NEW");
String OPEN = properties.getProperty("OPEN");
String SAVA_AS = properties.getProperty("SAVA_AS");
String SET = properties.getProperty("SET");
String WORD_WRAP = properties.getProperty("WORD_WRAP");
String PLUGIN = properties.getProperty("PLUGIN");
String ADD_PLUGIN = properties.getProperty("ADD_PLUGIN");
String STATISTICS = properties.getProperty("STATISTICS");
private static final JNotepadMenuBar MENU_BAR = new JNotepadMenuBar();
private JNotepadMenuBar() {
@ -90,18 +110,18 @@ public class JNotepadMenuBar extends MenuBar {
*/
private void initFileMenu() {
// 文件菜单
fileMenu = new Menu("文件");
fileMenu = new Menu(FILE);
newItem = new MenuItem("新建");
newItem = new MenuItem(NEW);
itemMap.put("newItem", newItem);
openItem = new MenuItem("打开");
openItem = new MenuItem(OPEN);
itemMap.put("openItem", openItem);
saveItem = new MenuItem("保存");
saveItem = new MenuItem(SAVA);
itemMap.put("saveItem", saveItem);
saveAsItem = new MenuItem("另存为");
saveAsItem = new MenuItem(SAVA_AS);
itemMap.put("saveAsItem", saveAsItem);
fileMenu.getItems().addAll(newItem, openItem, saveItem, saveAsItem);
@ -112,9 +132,9 @@ public class JNotepadMenuBar extends MenuBar {
*/
private void initSettingMenu() {
// 设置菜单
setMenu = new Menu("设置");
setMenu = new Menu(SET);
lineFeedItem = new CheckMenuItem("自动换行");
lineFeedItem = new CheckMenuItem(WORD_WRAP);
itemMap.put("lineFeedItem", lineFeedItem);
lineFeedItem.selectedProperty().set(true);
@ -126,11 +146,11 @@ public class JNotepadMenuBar extends MenuBar {
*/
private void initPluginMenu() {
// 插件菜单
pluginMenu = new Menu("插件");
addItem = new MenuItem("增加插件");
pluginMenu = new Menu(PLUGIN);
addItem = new MenuItem(ADD_PLUGIN);
itemMap.put("addItem", addItem);
countItem = new MenuItem("统计字数");
countItem = new MenuItem(STATISTICS);
itemMap.put("countItem", countItem);
pluginMenu.getItems().addAll(addItem, countItem);

View File

@ -4,9 +4,11 @@ import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import org.jcnc.jnotepad.init.Config;
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
import java.nio.charset.Charset;
import java.util.Properties;
/**
* 状态栏组件封装
@ -17,6 +19,16 @@ import java.nio.charset.Charset;
*/
public class JNotepadStatusBox extends HBox {
Config config = new Config();
Properties properties = config.readPropertiesFromFile();
String ROW = properties.getProperty("ROW");
String COLUMN = properties.getProperty("COLUMN");
String WORD_COUNT = properties.getProperty("WORD_COUNT");
String ENCODE = properties.getProperty("ENCODE");
private static final JNotepadStatusBox STATUS_BOX = new JNotepadStatusBox();
/**
@ -31,7 +43,7 @@ public class JNotepadStatusBox extends HBox {
private JNotepadStatusBox() {
// 创建状态栏
statusLabel = new Label("行数1 \t列数1 \t字数0 ");
statusLabel = new Label(ROW + "1 \t" + COLUMN + "1 \t" + WORD_COUNT + "0 ");
// 创建新的标签以显示编码信息
enCodingLabel = new Label();
@ -51,13 +63,14 @@ public class JNotepadStatusBox extends HBox {
/**
* 更新编码展示
*
* @param encoding 文件编码
*/
public void updateEncodingLabel(String encoding) {
if (encoding == null) {
encoding = Charset.defaultCharset().name();
}
this.enCodingLabel.setText("\t编码: " + encoding);
this.enCodingLabel.setText("\t" + ENCODE + ": " + encoding);
}
/**
@ -69,7 +82,7 @@ public class JNotepadStatusBox extends HBox {
int row = getRow(caretPosition, textArea.getText());
int column = getColumn(caretPosition, textArea.getText());
int length = textArea.getLength();
this.statusLabel.setText(": " + row + " \t: " + column + " \t字数: " + length);
this.statusLabel.setText(ROW + ": " + row + " \t" + COLUMN + ": " + column + " \t" + WORD_COUNT + ": " + length);
}
/**