Skip to content

Commit

Permalink
fixup: i18n for prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Mar 28, 2023
1 parent 6782e65 commit 8340009
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 57 deletions.
32 changes: 12 additions & 20 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "../store";
import { Avatar, PromptHints } from "./home";

import Locale, { changeLang, getLang } from "../locales";
import Locale, { AllLangs, changeLang, getLang } from "../locales";
import { getCurrentCommitId } from "../utils";
import Link from "next/link";
import { UPDATE_URL } from "../constant";
Expand Down Expand Up @@ -212,26 +212,18 @@ export function Settings(props: { closeSettings: () => void }) {
</ListItem>

<SettingItem title={Locale.Settings.Lang.Name}>
<div className="">
<select
value={getLang()}
onChange={(e) => {
changeLang(e.target.value as any);
}}
>
<option value="en" key="en">
{Locale.Settings.Lang.Options.en}
</option>

<option value="cn" key="cn">
{Locale.Settings.Lang.Options.cn}
</option>

<option value="tw" key="tw">
{Locale.Settings.Lang.Options.tw}
<select
value={getLang()}
onChange={(e) => {
changeLang(e.target.value as any);
}}
>
{AllLangs.map((lang) => (
<option value={lang} key={lang}>
{Locale.Settings.Lang.Options[lang]}
</option>
</select>
</div>
))}
</select>
</SettingItem>

<div className="no-mobile">
Expand Down
10 changes: 10 additions & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ const en: LocaleType = {
SendKey: "Send Key",
Theme: "Theme",
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: {
Title: "Attached Messages Count",
SubTitle: "Number of sent messages attached per request",
Expand Down
69 changes: 35 additions & 34 deletions app/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
import CN from './cn'
import EN from './en'
import TW from './tw'
import CN from "./cn";
import EN from "./en";
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) {
try {
return localStorage.getItem(key)
} catch {
return null
}
try {
return localStorage.getItem(key);
} catch {
return null;
}
}

function setItem(key: string, value: string) {
try {
localStorage.setItem(key, value)
} catch { }
try {
localStorage.setItem(key, value);
} catch {}
}

function getLanguage() {
try {
return navigator.language.toLowerCase()
} catch {
return 'cn'
}
try {
return navigator.language.toLowerCase();
} catch {
return "cn";
}
}

export function getLang(): Lang {
const savedLang = getItem(LANG_KEY)
const savedLang = getItem(LANG_KEY);

if (['en', 'cn', 'tw'].includes(savedLang ?? '')) {
return savedLang as Lang
}
if (AllLangs.includes((savedLang ?? "") as Lang)) {
return savedLang as Lang;
}

const lang = getLanguage()
const lang = getLanguage();

if (lang.includes('zh') || lang.includes('cn')) {
return 'cn'
} else if (lang.includes('tw')) {
return 'tw'
} else {
return 'en'
}
if (lang.includes("zh") || lang.includes("cn")) {
return "cn";
} else if (lang.includes("tw")) {
return "tw";
} else {
return "en";
}
}

export function changeLang(lang: Lang) {
setItem(LANG_KEY, lang)
location.reload()
setItem(LANG_KEY, lang);
location.reload();
}

export default { en: EN, cn: CN, tw: TW }[getLang()]
export default { en: EN, cn: CN, tw: TW }[getLang()];
11 changes: 10 additions & 1 deletion app/locales/tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ const tw: LocaleType = {
SendKey: "發送鍵",
Theme: "主題",
TightBorder: "緊湊邊框",
Prompt: {
Disable: {
Title: "禁用提示詞自動補全",
SubTitle: "禁用後將無法自動根據輸入補全",
},
List: "自定義提示詞列表",
ListCount: (builtin: number, custom: number) =>
`內置 ${builtin} 條,用戶定義 ${custom} 條`,
Edit: "編輯",
},
HistoryCount: {
Title: "附帶歷史消息數",
SubTitle: "每次請求攜帶的歷史消息數",
Expand Down Expand Up @@ -117,4 +127,3 @@ const tw: LocaleType = {
};

export default tw;

2 changes: 0 additions & 2 deletions app/store/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import Fuse from "fuse.js";
import { showToast } from "../components/ui-lib";

export interface Prompt {
id?: number;
Expand Down Expand Up @@ -111,7 +110,6 @@ export const usePromptStore = create<PromptStore>()(
);
SearchService.count.builtin = res.en.length + res.cn.length;
SearchService.init(allPromptsForSearch);
showToast(`已加载 ${allPromptsForSearch.length} 条 Prompts`);
});
},
}
Expand Down

0 comments on commit 8340009

Please sign in to comment.