feat: Internationalization User Interface Support. PR Welcome! (#89, #39)

This commit is contained in:
josc146
2023-03-29 14:17:16 +08:00
parent 3ce221ab21
commit 25ed2cc7c7
22 changed files with 541 additions and 78 deletions
+14 -2
View File
@@ -63,7 +63,7 @@ export const defaultConfig = {
apiKey: '',
/** @type {keyof ModelMode}*/
modelMode: 'balanced',
preferredLanguage: navigator.language.substring(0, 2),
preferredLanguage: getNavigatorLanguage(),
insertAtTop: isMobile(),
lockWhenAnswer: false,
customModelApiUrl: 'http://localhost:8000/chat/completions',
@@ -98,7 +98,7 @@ export const defaultConfig = {
// unchangeable
userLanguage: navigator.language.substring(0, 2),
userLanguage: getNavigatorLanguage(),
selectionTools: [
'translate',
'translateBidi',
@@ -132,6 +132,12 @@ export const defaultConfig = {
],
}
export function getNavigatorLanguage() {
const l = navigator.language.toLowerCase()
if (['zh-hk', 'zh-mo', 'zh-tw', 'zh-cht', 'zh-hant'].includes(l)) return 'zhHant'
return navigator.language.substring(0, 2)
}
export function isUsingApiKey(config) {
return (
gptApiModelKeys.includes(config.modelName) || chatgptApiModelKeys.includes(config.modelName)
@@ -146,6 +152,12 @@ export function isUsingCustomModel(config) {
return customApiModelKeys.includes(config.modelName)
}
export async function getPreferredLanguageKey() {
const config = await getUserConfig()
if (config.preferredLanguage === 'auto') return config.userLanguage
return config.preferredLanguage
}
/**
* get user config from local storage
* @returns {Promise<UserConfig>}
+5
View File
@@ -2,6 +2,11 @@ import { languages } from 'countries-list'
import { defaultConfig, getUserConfig } from './index.mjs'
export const languageList = { auto: { name: 'Auto', native: 'Auto' }, ...languages }
languageList.zh.name = 'Chinese (Simplified)'
languageList.zh.native = '简体中文'
languageList.zhHant = { ...languageList.zh }
languageList.zhHant.name = 'Chinese (Traditional)'
languageList.zhHant.native = '正體中文'
export async function getUserLanguage() {
return languageList[defaultConfig.userLanguage].name