feat(i18n): new language switcher in config page

This commit is contained in:
kunish 2023-09-23 02:06:57 +08:00
parent 81bab5d0ab
commit 21d479a1df
No known key found for this signature in database
GPG Key ID: 647A12B4F782C430
4 changed files with 51 additions and 32 deletions

View File

@ -3,7 +3,6 @@ import {
IconFileStack,
IconGlobe,
IconHome,
IconLanguage,
IconMenu,
IconNetwork,
IconPalette,
@ -11,9 +10,9 @@ import {
IconSettings,
} from '@tabler/icons-solidjs'
import { For, ParentComponent, Show, createSignal } from 'solid-js'
import { Button, LogoText } from '~/components'
import { LANG, ROUTES, themes } from '~/constants'
import { setLocale, useI18n } from '~/i18n'
import { LogoText } from '~/components'
import { ROUTES, themes } from '~/constants'
import { useI18n } from '~/i18n'
import { setCurTheme } from '~/signals'
const Nav: ParentComponent<{ href: string; tooltip: string }> = ({
@ -152,14 +151,6 @@ export const Header = () => {
<div class="navbar-end">
<div class="flex items-center gap-2">
<Button
class="btn-circle btn-secondary btn-sm"
onClick={() =>
setLocale((locale) => (locale === LANG.EN ? LANG.ZH : LANG.EN))
}
icon={<IconLanguage />}
/>
<ThemeSwitcher />
</div>
</div>

View File

@ -101,4 +101,6 @@ export default {
tunModeStack: 'TUN Mode Stack',
tunDeviceName: 'TUN Device Name',
interfaceName: 'Interface Name',
en: 'English',
zh: 'Chinese',
}

View File

@ -103,4 +103,6 @@ export default {
tunModeStack: 'TUN 模式堆栈',
tunDeviceName: 'TUN 设备名称',
interfaceName: '接口名称',
en: '英文',
zh: '中文',
} satisfies Dict

View File

@ -26,8 +26,8 @@ import {
upgradingBackend,
} from '~/apis'
import { Button, ConfigTitle } from '~/components'
import { MODE_OPTIONS, ROUTES, themes } from '~/constants'
import { useI18n } from '~/i18n'
import { LANG, MODE_OPTIONS, ROUTES, themes } from '~/constants'
import { locale, setLocale, useI18n } from '~/i18n'
import {
autoSwitchTheme,
favDayTheme,
@ -354,6 +354,17 @@ const ConfigForm = () => {
const ConfigForXd = () => {
const [t] = useI18n()
const languages = [
{
label: () => t('en'),
value: LANG.EN,
},
{
label: () => t('zh'),
value: LANG.ZH,
},
]
return (
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
<div class="flex flex-col gap-2">
@ -375,14 +386,11 @@ const ConfigForXd = () => {
<select
class="select select-bordered"
value={favDayTheme()}
onChange={(e) => setFavDayTheme(e.target.value)}
>
<For each={themes}>
{(theme) => (
<option selected={favDayTheme() === theme} value={theme}>
{theme}
</option>
)}
{(theme) => <option value={theme}>{theme}</option>}
</For>
</select>
</div>
@ -392,14 +400,11 @@ const ConfigForXd = () => {
<select
class="select select-bordered"
value={favNightTheme()}
onChange={(e) => setFavNightTheme(e.target.value)}
>
<For each={themes}>
{(theme) => (
<option selected={favNightTheme() === theme} value={theme}>
{theme}
</option>
)}
{(theme) => <option value={theme}>{theme}</option>}
</For>
</select>
</div>
@ -407,6 +412,7 @@ const ConfigForXd = () => {
</Show>
</div>
<div class="flex flex-col gap-2">
<div class="flex flex-col items-center">
<ConfigTitle>{t('useTwemoji')}</ConfigTitle>
@ -417,6 +423,24 @@ const ConfigForXd = () => {
onChange={(e) => setUseTwemoji(e.target.checked)}
/>
</div>
<div class="flex flex-col">
<ConfigTitle>{t('switchLanguage')}</ConfigTitle>
<select
class="select select-bordered"
onChange={(e) => setLocale(e.target.value as LANG)}
>
<For each={languages}>
{(lang) => (
<option selected={locale() === lang.value} value={lang.value}>
{lang.label()}
</option>
)}
</For>
</select>
</div>
</div>
</div>
)
}