fix(CopyButton): replace hardcoded Chinese strings with i18n t() calls
Use useTranslation('common') for tooltip, copy empty/success/error
toast messages.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import { copyTextToClipboard } from '@/utils/clipboard';
|
|||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
import { buttonVariants, type ButtonProps } from '@/components/ui/button';
|
import { buttonVariants, type ButtonProps } from '@/components/ui/button';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
|
interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
|
||||||
text: string;
|
text: string;
|
||||||
@@ -12,12 +13,13 @@ interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
|
|||||||
|
|
||||||
export const CopyButton: React.FC<CopyButtonProps> = ({
|
export const CopyButton: React.FC<CopyButtonProps> = ({
|
||||||
text,
|
text,
|
||||||
tooltip = '复制',
|
tooltip,
|
||||||
variant = 'ghost',
|
variant = 'ghost',
|
||||||
size = 'sm',
|
size = 'sm',
|
||||||
className,
|
className,
|
||||||
...props
|
...props
|
||||||
}) => {
|
}) => {
|
||||||
|
const { t } = useTranslation('common');
|
||||||
const [copied, setCopied] = useState(false);
|
const [copied, setCopied] = useState(false);
|
||||||
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
|
||||||
@@ -31,18 +33,18 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
|
|||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
||||||
if (!text) {
|
if (!text) {
|
||||||
toast.error('无内容可复制');
|
toast.error(t('messages.copyEmpty'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const success = await copyTextToClipboard(text);
|
const success = await copyTextToClipboard(text);
|
||||||
if (success) {
|
if (success) {
|
||||||
toast.success('复制成功');
|
toast.success(t('messages.copySuccess'));
|
||||||
setCopied(true);
|
setCopied(true);
|
||||||
if (timerRef.current) clearTimeout(timerRef.current);
|
if (timerRef.current) clearTimeout(timerRef.current);
|
||||||
timerRef.current = setTimeout(() => setCopied(false), 1500);
|
timerRef.current = setTimeout(() => setCopied(false), 1500);
|
||||||
} else {
|
} else {
|
||||||
toast.error('复制失败');
|
toast.error(t('messages.copyError'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,7 +52,7 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={handleCopy}
|
onClick={handleCopy}
|
||||||
title={tooltip}
|
title={tooltip ?? t('buttons.copy')}
|
||||||
className={cn(
|
className={cn(
|
||||||
buttonVariants({ variant, size }),
|
buttonVariants({ variant, size }),
|
||||||
'h-8 w-8',
|
'h-8 w-8',
|
||||||
|
|||||||
Reference in New Issue
Block a user