mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 04:40:06 +08:00
Prefer a supported saved language over the cache and browser locale. Align frontend alias resolution with Rust and ignore unsupported saved values when the layout mounts so they cannot overwrite the fallback. In Rust, walk borrowed prefixes instead of rebuilding strings. This preserves locale resolution and removes quadratic work on long tags. Across all 14 shipped locale IDs (equally weighted), median lookup time fell from 40.0 to 22.4 ns and heap allocations fell from 4 to 2 per lookup. A synthetic 64 KiB tag fell from 771 ms to 240 µs.
33 lines
757 B
TypeScript
33 lines
757 B
TypeScript
import { useCallback } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { changeLanguage, resolveSupportedLanguage } from '@/services/i18n'
|
|
|
|
export const useI18n = () => {
|
|
const { i18n } = useTranslation()
|
|
|
|
const switchLanguage = useCallback(
|
|
async (language: string) => {
|
|
const targetLanguage = resolveSupportedLanguage(language)
|
|
|
|
if (!targetLanguage) {
|
|
console.warn(`Unsupported language: ${language}`)
|
|
return
|
|
}
|
|
|
|
if (i18n.language === targetLanguage) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
await changeLanguage(targetLanguage)
|
|
} catch (error) {
|
|
console.error('Failed to change language:', error)
|
|
}
|
|
},
|
|
[i18n.language],
|
|
)
|
|
|
|
return { switchLanguage }
|
|
}
|