实现 CT 时间偏移选择并优化 URL 语言处理:新增获取 CT 时间偏移选项接口,表单改用下拉选择;URL 语言参数统一为 zh/tw/en 并兼容旧版;Vite 支持局域网访问,更新开发文档。
This commit is contained in:
@@ -2,8 +2,48 @@ export const APP_LOCALES = ['zh-CN', 'zh-TW', 'en-US'] as const
|
||||
|
||||
export type AppLocale = (typeof APP_LOCALES)[number]
|
||||
|
||||
/** URL `lang` 查询参数取值(写入地址栏时使用) */
|
||||
export const URL_LANG_CODES = ['zh', 'tw', 'en'] as const
|
||||
|
||||
export type UrlLangCode = (typeof URL_LANG_CODES)[number]
|
||||
|
||||
export const DEFAULT_LOCALE: AppLocale = 'zh-TW'
|
||||
|
||||
export const DEFAULT_URL_LANG: UrlLangCode = 'tw'
|
||||
|
||||
export const LOCALE_TO_URL_LANG: Record<AppLocale, UrlLangCode> = {
|
||||
'zh-CN': 'zh',
|
||||
'zh-TW': 'tw',
|
||||
'en-US': 'en',
|
||||
}
|
||||
|
||||
const URL_LANG_TO_LOCALE: Record<UrlLangCode, AppLocale> = {
|
||||
zh: 'zh-CN',
|
||||
tw: 'zh-TW',
|
||||
en: 'en-US',
|
||||
}
|
||||
|
||||
export function isAppLocale(value: string | null): value is AppLocale {
|
||||
return value !== null && (APP_LOCALES as readonly string[]).includes(value)
|
||||
}
|
||||
|
||||
/** 解析 URL / 用户输入的语言参数为内部 `AppLocale` */
|
||||
export function resolveLocaleFromLangParam(value: string | undefined): AppLocale {
|
||||
if (!value) {
|
||||
return DEFAULT_LOCALE
|
||||
}
|
||||
const trimmed = value.trim()
|
||||
const lower = trimmed.toLowerCase()
|
||||
if ((URL_LANG_CODES as readonly string[]).includes(lower)) {
|
||||
return URL_LANG_TO_LOCALE[lower as UrlLangCode]
|
||||
}
|
||||
const normalized = trimmed.replace('_', '-')
|
||||
if (isAppLocale(normalized)) {
|
||||
return normalized
|
||||
}
|
||||
return DEFAULT_LOCALE
|
||||
}
|
||||
|
||||
export function localeToUrlLang(locale: AppLocale): UrlLangCode {
|
||||
return LOCALE_TO_URL_LANG[locale]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { LocationQuery } from 'vue-router'
|
||||
import { DEFAULT_LOCALE, isAppLocale, type AppLocale } from '@/config/appLocale'
|
||||
import { localeToUrlLang, resolveLocaleFromLangParam, type AppLocale } from '@/config/appLocale'
|
||||
import { APP_THEMES, DEFAULT_THEME, type AppTheme } from '@/theme/types'
|
||||
|
||||
export const URL_PARAM_LANG = 'lang'
|
||||
@@ -37,14 +37,7 @@ function normalizeTheme(value: string | undefined): AppTheme {
|
||||
}
|
||||
|
||||
function normalizeLocale(value: string | undefined): AppLocale {
|
||||
if (!value) {
|
||||
return DEFAULT_LOCALE
|
||||
}
|
||||
const normalized = value.replace('_', '-')
|
||||
if (isAppLocale(normalized)) {
|
||||
return normalized
|
||||
}
|
||||
return DEFAULT_LOCALE
|
||||
return resolveLocaleFromLangParam(value)
|
||||
}
|
||||
|
||||
export function parsePreferencesFromSearch(search: string): AppPreferences {
|
||||
@@ -82,7 +75,7 @@ export function preferencesToQuery(
|
||||
}
|
||||
}
|
||||
}
|
||||
next[URL_PARAM_LANG] = preferences.locale
|
||||
next[URL_PARAM_LANG] = localeToUrlLang(preferences.locale)
|
||||
next[URL_PARAM_THEME] = preferences.theme
|
||||
return next
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user