refactor(i18n): simplify internationalization to support only English
- Removed language switching functionality and related hooks. - Updated i18n configuration to default to English and support only English. - Adjusted components to hide language switcher and related features. - Ensured all language-related functions and formatting are fixed to English.
This commit is contained in:
parent
9b4458399d
commit
c855cfca82
|
|
@ -2,13 +2,9 @@
|
|||
* i18n React Hook
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useState, useCallback } from 'react';
|
||||
import { Language, languageNames } from '../i18n';
|
||||
import {
|
||||
getCurrentLanguage,
|
||||
setCurrentLanguage,
|
||||
initLanguageFromStorage,
|
||||
detectAndSetSystemLanguage,
|
||||
t,
|
||||
formatNumber,
|
||||
formatDate
|
||||
|
|
@ -34,48 +30,23 @@ export interface UseI18nReturn {
|
|||
}
|
||||
|
||||
/**
|
||||
* i18n Hook
|
||||
* i18n Hook - 固定为英语
|
||||
*/
|
||||
export const useI18n = (): UseI18nReturn => {
|
||||
const [currentLanguage, setCurrentLanguageState] = useState<Language>(getCurrentLanguage());
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [currentLanguage] = useState<Language>('en-US');
|
||||
const [loading] = useState(false); // 不需要加载,直接设为false
|
||||
|
||||
// 初始化语言设置
|
||||
useEffect(() => {
|
||||
const initLanguage = async () => {
|
||||
try {
|
||||
// 首先尝试从本地存储加载
|
||||
await initLanguageFromStorage();
|
||||
|
||||
// 如果没有保存的语言,检测系统语言
|
||||
const language = await detectAndSetSystemLanguage();
|
||||
setCurrentLanguageState(language);
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize language:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
initLanguage();
|
||||
}, []);
|
||||
|
||||
// 切换语言
|
||||
const changeLanguage = useCallback(async (language: Language) => {
|
||||
try {
|
||||
await setCurrentLanguage(language);
|
||||
setCurrentLanguageState(language);
|
||||
} catch (error) {
|
||||
console.error('Failed to change language:', error);
|
||||
throw error;
|
||||
}
|
||||
// 切换语言 - 空实现,因为只支持英语
|
||||
const changeLanguage = useCallback(async () => {
|
||||
// 不执行任何操作,因为只支持英语
|
||||
console.log('Language switching is disabled, only English is supported');
|
||||
}, []);
|
||||
|
||||
return {
|
||||
currentLanguage,
|
||||
t,
|
||||
changeLanguage,
|
||||
supportedLanguages: ['zh-CN', 'en-US', 'ja-JP', 'ko-KR'],
|
||||
supportedLanguages: ['en-US'], // 只支持英语
|
||||
languageNames,
|
||||
formatNumber,
|
||||
formatDate,
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@ export interface I18nConfig {
|
|||
}
|
||||
|
||||
export const i18nConfig: I18nConfig = {
|
||||
defaultLanguage: 'zh-CN',
|
||||
fallbackLanguage: 'zh-CN',
|
||||
supportedLanguages: ['zh-CN', 'en-US', 'ja-JP', 'ko-KR'],
|
||||
defaultLanguage: 'en-US',
|
||||
fallbackLanguage: 'en-US',
|
||||
supportedLanguages: ['en-US'],
|
||||
storageKey: 'app_language',
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ import Taro from '@tarojs/taro';
|
|||
import { Language, i18nConfig } from './index';
|
||||
import { locales } from './locales';
|
||||
|
||||
// 当前语言状态
|
||||
let currentLanguage: Language = i18nConfig.defaultLanguage;
|
||||
// 当前语言状态 - 固定为英语
|
||||
let currentLanguage: Language = 'en-US';
|
||||
|
||||
/**
|
||||
* 获取当前语言
|
||||
|
|
@ -17,37 +17,26 @@ export const getCurrentLanguage = (): Language => {
|
|||
};
|
||||
|
||||
/**
|
||||
* 设置当前语言
|
||||
* 设置当前语言 - 固定为英语
|
||||
*/
|
||||
export const setCurrentLanguage = async (language: Language): Promise<void> => {
|
||||
if (!i18nConfig.supportedLanguages.includes(language)) {
|
||||
console.warn(`Unsupported language: ${language}, fallback to ${i18nConfig.fallbackLanguage}`);
|
||||
language = i18nConfig.fallbackLanguage;
|
||||
}
|
||||
|
||||
currentLanguage = language;
|
||||
export const setCurrentLanguage = async (): Promise<void> => {
|
||||
// 始终使用英语
|
||||
currentLanguage = 'en-US';
|
||||
|
||||
// 保存到本地存储
|
||||
try {
|
||||
await Taro.setStorageSync(i18nConfig.storageKey, language);
|
||||
await Taro.setStorageSync(i18nConfig.storageKey, 'en-US');
|
||||
} catch (error) {
|
||||
console.error('Failed to save language to storage:', error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 从本地存储初始化语言
|
||||
* 从本地存储初始化语言 - 固定为英语
|
||||
*/
|
||||
export const initLanguageFromStorage = async (): Promise<Language> => {
|
||||
try {
|
||||
const savedLanguage = await Taro.getStorageSync(i18nConfig.storageKey);
|
||||
if (savedLanguage && i18nConfig.supportedLanguages.includes(savedLanguage)) {
|
||||
currentLanguage = savedLanguage;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load language from storage:', error);
|
||||
}
|
||||
|
||||
// 始终返回英语,忽略本地存储
|
||||
currentLanguage = 'en-US';
|
||||
return currentLanguage;
|
||||
};
|
||||
|
||||
|
|
@ -95,73 +84,37 @@ export const t = (key: string, params?: Record<string, string | number>): string
|
|||
};
|
||||
|
||||
/**
|
||||
* 检测系统语言并设置
|
||||
* 检测系统语言并设置 - 固定为英语
|
||||
*/
|
||||
export const detectAndSetSystemLanguage = async (): Promise<Language> => {
|
||||
try {
|
||||
const systemInfo = await Taro.getSystemInfo();
|
||||
const systemLanguage = systemInfo.language;
|
||||
|
||||
// 映射系统语言到支持的语言
|
||||
const languageMap: Record<string, Language> = {
|
||||
'zh-CN': 'zh-CN',
|
||||
'zh-Hans': 'zh-CN',
|
||||
'zh': 'zh-CN',
|
||||
'en-US': 'en-US',
|
||||
'en': 'en-US',
|
||||
'ja-JP': 'ja-JP',
|
||||
'ja': 'ja-JP',
|
||||
'ko-KR': 'ko-KR',
|
||||
'ko': 'ko-KR',
|
||||
};
|
||||
|
||||
const detectedLanguage = languageMap[systemLanguage] || i18nConfig.defaultLanguage;
|
||||
|
||||
// 如果本地存储中没有语言设置,使用检测到的语言
|
||||
const savedLanguage = await Taro.getStorageSync(i18nConfig.storageKey);
|
||||
if (!savedLanguage) {
|
||||
await setCurrentLanguage(detectedLanguage);
|
||||
return detectedLanguage;
|
||||
}
|
||||
|
||||
return getCurrentLanguage();
|
||||
} catch (error) {
|
||||
console.error('Failed to detect system language:', error);
|
||||
return i18nConfig.defaultLanguage;
|
||||
}
|
||||
// 始终返回英语,忽略系统语言检测
|
||||
currentLanguage = 'en-US';
|
||||
return 'en-US';
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化数字(根据语言环境)
|
||||
* 格式化数字(根据语言环境) - 固定为英语
|
||||
*/
|
||||
export const formatNumber = (num: number): string => {
|
||||
try {
|
||||
const locale = currentLanguage === 'zh-CN' ? 'zh-CN' :
|
||||
currentLanguage === 'ja-JP' ? 'ja-JP' :
|
||||
currentLanguage === 'ko-KR' ? 'ko-KR' : 'en-US';
|
||||
|
||||
return new Intl.NumberFormat(locale).format(num);
|
||||
return new Intl.NumberFormat('en-US').format(num);
|
||||
} catch (error) {
|
||||
return num.toString();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化日期(根据语言环境)
|
||||
* 格式化日期(根据语言环境) - 固定为英语
|
||||
*/
|
||||
export const formatDate = (date: Date, options?: Intl.DateTimeFormatOptions): string => {
|
||||
try {
|
||||
const locale = currentLanguage === 'zh-CN' ? 'zh-CN' :
|
||||
currentLanguage === 'ja-JP' ? 'ja-JP' :
|
||||
currentLanguage === 'ko-KR' ? 'ko-KR' : 'en-US';
|
||||
|
||||
const defaultOptions: Intl.DateTimeFormatOptions = {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
};
|
||||
|
||||
return new Intl.DateTimeFormat(locale, options || defaultOptions).format(date);
|
||||
return new Intl.DateTimeFormat('en-US', options || defaultOptions).format(date);
|
||||
} catch (error) {
|
||||
return date.toLocaleDateString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { useEffect, useState } from 'react';
|
|||
import { useServerSdk } from '../../hooks/index';
|
||||
import { useI18n } from '../../hooks/useI18n';
|
||||
import { i18nManager } from '../../i18n/manager';
|
||||
import LanguageSwitcher from '../../components/LanguageSwitcher';
|
||||
// import LanguageSwitcher from '../../components/LanguageSwitcher'; // 已隐藏 - 只支持英语
|
||||
import './index.css';
|
||||
export default function History() {
|
||||
const { t } = useI18n();
|
||||
|
|
@ -127,7 +127,7 @@ export default function History() {
|
|||
<View className="history-header">
|
||||
<View className="header-content">
|
||||
<Text className="history-title">{t('navigation.mine')}</Text>
|
||||
<LanguageSwitcher className="language-switcher-compact" />
|
||||
{/* 语言切换器已隐藏 - 只支持英语 */}
|
||||
</View>
|
||||
</View>
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import { useSdk, useServerSdk } from '../../hooks/index';
|
|||
import { useImageDetectionTaskManager, ImageAuditResult, AuditConclusion } from '../../hooks/useImageDetectionTaskManager';
|
||||
import { useI18n } from '../../hooks/useI18n';
|
||||
import { i18nManager } from '../../i18n/manager';
|
||||
import LanguageSwitcher from '../../components/LanguageSwitcher';
|
||||
// import LanguageSwitcher from '../../components/LanguageSwitcher'; // 已隐藏 - 只支持英语
|
||||
|
||||
import './index.css';
|
||||
|
||||
|
|
@ -103,10 +103,7 @@ export default function Home() {
|
|||
|
||||
return (
|
||||
<View className="home">
|
||||
{/* 语言切换按钮 - 右上角浮动 */}
|
||||
<View className="language-switcher-float">
|
||||
<LanguageSwitcher className="compact" />
|
||||
</View>
|
||||
{/* 语言切换按钮已隐藏 - 只支持英语 */}
|
||||
|
||||
<ScrollView
|
||||
className="home-scroll"
|
||||
|
|
|
|||
Loading…
Reference in New Issue