105 lines
4.0 KiB
React
105 lines
4.0 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
import { Button, Dropdown } from '@douyinfe/semi-ui';
|
|
import { Globe } from 'lucide-react';
|
|
import { CN, GB, JP, KR, TH, VN } from 'country-flag-icons/react/3x2';
|
|
import { useActualTheme } from '../../../context/Theme';
|
|
import { normalizeLanguage } from '../../../i18n/language';
|
|
import { orderEnabledLanguages } from '../../../i18n/catalog';
|
|
import {
|
|
LANG_BOOT_UPDATED_EVENT,
|
|
getBootConfig,
|
|
} from '../../../i18n/bootstrap';
|
|
|
|
// 语言目录里的 flag 国家码 → 旗帜组件
|
|
const FLAGS = { CN, GB, JP, KR, TH, VN };
|
|
|
|
const LanguageSelector = ({ currentLang, onLanguageChange, t }) => {
|
|
// i18n.language 可能带地区码(如 zh-CN / en-US),高亮判断先归一化;
|
|
// 繁体靠地区码区分,不能简单取主语言
|
|
const lang = normalizeLanguage(currentLang);
|
|
// 与 ThemeToggle 同规范:图标颜色随实际主题动态取色(Semi 暗色由
|
|
// body[theme-mode] 驱动,Tailwind dark: 类在此不可靠),菜单样式走
|
|
// semi-color token 天然亮暗自适应
|
|
const actualTheme = useActualTheme();
|
|
|
|
// 首访时后台判定可能晚于顶栏首次渲染:结果写入缓存后重新读取启用语言,
|
|
// 避免列出后台已停用的语言
|
|
const [, setBootVersion] = useState(0);
|
|
useEffect(() => {
|
|
const handleBootUpdated = () => setBootVersion((version) => version + 1);
|
|
window.addEventListener(LANG_BOOT_UPDATED_EVENT, handleBootUpdated);
|
|
return () =>
|
|
window.removeEventListener(LANG_BOOT_UPDATED_EVENT, handleBootUpdated);
|
|
}, []);
|
|
|
|
const getItemClassName = (isSelected) =>
|
|
`!flex !items-center !gap-2 !px-3 !py-1.5 !text-sm ${
|
|
isSelected
|
|
? '!bg-semi-color-primary-light-default !font-semibold'
|
|
: 'hover:!bg-semi-color-fill-1'
|
|
}`;
|
|
|
|
// 展示哪些语言、按什么顺序由后台「语言与地区」配置决定(经 /api/i18n/locale 缓存下发)
|
|
const languages = orderEnabledLanguages(getBootConfig()?.enabled_languages);
|
|
|
|
return (
|
|
<Dropdown
|
|
position='bottomRight'
|
|
render={
|
|
<Dropdown.Menu>
|
|
{languages.map(({ code, endonym, flag }) => {
|
|
const Flag = FLAGS[flag];
|
|
return (
|
|
<Dropdown.Item
|
|
key={code}
|
|
onClick={() => onLanguageChange(code)}
|
|
className={getItemClassName(lang === code)}
|
|
>
|
|
{Flag && <Flag title={endonym} className='!w-5 !h-auto' />}
|
|
<span>{endonym}</span>
|
|
</Dropdown.Item>
|
|
);
|
|
})}
|
|
</Dropdown.Menu>
|
|
}
|
|
>
|
|
<span className='inline-flex'>
|
|
<Button
|
|
icon={<Globe size={20} />}
|
|
aria-label={t('common.changeLanguage')}
|
|
theme='borderless'
|
|
type='tertiary'
|
|
className='!w-8 !h-8 sm:!w-9 sm:!h-9 !flex !items-center !justify-center hover:!bg-brand/10 dark:hover:!bg-brand/15 hover:!text-brand-deep dark:hover:!text-brand-from !rounded-full !transition-all !duration-200 hover:!scale-[1.08] active:!scale-95'
|
|
style={{
|
|
color:
|
|
actualTheme === 'dark'
|
|
? 'rgba(243,244,246,0.9)'
|
|
: 'rgba(75,85,99,1)',
|
|
}}
|
|
/>
|
|
</span>
|
|
</Dropdown>
|
|
);
|
|
};
|
|
|
|
export default LanguageSelector;
|