fixup: i18n for prompts

This commit is contained in:
Yifei Zhang 2023-03-28 17:39:14 +00:00
parent 6782e65fdf
commit 83400093a2
5 changed files with 67 additions and 57 deletions

View File

@ -22,7 +22,7 @@ import {
} from "../store"; } from "../store";
import { Avatar, PromptHints } from "./home"; import { Avatar, PromptHints } from "./home";
import Locale, { changeLang, getLang } from "../locales"; import Locale, { AllLangs, changeLang, getLang } from "../locales";
import { getCurrentCommitId } from "../utils"; import { getCurrentCommitId } from "../utils";
import Link from "next/link"; import Link from "next/link";
import { UPDATE_URL } from "../constant"; import { UPDATE_URL } from "../constant";
@ -212,26 +212,18 @@ export function Settings(props: { closeSettings: () => void }) {
</ListItem> </ListItem>
<SettingItem title={Locale.Settings.Lang.Name}> <SettingItem title={Locale.Settings.Lang.Name}>
<div className="">
<select <select
value={getLang()} value={getLang()}
onChange={(e) => { onChange={(e) => {
changeLang(e.target.value as any); changeLang(e.target.value as any);
}} }}
> >
<option value="en" key="en"> {AllLangs.map((lang) => (
{Locale.Settings.Lang.Options.en} <option value={lang} key={lang}>
</option> {Locale.Settings.Lang.Options[lang]}
<option value="cn" key="cn">
{Locale.Settings.Lang.Options.cn}
</option>
<option value="tw" key="tw">
{Locale.Settings.Lang.Options.tw}
</option> </option>
))}
</select> </select>
</div>
</SettingItem> </SettingItem>
<div className="no-mobile"> <div className="no-mobile">

View File

@ -66,6 +66,16 @@ const en: LocaleType = {
SendKey: "Send Key", SendKey: "Send Key",
Theme: "Theme", Theme: "Theme",
TightBorder: "Tight Border", TightBorder: "Tight Border",
Prompt: {
Disable: {
Title: "Disable auto-completion",
SubTitle: "After disabling, auto-completion will not be available",
},
List: "Prompt List",
ListCount: (builtin: number, custom: number) =>
`${builtin} built-in, ${custom} user-defined`,
Edit: "Edit",
},
HistoryCount: { HistoryCount: {
Title: "Attached Messages Count", Title: "Attached Messages Count",
SubTitle: "Number of sent messages attached per request", SubTitle: "Number of sent messages attached per request",

View File

@ -1,56 +1,57 @@
import CN from './cn' import CN from "./cn";
import EN from './en' import EN from "./en";
import TW from './tw' import TW from "./tw";
export type { LocaleType } from './cn' export type { LocaleType } from "./cn";
type Lang = 'en' | 'cn' | 'tw' export const AllLangs = ["en", "cn", "tw"] as const;
type Lang = typeof AllLangs[number];
const LANG_KEY = 'lang' const LANG_KEY = "lang";
function getItem(key: string) { function getItem(key: string) {
try { try {
return localStorage.getItem(key) return localStorage.getItem(key);
} catch { } catch {
return null return null;
} }
} }
function setItem(key: string, value: string) { function setItem(key: string, value: string) {
try { try {
localStorage.setItem(key, value) localStorage.setItem(key, value);
} catch {} } catch {}
} }
function getLanguage() { function getLanguage() {
try { try {
return navigator.language.toLowerCase() return navigator.language.toLowerCase();
} catch { } catch {
return 'cn' return "cn";
} }
} }
export function getLang(): Lang { export function getLang(): Lang {
const savedLang = getItem(LANG_KEY) const savedLang = getItem(LANG_KEY);
if (['en', 'cn', 'tw'].includes(savedLang ?? '')) { if (AllLangs.includes((savedLang ?? "") as Lang)) {
return savedLang as Lang return savedLang as Lang;
} }
const lang = getLanguage() const lang = getLanguage();
if (lang.includes('zh') || lang.includes('cn')) { if (lang.includes("zh") || lang.includes("cn")) {
return 'cn' return "cn";
} else if (lang.includes('tw')) { } else if (lang.includes("tw")) {
return 'tw' return "tw";
} else { } else {
return 'en' return "en";
} }
} }
export function changeLang(lang: Lang) { export function changeLang(lang: Lang) {
setItem(LANG_KEY, lang) setItem(LANG_KEY, lang);
location.reload() location.reload();
} }
export default { en: EN, cn: CN, tw: TW }[getLang()] export default { en: EN, cn: CN, tw: TW }[getLang()];

View File

@ -64,6 +64,16 @@ const tw: LocaleType = {
SendKey: "發送鍵", SendKey: "發送鍵",
Theme: "主題", Theme: "主題",
TightBorder: "緊湊邊框", TightBorder: "緊湊邊框",
Prompt: {
Disable: {
Title: "禁用提示詞自動補全",
SubTitle: "禁用後將無法自動根據輸入補全",
},
List: "自定義提示詞列表",
ListCount: (builtin: number, custom: number) =>
`內置 ${builtin} 條,用戶定義 ${custom}`,
Edit: "編輯",
},
HistoryCount: { HistoryCount: {
Title: "附帶歷史消息數", Title: "附帶歷史消息數",
SubTitle: "每次請求攜帶的歷史消息數", SubTitle: "每次請求攜帶的歷史消息數",
@ -117,4 +127,3 @@ const tw: LocaleType = {
}; };
export default tw; export default tw;

View File

@ -1,7 +1,6 @@
import { create } from "zustand"; import { create } from "zustand";
import { persist } from "zustand/middleware"; import { persist } from "zustand/middleware";
import Fuse from "fuse.js"; import Fuse from "fuse.js";
import { showToast } from "../components/ui-lib";
export interface Prompt { export interface Prompt {
id?: number; id?: number;
@ -111,7 +110,6 @@ export const usePromptStore = create<PromptStore>()(
); );
SearchService.count.builtin = res.en.length + res.cn.length; SearchService.count.builtin = res.en.length + res.cn.length;
SearchService.init(allPromptsForSearch); SearchService.init(allPromptsForSearch);
showToast(`已加载 ${allPromptsForSearch.length} 条 Prompts`);
}); });
}, },
} }