Initial commit
This commit is contained in:
10
locales/en.json
Normal file
10
locales/en.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"welcome": "Welcome",
|
||||
"greeting": "Hello, {{name}}!",
|
||||
"login": "Login123",
|
||||
"username": "Username",
|
||||
"password": "Password",
|
||||
"login_success": "Login Success",
|
||||
"login_failed": "Login Failed",
|
||||
"login_error_message": "Login Failed, Please Try Again"
|
||||
}
|
||||
60
locales/i18n.js
Normal file
60
locales/i18n.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import i18n from 'i18next';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
import * as RNLocalize from 'react-native-localize';
|
||||
import AsyncStorage from '@/storage/index';
|
||||
// 导入语言包
|
||||
import en from './en.json';
|
||||
import zh from './zh.json';
|
||||
console.log('🚀 ~ RNLocalize:', RNLocalize)
|
||||
|
||||
// 导入语言资源
|
||||
const resources = {
|
||||
en: { translation: en },
|
||||
zh: { translation: zh }
|
||||
};
|
||||
|
||||
const languageDetector = {
|
||||
type: 'languageDetector',
|
||||
async: true,
|
||||
detect: async callback => {
|
||||
try {
|
||||
// 从本地存储读取用户选择的语言
|
||||
const saveLng = await AsyncStorage.getItemAsync('@APP_LANGUAGE');
|
||||
const lng = saveLng ? saveLng.lng : null;
|
||||
|
||||
// 如果没有选择的语言,使用设备首选语言
|
||||
const bestLanguageOption = RNLocalize.getLocales()[0].languageCode || 'en';
|
||||
callback(lng || bestLanguageOption);
|
||||
} catch (error) {
|
||||
console.error("Failed to detect or load the language", error);
|
||||
}
|
||||
},
|
||||
init: () => {},
|
||||
cacheUserLanguage: async lng => {
|
||||
console.log('🚀 ~ lng:', lng)
|
||||
try {
|
||||
// 将用户选择的语言存储到本地
|
||||
await AsyncStorage.setItemAsync('@APP_LANGUAGE', {lng});
|
||||
} catch (error) {
|
||||
console.error("Failed to save the user's language choice", error);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
i18n
|
||||
.use(languageDetector)
|
||||
.use(initReactI18next)
|
||||
.init({
|
||||
resources,
|
||||
fallbackLng: 'en',
|
||||
compatibilityJSON: 'v3',
|
||||
interpolation: {
|
||||
escapeValue: false, // react already saves from xss
|
||||
},
|
||||
});
|
||||
|
||||
export default i18n;
|
||||
|
||||
|
||||
|
||||
10
locales/zh.json
Normal file
10
locales/zh.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"welcome": "欢迎",
|
||||
"greeting": "你好, {{name}}!",
|
||||
"login": "登录",
|
||||
"username": "用户名",
|
||||
"password": "密码",
|
||||
"login_success": "登录成功",
|
||||
"login_failed": "登录失败",
|
||||
"login_error_message": "登录失败,请重试"
|
||||
}
|
||||
Reference in New Issue
Block a user