Skip to content

Commit

Permalink
✨ 优化脚本名i18n问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jul 28, 2023
1 parent 968122d commit aa8a119
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
13 changes: 13 additions & 0 deletions src/locales/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { initReactI18next } from "react-i18next";
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
import relativeTime from "dayjs/plugin/relativeTime";
import { Metadata } from "@App/app/repo/scripts";
import enUS from "./en-US/translation.yaml";
import zhCN from "./zh-CN/translation.yaml";

Expand All @@ -25,4 +26,16 @@ dayjs.locale(
);
dayjs.extend(relativeTime);

export function i18nName(script: { name: string; metadata: Metadata }) {
return script.metadata[`name:${i18n.language.toLowerCase()}`]
? script.metadata[`name:${i18n.language.toLowerCase()}`][0]
: script.name;
}

export function i18nDescription(script: { metadata: Metadata }) {
return script.metadata[`description:${i18n.language.toLowerCase()}`]
? script.metadata[`description:${i18n.language.toLowerCase()}`][0]
: script.metadata.description;
}

export default i18n;
7 changes: 5 additions & 2 deletions src/pages/install/description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import IoC from "@App/app/ioc";
import { Subscribe, SUBSCRIBE_STATUS_ENABLE } from "@App/app/repo/subscribe";
import SubscribeController from "@App/app/service/subscribe/controller";
import { useTranslation } from "react-i18next";
import { i18nDescription, i18nName } from "@App/locales/locales";
import CodeEditor from "../components/CodeEditor";

type Permission = { label: string; color?: string; value: string[] }[];
Expand Down Expand Up @@ -246,7 +247,7 @@ export default function Description() {
</Avatar>
)}
<Typography.Text bold className="text-size-lg">
{metadata.name}
{upsertScript && i18nName(upsertScript)}
<Tooltip
content={
isSub
Expand Down Expand Up @@ -274,7 +275,9 @@ export default function Description() {
</Typography.Text>
</div>
<div>
<Typography.Text bold>{metadata.description}</Typography.Text>
<Typography.Text bold>
{upsertScript && i18nDescription(upsertScript)}
</Typography.Text>
</div>
<div>
<Typography.Text bold>
Expand Down
31 changes: 13 additions & 18 deletions src/pages/options/routes/ScriptList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ import CloudScriptPlan from "@App/pages/components/CloudScriptPlan";
import SynchronizeController from "@App/app/service/synchronize/controller";
import { useTranslation } from "react-i18next";
import { nextTime, semTime } from "@App/pkg/utils/utils";
import { i18nName } from "@App/locales/locales";
import { getValues, ListHomeRender, scriptListSort } from "./utils";
// eslint-disable-next-line import/order
import i18n from "@App/locales/locales";

type ListType = Script & { loading?: boolean };

Expand Down Expand Up @@ -202,7 +201,11 @@ function ScriptList() {
</div>
);
},
onFilter: (value, row) => (value ? row.name.indexOf(value) !== -1 : true),
onFilter: (value, row) =>
value
? row.name.indexOf(value) !== -1 ||
i18nName(row).indexOf(value) !== -1
: true,
onFilterDropdownVisibleChange: (visible) => {
if (visible) {
setTimeout(() => inputRef.current!.focus(), 150);
Expand All @@ -226,7 +229,7 @@ function ScriptList() {
whiteSpace: "nowrap",
}}
>
{item.metadata[`name:${i18n.language.toLowerCase()}`] ?? col}
{i18nName(item)}
</Text>
</Link>
</Tooltip>
Expand Down Expand Up @@ -777,6 +780,9 @@ function ScriptList() {
// eslint-disable-next-line no-restricted-globals, no-alert
if (confirm(t("list.confirm_update")!)) {
select.forEach((item, index, array) => {
if (!item.checkUpdateUrl) {
return;
}
Message.warning({
id: "checkupdateStart",
content: t("starting_updates"),
Expand All @@ -788,14 +794,10 @@ function ScriptList() {
// 需要更新
Message.warning({
id: "checkupdate",
content: t("new_version_available"),
content: `${i18nName(item)} ${t(
"new_version_available"
)}`,
});
} else {
// 已是最新,不弹出了,不然脚本数目太多会一直显示很久
// Message.success({
// id: "checkupdate",
// content: t("latest_version"),
// });
}
if (index === array.length - 1) {
// 当前元素是最后一个
Expand All @@ -813,13 +815,6 @@ function ScriptList() {
}`,
});
});
const list = scriptList.map((script) => {
if (script.id === item.id) {
script.updatetime = item.updatetime;
}
return script;
});
setScriptList(list);
});
}
break;
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/background/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import FileSystemFactory from "@Pkg/filesystem/factory";
import FileSystem from "@Pkg/filesystem/filesystem";
import { joinPath } from "@Pkg/filesystem/utils";
import i18next from "i18next";
import { i18nName } from "@App/locales/locales";
import PermissionVerify, {
ConfirmParam,
IPermissionVerify,
Expand Down Expand Up @@ -217,7 +218,7 @@ export default class GMApi {
}
}
const metadata: { [key: string]: string } = {};
metadata[i18next.t("script_name")] = request.script.name;
metadata[i18next.t("script_name")] = i18nName(request.script);
metadata[i18next.t("request_domain")] = url.hostname;
metadata[i18next.t("request_url")] = config.url;

Expand Down Expand Up @@ -689,7 +690,7 @@ export default class GMApi {
);
}
const metadata: { [key: string]: string } = {};
metadata[i18next.t("script_name")] = request.script.name;
metadata[i18next.t("script_name")] = i18nName(request.script);
metadata[i18next.t("request_domain")] = url.host;
return Promise.resolve({
permission: "cookie",
Expand Down Expand Up @@ -836,7 +837,7 @@ export default class GMApi {
}
const dir = details.baseDir ? details.baseDir : request.script.uuid;
const metadata: { [key: string]: string } = {};
metadata[i18next.t("script_name")] = request.script.name;
metadata[i18next.t("script_name")] = i18nName(request.script);
return Promise.resolve({
permission: "file_storage",
permissionValue: dir,
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/background/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Channel } from "@App/app/message/channel";
import IoC from "@App/app/ioc";
import Manager from "@App/app/service/manager";
import Hook from "@App/app/service/hook";
import { i18nName } from "@App/locales/locales";
import { compileInjectScript, compileScriptCode } from "../content/utils";
import GMApi, { Request } from "./gm_api";
import { genScriptMenu } from "./utils";
Expand Down Expand Up @@ -327,7 +328,7 @@ export default class Runtime extends Manager {
if (!script) {
return {
id: item.script.id,
name: item.script.name,
name: i18nName(item.script),
enable: item.script.status === SCRIPT_STATUS_ENABLE,
updatetime: item.script.updatetime || item.script.createtime,
hasUserConfig: !!item.script.config,
Expand All @@ -340,7 +341,7 @@ export default class Runtime extends Manager {
}
return {
id: script.id,
name: script.name,
name: i18nName(script),
enable: script.status === SCRIPT_STATUS_ENABLE,
updatetime: script.updatetime || script.createtime,
hasUserConfig: !!script?.config,
Expand Down

0 comments on commit aa8a119

Please sign in to comment.