input box trans

This commit is contained in:
Gabe Yuan
2023-09-14 14:45:22 +08:00
parent 76f54461e7
commit 72b2f44e32
5 changed files with 90 additions and 11 deletions

View File

@@ -577,6 +577,14 @@ export const I18N = {
}, },
shortcut_press_count: { shortcut_press_count: {
zh: `快捷键连击次数`, zh: `快捷键连击次数`,
en: `Shortcut Press Nunber`, en: `Shortcut Press Number`,
},
input_trans_start_sign: {
zh: `翻译起始标识`,
en: `Translation Start Sign`,
},
input_trans_start_sign_help: {
zh: `标识后面可以加目标语言代码,如: “/en 你好”、“/zh hello”`,
en: `The target language code can be added after the sign, such as: "/en 你好", "/zh hello"`,
}, },
}; };

View File

@@ -137,6 +137,7 @@ export const OPT_LANGS_SPECIAL = {
), ),
[OPT_TRANS_CUSTOMIZE]: new Map([["auto", ""]]), [OPT_TRANS_CUSTOMIZE]: new Map([["auto", ""]]),
}; };
export const OPT_LANGS_LIST = OPT_LANGS_TO.map(([lang]) => lang);
export const OPT_STYLE_NONE = "style_none"; // 无 export const OPT_STYLE_NONE = "style_none"; // 无
export const OPT_STYLE_LINE = "under_line"; // 下划线 export const OPT_STYLE_LINE = "under_line"; // 下划线
@@ -200,6 +201,7 @@ export const GLOBLA_RULE = {
}; };
// 输入框翻译 // 输入框翻译
export const OPT_INPUT_TRANS_SIGNS = ["/", "//", "\\", "\\\\", ">", ">>"];
export const DEFAULT_INPUT_SHORTCUT = ["Alt", "i"]; export const DEFAULT_INPUT_SHORTCUT = ["Alt", "i"];
export const DEFAULT_INPUT_RULE = { export const DEFAULT_INPUT_RULE = {
transOpen: true, transOpen: true,
@@ -208,6 +210,7 @@ export const DEFAULT_INPUT_RULE = {
toLang: "en", toLang: "en",
triggerShortcut: DEFAULT_INPUT_SHORTCUT, triggerShortcut: DEFAULT_INPUT_SHORTCUT,
triggerCount: 1, triggerCount: 1,
transSign: OPT_INPUT_TRANS_SIGNS[0],
}; };
// 订阅列表 // 订阅列表

View File

@@ -12,10 +12,11 @@ import {
DEFAULT_INPUT_RULE, DEFAULT_INPUT_RULE,
DEFAULT_TRANS_APIS, DEFAULT_TRANS_APIS,
DEFAULT_INPUT_SHORTCUT, DEFAULT_INPUT_SHORTCUT,
OPT_LANGS_LIST,
} from "../config"; } from "../config";
import Content from "../views/Content"; import Content from "../views/Content";
import { updateFetchPool, clearFetchPool } from "./fetch"; import { updateFetchPool, clearFetchPool } from "./fetch";
import { debounce, genEventName, removeEndchar } from "./utils"; import { debounce, genEventName, removeEndchar, matchInputStr } from "./utils";
import { stepShortcutRegister } from "./shortcut"; import { stepShortcutRegister } from "./shortcut";
import { apiTranslate } from "../apis"; import { apiTranslate } from "../apis";
import { tryDetectLang } from "."; import { tryDetectLang } from ".";
@@ -265,6 +266,7 @@ export class Translator {
toLang, toLang,
triggerCount, triggerCount,
selector, selector,
transSign,
} = this._inputRule; } = this._inputRule;
const apiSetting = (this._setting.transApis || DEFAULT_TRANS_APIS)[ const apiSetting = (this._setting.transApis || DEFAULT_TRANS_APIS)[
translator translator
@@ -298,6 +300,22 @@ export class Translator {
return; return;
} }
if (transSign) {
const res = matchInputStr(text, transSign);
if (res) {
let lang = res[1];
if (lang === "zh" || lang === "cn") {
lang = "zh-CN";
} else if (lang === "tw" || lang === "hk") {
lang = "zh-TW";
}
if (lang && OPT_LANGS_LIST.includes(lang)) {
toLang = lang;
}
text = res[2];
}
}
// console.log("input -->", text); // console.log("input -->", text);
try { try {

View File

@@ -194,3 +194,32 @@ export const removeEndchar = (s, c, count = 1) => {
} }
return s.slice(0, i); return s.slice(0, i);
}; };
/**
* 匹配字符串及语言标识
* @param {*} str
* @param {*} sign
* @returns
*/
export const matchInputStr = (str, sign) => {
let reg = /\/([\w-]+)\s+([^]+)/;
switch (sign) {
case "//":
reg = /\/\/([\w-]+)\s+([^]+)/;
break;
case "\\":
reg = /\\([\w-]+)\s+([^]+)/;
break;
case "\\\\":
reg = /\\\\([\w-]+)\s+([^]+)/;
break;
case ">":
reg = />([\w-]+)\s+([^]+)/;
break;
case ">>":
reg = />>([\w-]+)\s+([^]+)/;
break;
default:
}
return str.match(reg);
};

View File

@@ -2,9 +2,13 @@ import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack"; import Stack from "@mui/material/Stack";
import TextField from "@mui/material/TextField"; import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem"; import MenuItem from "@mui/material/MenuItem";
import { limitNumber } from "../../libs/utils";
import { useI18n } from "../../hooks/I18n"; import { useI18n } from "../../hooks/I18n";
import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config"; import {
OPT_TRANS_ALL,
OPT_LANGS_FROM,
OPT_LANGS_TO,
OPT_INPUT_TRANS_SIGNS,
} from "../../config";
import ShortcutInput from "./ShortcutInput"; import ShortcutInput from "./ShortcutInput";
import FormControlLabel from "@mui/material/FormControlLabel"; import FormControlLabel from "@mui/material/FormControlLabel";
import Switch from "@mui/material/Switch"; import Switch from "@mui/material/Switch";
@@ -20,13 +24,12 @@ export default function InputSetting() {
const handleChange = (e) => { const handleChange = (e) => {
e.preventDefault(); e.preventDefault();
let { name, value } = e.target; let { name, value } = e.target;
console.log({ name, value }); // switch (name) {
switch (name) { // case "triggerCount":
case "triggerCount": // value = limitNumber(value, 1, 5);
value = limitNumber(value, 1, 3); // break;
break; // default:
default: // }
}
updateInputRule({ updateInputRule({
[name]: value, [name]: value,
}); });
@@ -46,6 +49,7 @@ export default function InputSetting() {
toLang, toLang,
triggerShortcut, triggerShortcut,
triggerCount, triggerCount,
transSign,
} = inputRule; } = inputRule;
return ( return (
@@ -112,6 +116,23 @@ export default function InputSetting() {
))} ))}
</TextField> </TextField>
<TextField
select
size="small"
name="transSign"
value={transSign}
label={i18n("input_trans_start_sign")}
onChange={handleChange}
helperText={i18n("input_trans_start_sign_help")}
>
<MenuItem value={""}>{i18n("style_none")}</MenuItem>
{OPT_INPUT_TRANS_SIGNS.map((item) => (
<MenuItem key={item} value={item}>
{item}
</MenuItem>
))}
</TextField>
<Box> <Box>
<Grid container rowSpacing={2} columns={12}> <Grid container rowSpacing={2} columns={12}>
<Grid item xs={12} sm={12} md={6} lg={6}> <Grid item xs={12} sm={12} md={6} lg={6}>