feat(i18n): 添加多语言支持并重构相关组件
实现国际化(i18n)支持,包括: 1. 新增i18n配置文件和中间件 2. 重构页面和组件以支持多语言 3. 添加中英日三语翻译文件 4. 修改API路由以支持语言参数 5. 更新README文档说明i18n功能 6. 添加语言切换组件 7. 调整布局和路由结构支持多语言路径
This commit is contained in:
@@ -4,12 +4,15 @@ import React from 'react';
|
||||
import { AiOutlineShareAlt } from 'react-icons/ai';
|
||||
import { useToast, ToastContainer} from './Toast'; // 确保路径正确
|
||||
import { usePathname } from 'next/navigation'; // next/navigation 用于获取当前路径
|
||||
import { useTranslation } from '../i18n/client'; // 导入 useTranslation
|
||||
|
||||
interface ShareButtonProps {
|
||||
className?: string; // 允许外部传入样式
|
||||
lang: string; // 新增 lang 属性
|
||||
}
|
||||
|
||||
const ShareButton: React.FC<ShareButtonProps> = ({ className }) => {
|
||||
const ShareButton: React.FC<ShareButtonProps> = ({ className, lang }) => {
|
||||
const { t } = useTranslation(lang, 'components'); // 初始化 useTranslation 并指定命名空间
|
||||
const { toasts, success, error, removeToast } = useToast();
|
||||
const pathname = usePathname(); // 获取当前路由路径
|
||||
|
||||
@@ -18,12 +21,12 @@ const ShareButton: React.FC<ShareButtonProps> = ({ className }) => {
|
||||
try {
|
||||
const currentUrl = window.location.origin + pathname; // 构建完整的当前页面 URL
|
||||
await navigator.clipboard.writeText(currentUrl);
|
||||
success('复制成功', '页面链接已复制到剪贴板!');
|
||||
console.log('页面链接已复制:', currentUrl); // 添加成功日志
|
||||
success(t('shareButton.copySuccess'), t('shareButton.pageLinkCopied'));
|
||||
console.log(`${t('shareButton.pageLinkCopied')}:`, currentUrl); // 添加成功日志
|
||||
} catch (err) {
|
||||
console.error('复制失败:', err); // 保留原有错误日志
|
||||
error('复制失败', '无法复制页面链接到剪贴板。');
|
||||
console.error('无法复制页面链接到剪贴板,错误信息:', err); // 添加详细错误日志
|
||||
console.error(`${t('shareButton.copyFailed')}:`, err); // 保留原有错误日志
|
||||
error(t('shareButton.copyFailed'), t('shareButton.cannotCopyPageLink'));
|
||||
console.error(`${t('shareButton.cannotCopyPageLink')}, ${t('shareButton.errorInfo')}:`, err); // 添加详细错误日志
|
||||
}
|
||||
};
|
||||
|
||||
@@ -32,7 +35,7 @@ const ShareButton: React.FC<ShareButtonProps> = ({ className }) => {
|
||||
<button
|
||||
onClick={handleShare}
|
||||
className={`text-neutral-500 hover:text-black transition-colors text-sm ${className}`}
|
||||
aria-label="分享页面"
|
||||
aria-label={t('shareButton.sharePage')}
|
||||
>
|
||||
<AiOutlineShareAlt className="w-5 h-5" />
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user