smart-home/smart-home-app/utils/i18n.js
2026-02-26 09:16:34 +08:00

329 lines
8.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 国际化工具类
* 支持中文和英文切换
*/
// 语言包
const messages = {
zh: {
// 通用
common: {
confirm: '确认',
cancel: '取消',
save: '保存',
delete: '删除',
edit: '编辑',
add: '添加',
back: '返回',
refresh: '刷新',
loading: '加载中...',
success: '成功',
error: '错误',
warning: '警告',
info: '信息',
settings: '设置',
status: '状态',
online: '在线',
offline: '离线',
connected: '已连接',
disconnected: '未连接'
},
// 主页
home: {
title: '智能家居',
welcome: '欢迎使用萤火虫智能家居系统',
deviceCount: '设备数量',
roomCount: '房间数量',
sceneCount: '场景数量'
},
// 设备管理
device: {
title: '设备管理',
list: '设备列表',
add: '添加设备',
detail: '设备详情',
name: '设备名称',
type: '设备类型',
room: '所在房间',
status: '设备状态',
temperature: '温度',
humidity: '湿度',
battery: '电池电量'
},
// 厨房监控
kitchen: {
title: '厨房监控',
monitor: '实时监控',
history: '报警历史',
settings: '高级选项',
temperature: '温度',
fire: '火灾检测',
person: '人员检测',
alarm: '报警',
normal: '正常',
warning: '警告',
danger: '危险'
},
// 设置页面
settings: {
title: '设置',
deviceConfig: '设备配置',
network: '网络设置',
language: '语言设置',
about: '关于',
version: '版本信息',
// 设备配置
alarmThreshold: '报警阈值',
timing: '时间设置',
frequency: '频率设置',
sound: '声音设置',
light: '灯光设置',
// 时间设置
timingTitle: '时间设置',
timingDesc: 'StillSense等待多长时间后进行检查',
timingShorter: '较短',
timingDefault: '默认',
timingLonger: '较长',
timingNote: '这影响StillSense的耐心程度——而不是它如何判断情况。',
// 频率设置
frequencyTitle: '频率设置',
frequencyDesc: 'StillSense应该多久重复一次检查',
frequencyOnce: '一次',
frequencyFewMinutes: '每隔几分钟',
frequencyUntilAck: '直到确认',
frequencyNote: 'StillSense不会过于频繁地重复检查。',
// 声音设置
soundTitle: '声音设置',
soundSoft: '轻柔',
soundNormal: '正常',
soundClear: '清晰',
soundNote: '声音表示紧急程度,而不是危险程度。',
// 灯光设置
lightTitle: '灯光设置',
lightDirectional: '方向LED',
lightIntensity: '灯光强度',
lightOn: '开启',
lightOff: '关闭',
lightLow: '低',
lightMedium: '中',
lightHigh: '高',
lightNote: '灯光帮助您快速看到需要注意的地方。'
}
},
en: {
// Common
common: {
confirm: 'Confirm',
cancel: 'Cancel',
save: 'Save',
delete: 'Delete',
edit: 'Edit',
add: 'Add',
back: 'Back',
refresh: 'Refresh',
loading: 'Loading...',
success: 'Success',
error: 'Error',
warning: 'Warning',
info: 'Info',
settings: 'Settings',
status: 'Status',
online: 'Online',
offline: 'Offline',
connected: 'Connected',
disconnected: 'Disconnected'
},
// Home
home: {
title: 'Smart Home',
welcome: 'Welcome to Firefly Smart Home System',
deviceCount: 'Device Count',
roomCount: 'Room Count',
sceneCount: 'Scene Count'
},
// Device Management
device: {
title: 'Device Management',
list: 'Device List',
add: 'Add Device',
detail: 'Device Details',
name: 'Device Name',
type: 'Device Type',
room: 'Room',
status: 'Device Status',
temperature: 'Temperature',
humidity: 'Humidity',
battery: 'Battery Level'
},
// Kitchen Monitor
kitchen: {
title: 'Kitchen Monitor',
monitor: 'Real-time Monitor',
history: 'Alarm History',
settings: 'Advanced Options',
temperature: 'Temperature',
fire: 'Fire Detection',
person: 'Person Detection',
alarm: 'Alarm',
normal: 'Normal',
warning: 'Warning',
danger: 'Danger'
},
// Settings
settings: {
title: 'Settings',
deviceConfig: 'Device Configuration',
network: 'Network Settings',
language: 'Language Settings',
about: 'About',
version: 'Version Info',
// Device Configuration
alarmThreshold: 'Alarm Threshold',
timing: 'Timing Settings',
frequency: 'Frequency Settings',
sound: 'Sound Settings',
light: 'Light Settings',
// Timing Settings
timingTitle: 'Timing',
timingDesc: 'How long should StillSense wait before checking in?',
timingShorter: 'Shorter',
timingDefault: 'Default',
timingLonger: 'Longer',
timingNote: 'This affects how patient StillSense is — not how it judges a situation.',
// Frequency Settings
frequencyTitle: 'Frequency',
frequencyDesc: 'How often should StillSense repeat a check-in?',
frequencyOnce: 'Once',
frequencyFewMinutes: 'Every few minutes',
frequencyUntilAck: 'Until acknowledged',
frequencyNote: 'StillSense won\'t repeat check-ins too often.',
// Sound Settings
soundTitle: 'Sound',
soundSoft: 'Soft',
soundNormal: 'Normal',
soundClear: 'Clear',
soundNote: 'Sound indicates urgency, not danger.',
// Light Settings
lightTitle: 'Light',
lightDirectional: 'Directional LED',
lightIntensity: 'Light intensity',
lightOn: 'On',
lightOff: 'Off',
lightLow: 'Low',
lightMedium: 'Medium',
lightHigh: 'High',
lightNote: 'Lights help you quickly see where attention is needed.'
}
}
}
// 当前语言
let currentLanguage = 'zh'
// 国际化工具类
class I18n {
constructor() {
// 从本地存储读取语言设置
this.loadLanguage()
}
// 加载语言设置
loadLanguage() {
try {
const savedLang = uni.getStorageSync('app_language')
if (savedLang && (savedLang === 'zh' || savedLang === 'en')) {
currentLanguage = savedLang
}
} catch (e) {
console.warn('Failed to load language setting:', e)
}
}
// 保存语言设置
saveLanguage(lang) {
try {
uni.setStorageSync('app_language', lang)
currentLanguage = lang
} catch (e) {
console.error('Failed to save language setting:', e)
}
}
// 获取当前语言
getCurrentLanguage() {
return currentLanguage
}
// 设置语言
setLanguage(lang) {
if (lang === 'zh' || lang === 'en') {
this.saveLanguage(lang)
return true
}
return false
}
// 获取翻译文本
t(key) {
const keys = key.split('.')
let value = messages[currentLanguage]
for (const k of keys) {
if (value && typeof value === 'object' && k in value) {
value = value[k]
} else {
// 如果当前语言没有找到,尝试中文
if (currentLanguage !== 'zh') {
let fallback = messages.zh
for (const fk of keys) {
if (fallback && typeof fallback === 'object' && fk in fallback) {
fallback = fallback[fk]
} else {
fallback = key
break
}
}
return fallback
}
return key
}
}
return value || key
}
// 获取所有支持的语言
getSupportedLanguages() {
return [
{ code: 'zh', name: '中文', nativeName: '中文' },
{ code: 'en', name: 'English', nativeName: 'English' }
]
}
}
// 创建全局实例
const i18n = new I18n()
// 导出
export default i18n
export { I18n }