feat(I18n) 添加I18n相关功能,支持中文和英文
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { fetchUserIp } from '../api/latency'
|
||||
import { useLanguage } from '../contexts/LanguageContext'
|
||||
import './IpInput.css'
|
||||
|
||||
interface IpInputProps {
|
||||
@@ -19,19 +20,20 @@ export default function IpInput({ onTest, testing }: IpInputProps) {
|
||||
const [target, setTarget] = useState('')
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState('')
|
||||
const { t } = useLanguage()
|
||||
|
||||
useEffect(() => {
|
||||
fetchUserIp()
|
||||
.then(setTarget)
|
||||
.catch(() => setError('Failed to detect IP'))
|
||||
.catch(() => setError(t('IP检测失败', 'Failed to detect IP')))
|
||||
.finally(() => setLoading(false))
|
||||
}, [])
|
||||
}, [t])
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
const trimmed = target.trim()
|
||||
if (!isValidTarget(trimmed)) {
|
||||
setError('Invalid IP address or domain')
|
||||
setError(t('无效的IP地址或域名', 'Invalid IP address or domain'))
|
||||
return
|
||||
}
|
||||
setError('')
|
||||
@@ -48,7 +50,7 @@ export default function IpInput({ onTest, testing }: IpInputProps) {
|
||||
setTarget(e.target.value)
|
||||
setError('')
|
||||
}}
|
||||
placeholder={loading ? 'Detecting IP...' : 'Enter IP or domain (e.g., 8.8.8.8 or google.com)'}
|
||||
placeholder={loading ? t('正在检测IP...', 'Detecting IP...') : t('输入IP或域名(如 8.8.8.8 或 google.com)', 'Enter IP or domain (e.g., 8.8.8.8 or google.com)')}
|
||||
className={`ip-input ${error ? 'ip-input-error' : ''}`}
|
||||
disabled={testing || loading}
|
||||
/>
|
||||
@@ -58,10 +60,10 @@ export default function IpInput({ onTest, testing }: IpInputProps) {
|
||||
{testing ? (
|
||||
<>
|
||||
<span className="spinner" />
|
||||
Testing...
|
||||
{t('测试中...', 'Testing...')}
|
||||
</>
|
||||
) : (
|
||||
'Test Latency'
|
||||
t('开始测试', 'Test Latency')
|
||||
)}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user