Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 语言切换功能集成 #876

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Supported Versions(支持的版本)

| Version | Supported |
| ------- | ------------------ |
|---------|--------------------|
| 1.0.x | :white_check_mark: |

## Reporting a Vulnerability(报告漏洞)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package com.tlcsdm.cg.provider;

import cn.hutool.core.net.NetUtil;
import cn.hutool.log.StaticLog;
import com.tlcsdm.cg.CgSample;
import com.tlcsdm.cg.util.CgConstant;
import com.tlcsdm.cg.util.I18nUtils;
Expand All @@ -37,17 +36,16 @@
import com.tlcsdm.core.javafx.control.DependencyTableView;
import com.tlcsdm.core.javafx.controlsfx.FxAction;
import com.tlcsdm.core.javafx.controlsfx.FxActionGroup;
import com.tlcsdm.core.javafx.controlsfx.FxLanguageActionGroup;
import com.tlcsdm.core.javafx.dialog.FxAlerts;
import com.tlcsdm.core.javafx.dialog.FxButtonType;
import com.tlcsdm.core.javafx.dialog.FxDialog;
import com.tlcsdm.core.javafx.dialog.LicenseDialog;
import com.tlcsdm.core.javafx.dialog.LogConsoleDialog;
import com.tlcsdm.core.javafx.helper.LayoutHelper;
import com.tlcsdm.core.javafx.richtext.hyperlink.TextHyperlinkArea;
import com.tlcsdm.core.javafx.util.Config;
import com.tlcsdm.core.javafx.util.FxXmlHelper;
import com.tlcsdm.core.javafx.util.JavaFxSystemUtil;
import com.tlcsdm.core.javafx.util.Keys;
import com.tlcsdm.core.util.CoreUtil;
import com.tlcsdm.core.util.DependencyInfo;
import com.tlcsdm.core.util.DependencyInfo.Dependency;
Expand All @@ -60,13 +58,12 @@
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.action.Action;
import org.controlsfx.control.action.ActionCheck;
import org.controlsfx.control.action.ActionGroup;
import org.controlsfx.control.action.ActionUtils;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

import static org.controlsfx.control.action.ActionUtils.ACTION_SEPARATOR;
Expand Down Expand Up @@ -181,105 +178,25 @@ public class CgMenubarConfigrationProvider implements MenubarConfigration {

private final Action release = FxAction.release(actionEvent -> CoreUtil.openWeb(CgConstant.PROJECT_RELEASE_URL));

CheckLangAction chinese = new CheckLangAction(CgConstant.LANGUAGE_CHINESE);
CheckLangAction english = new CheckLangAction(CgConstant.LANGUAGE_ENGLISH);
CheckLangAction japanese = new CheckLangAction(CgConstant.LANGUAGE_JAPANESE);
private final ActionGroup languageGroup = new FxLanguageActionGroup((s) -> {
if (FxAlerts.confirmOkCancel(I18nUtils.get("cg.menubar.setting.language.dialog.title"),
I18nUtils.get("cg.menubar.setting.language.dialog.message"))) {
FXSampler.getStage().close();
Platform.runLater(() -> new FXSampler().start(new Stage()));
}
}).create();

private final Collection<? extends Action> actions = List.of(
FxActionGroup.file(export, induct, ACTION_SEPARATOR, restart, exit),
FxActionGroup.setting(systemSetting, FxActionGroup.language(chinese, english, japanese)),
FxActionGroup.setting(systemSetting, languageGroup),
FxActionGroup.tool(logConsole, pathWatch, colorPicker, screenshot),
FxActionGroup.help(openSysConfig, openLogDir, openUserData, ACTION_SEPARATOR, contactSupport, submitFeedback,
ACTION_SEPARATOR, api, css, fxml, ACTION_SEPARATOR, helpContent, release, about));

/**
* 初始化action
*/
private void initActions() {
// 语言设置
if (Config.defaultLocale.equals(Locale.ENGLISH)) {
english.setSelected(true);
} else if (Config.defaultLocale.equals(Locale.SIMPLIFIED_CHINESE)) {
chinese.setSelected(true);
} else if (Config.defaultLocale.equals(Locale.JAPANESE)) {
japanese.setSelected(true);
}
chinese.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue) {
return;
}
if (newValue) {
english.setSelected(false);
japanese.setSelected(false);
}
});
english.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue) {
return;
}
if (newValue) {
japanese.setSelected(false);
chinese.setSelected(false);
}
});
japanese.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue) {
return;
}
if (newValue) {
english.setSelected(false);
chinese.setSelected(false);
}
});
}

@Override
public MenuBar setMenuBar(MenuBar menuBar) {
initActions();
ActionUtils.updateMenuBar(menuBar, actions);
return menuBar;
}

@ActionCheck
private static class CheckLangAction extends Action {

public CheckLangAction(String name) {
super(name);
init();
}

private void init() {
setEventHandler(ae -> {
String languageType = getText();
if (CgConstant.LANGUAGE_CHINESE.equals(languageType)) {
if (Config.defaultLocale == Locale.SIMPLIFIED_CHINESE) {
return;
}
Config.set(Keys.Locale, Locale.SIMPLIFIED_CHINESE);
} else if (CgConstant.LANGUAGE_ENGLISH.equals(languageType)) {
if (Config.defaultLocale == Locale.ENGLISH) {
return;
}
Config.set(Keys.Locale, Locale.ENGLISH);
} else if (CgConstant.LANGUAGE_JAPANESE.equals(languageType)) {
if (Config.defaultLocale == Locale.JAPANESE) {
return;
}
Config.set(Keys.Locale, Locale.JAPANESE);
}
if (FxAlerts.confirmOkCancel(I18nUtils.get("cg.menubar.setting.language.dialog.title"),
I18nUtils.get("cg.menubar.setting.language.dialog.message"))) {
FXSampler.getStage().close();
Platform.runLater(() -> {
try {
new FXSampler().start(new Stage());
} catch (Exception e) {
StaticLog.error(e);
}
});
}
});
}
}

}
12 changes: 0 additions & 12 deletions cg/src/main/java/com/tlcsdm/cg/util/CgConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,6 @@ public class CgConstant {
* 可由VersionCheckerService实现类修改数据
*/
public static String PROJECT_RELEASE_URL = "https://github.com/unknowIfGuestInDream/javafxTool/releases?q=cg&expanded=true";
/**
* 简体中文
*/
public static final String LANGUAGE_CHINESE = "简体中文";
/**
* 英文
*/
public static final String LANGUAGE_ENGLISH = "English";
/**
* 日文
*/
public static final String LANGUAGE_JAPANESE = "日本語";
/**
* 默认项目版本号
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023 unknowIfGuestInDream.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.tlcsdm.core.javafx.controlsfx;

import com.tlcsdm.core.javafx.util.Config;
import com.tlcsdm.core.javafx.util.Keys;
import org.controlsfx.control.action.Action;
import org.controlsfx.control.action.ActionCheck;

import java.util.Locale;
import java.util.function.Consumer;

/**
* 语言action.
*
* @author unknowIfGuestInDream
*/
@ActionCheck
public class ConfigLanguageAction extends Action {

public ConfigLanguageAction(LanguageType language, Consumer<Object> consumer) {
super(language.getText());
setEventHandler(ae -> {
if (LanguageType.CHINESE.equals(language)) {
if (Config.defaultLocale == Locale.SIMPLIFIED_CHINESE) {
return;
}
Config.set(Keys.Locale, Locale.SIMPLIFIED_CHINESE);
} else if (LanguageType.ENGLISH.equals(language)) {
if (Config.defaultLocale == Locale.ENGLISH) {
return;
}
Config.set(Keys.Locale, Locale.ENGLISH);
} else if (LanguageType.JAPANESE.equals(language)) {
if (Config.defaultLocale == Locale.JAPANESE) {
return;
}
Config.set(Keys.Locale, Locale.JAPANESE);
}
consumer.accept(null);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2023 unknowIfGuestInDream.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.tlcsdm.core.javafx.controlsfx;

import com.tlcsdm.core.javafx.util.Config;
import org.controlsfx.control.action.ActionGroup;

import java.util.Locale;
import java.util.function.Consumer;

/**
* 语言切换ActionGroup.
*
* @author unknowIfGuestInDream
*/
public class FxLanguageActionGroup {
ConfigLanguageAction chinese;
ConfigLanguageAction english;
ConfigLanguageAction japanese;

public FxLanguageActionGroup(Consumer<Object> consumer) {
chinese = new ConfigLanguageAction(LanguageType.CHINESE, consumer);
english = new ConfigLanguageAction(LanguageType.ENGLISH, consumer);
japanese = new ConfigLanguageAction(LanguageType.JAPANESE, consumer);
// 语言设置
if (Config.defaultLocale.equals(Locale.ENGLISH)) {
english.setSelected(true);
} else if (Config.defaultLocale.equals(Locale.SIMPLIFIED_CHINESE)) {
chinese.setSelected(true);
} else if (Config.defaultLocale.equals(Locale.JAPANESE)) {
japanese.setSelected(true);
}
chinese.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue) {
return;
}
if (newValue) {
english.setSelected(false);
japanese.setSelected(false);
}
});
english.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue) {
return;
}
if (newValue) {
japanese.setSelected(false);
chinese.setSelected(false);
}
});
japanese.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue) {
return;
}
if (newValue) {
english.setSelected(false);
chinese.setSelected(false);
}
});
}

public ActionGroup create() {
return FxActionGroup.language(chinese, english, japanese);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 unknowIfGuestInDream.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.tlcsdm.core.javafx.controlsfx;

/**
* 支持的语言类型.
*
* @author unknowIfGuestInDream
*/
public enum LanguageType {
CHINESE("简体中文"),
ENGLISH("English"),
JAPANESE("日本語");

private final String text;

LanguageType(String text) {
this.text = text;
}

public String getText() {
return this.text;
}
}
Loading