-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
175 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export function useDeepGet(target: any, path: string | string[], defaultValue: any) { | ||
if (!Array.isArray(path) && typeof path !== 'string') | ||
throw new TypeError('path must be string or array') | ||
if (target === null) | ||
return defaultValue | ||
|
||
let pathArray = path | ||
if (typeof path === 'string') { | ||
path = path.replace(/\[(\w*)\]/g, '.$1') | ||
path = path.startsWith('.') ? path.slice(1) : path | ||
|
||
pathArray = path.split('.') | ||
} | ||
|
||
let index = 0 | ||
let levelPath: string | ||
while (target !== null && index < pathArray.length) { | ||
levelPath = pathArray[index++] | ||
target = target[levelPath] | ||
} | ||
|
||
return index === pathArray.length ? target : defaultValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { createSignal } from 'solid-js' | ||
import { en } from '@/locale/lang' | ||
import { locales } from '@/locale' | ||
import { providerSettingsMap } from '@/stores/settings' | ||
import { useDeepGet } from './useDepGet' | ||
import type { Accessor } from 'solid-js' | ||
import type { TranslatePair } from '@/locale' | ||
import type { GeneralSettings } from '@/types/app' | ||
|
||
const [currentLocale, setCurrentLocale] = createSignal(en.locales) | ||
|
||
export type TranslatorOption = Record<string, string | number> | ||
export type Translator = (path: string, option?: TranslatorOption) => string | ||
export interface I18nContext { | ||
locale: TranslatePair | ||
t: Translator | ||
} | ||
|
||
export const translate = (path: string, option: TranslatorOption | undefined) => { | ||
return currentLocale() ? (useDeepGet(currentLocale(), path, path) as string).replace(/\{(\w+)\}/g, (_, key) => `${option?.[key] ?? `{${key}}`}`) : '' | ||
} | ||
|
||
export const buildTranslator = (): Translator => (path, option) => translate(path, option) | ||
export const buildI18nContext = (locale: Accessor<TranslatePair>): I18nContext => { | ||
return { | ||
locale: locale(), | ||
t: buildTranslator(), | ||
} | ||
} | ||
|
||
export function useI18n() { | ||
let defaultLocale = providerSettingsMap.get()?.general?.locale ?? 'en' | ||
providerSettingsMap.listen((value, changedKey) => { | ||
const general = value[changedKey] as unknown as GeneralSettings | ||
defaultLocale = general?.locale | ||
setCurrentLocale(locales[defaultLocale as string]) | ||
}) | ||
|
||
setCurrentLocale(locales[defaultLocale as string]) | ||
return buildI18nContext(currentLocale) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Langs from './lang' | ||
import type { SelectOptionType } from '@/types/provider' | ||
|
||
export type LanguageType = keyof typeof Langs | ||
|
||
export interface TranslatePair { | ||
[key: string]: string | string[] | TranslatePair | ||
} | ||
|
||
export interface language { | ||
name: string | ||
desc: string | ||
locales: TranslatePair | ||
} | ||
|
||
export const locales = Object.fromEntries(Object.entries(Langs).map(([key, value]) => [key, value.locales])) | ||
|
||
export const localesOptions: SelectOptionType[] = Object.entries(Langs).map(([key, value]) => ({ label: value.desc, value: key })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { language } from '..' | ||
|
||
export const en = { | ||
name: 'en', | ||
desc: 'English', | ||
locales: { | ||
settings: { | ||
btn: 'TEST', | ||
general: { | ||
title: 'General', | ||
requestWithBackend: 'Request With Backend', | ||
locale: 'Change system language', | ||
}, | ||
openai: { | ||
title: 'OpenAI', | ||
key: '', | ||
}, | ||
replicate: {}, | ||
}, | ||
conversations: { | ||
title: 'CONVERSATIONS', | ||
}, | ||
}, | ||
} as language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './en' | ||
export * from './zh-cn' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { language } from '..' | ||
|
||
export const zhCN = { | ||
name: 'zhCN', | ||
desc: '简体中文', | ||
locales: { | ||
settings: { | ||
btn: 'TEST', | ||
general: { | ||
title: '通用', | ||
requestWithBackend: '请求代理后端', | ||
locale: '切换语言', | ||
}, | ||
openai: { | ||
title: 'OpenAI', | ||
key: '', | ||
}, | ||
replicate: {}, | ||
}, | ||
conversations: { | ||
title: 'CONVERSATIONS', | ||
}, | ||
}, | ||
} as language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters