tangjunhao 3 månader sedan
förälder
incheckning
1017662910
4 ändrade filer med 231 tillägg och 18 borttagningar
  1. 95 0
      public/locales/en.json
  2. 96 0
      public/locales/zh-CN.json
  3. 27 11
      src/utils/i18n.js
  4. 13 7
      src/views/preference/index.vue

+ 95 - 0
public/locales/en.json

@@ -0,0 +1,95 @@
+{
+  "message": {
+    "hello": "Hello World",
+    "welcome": "Welcome to our app",
+    "confirmDelete": "Are you sure to delete the selected items?",
+    "deleteSuccess": "Delete successful",
+    "deleteFailed": "Delete failed",
+    "deleteCancelled": "Deletion cancelled",
+    "logoutSuccess": "Logout successful"
+  },
+  "common": {
+    "tip": "Tip",
+    "confirm": "Confirm",
+    "ok":"OK",
+    "cancel": "Cancel"
+  },
+  "preferences": {
+    "title": "Preferences",
+    "theme": "Theme",
+    "language": "Language",
+    "notifications": "Notifications",
+    "privacy": "Privacy Settings"
+  },
+  "login": {
+    "title": "Login",
+    "subtitle": "Web-Based Engine System-Level Simulation Software",
+    "username": "Username",
+    "inputusername": "Please enter your username",
+    "inputpassword": "Please enter your password",
+    "checknamepwd": "Please enter username and password",
+    "loginError": "Login failed, please check your username and password",
+    "password": "Password",
+    "rememberMe": "Remember Me",
+    "loginButton": "Login",
+    "forgotPassword": "Forgot Password?",
+    "register": "Register"
+  },
+  "index": {
+    "title": "Home",
+    "subtitle": "Web-Based Engine System-Level Simulation Software",
+    "headetag": "PLATFORM",
+    "project": "Project",
+    "preferences": "Preferences",
+    "userinfo": "User Info",
+    "changePassword": "Change Password",
+    "logout": "Logout"
+  },
+  "project": {
+    "open": "OPEN",
+    "create": "New Project",
+    "newcompound": "New Compound",
+    "newlibrary": "New Library",
+    "projectlist": "Project List",
+    "number": "Number",
+    "name": "Project Name",
+    "dirsize": "Size",
+    "updateTime": "Last Modified",
+    "uname": "Author",
+    "keywords": "Keywords",
+    "description": "Description",
+    "total": "Total",
+    "selectAll": "Select All",
+    "delete": "Delete",
+    "ok": "OK"
+  },
+  "dialog": {
+    "ok": "OK",
+    "cancel": "Cancel",
+    "error": "Error",
+    "success": "Success",
+    "warning": "Warning",
+    "info": "Info",
+    "loading": "Loading",
+    "new": "New",
+    "edit": "Edit",
+    "delete": "Delete",
+    "save": "Save",
+    "userinfo": "User Info",
+    "changePassword": "Change Password",
+    "oldPassword": "Old Password",
+    "newPassword": "New Password",
+    "confirmNewPassword": "Confirm Again",
+    "inputOldPassword": "Please enter old password",
+    "inputNewPassword": "Please enter new password",
+    "inputConfirmNewPassword": "Please confirm new password",
+    "passwordMismatch": "Passwords do not match",
+    "passwordChangeSuccess": "Password changed successfully",
+    "passwordChangeFailed": "Password change failed",
+    "username": "Username",
+    "nickname": "Nickname",
+    "email": "Email",
+    "regTime": "Registration Time",
+    "mobileNo": "Mobile Number"
+  }
+}

+ 96 - 0
public/locales/zh-CN.json

@@ -0,0 +1,96 @@
+{
+  "message": {
+    "hello": "你好世界",
+    "welcome": "欢迎使用我们的应用",
+    "confirmDelete": "您确定要删除选中的项目吗?",
+    "deleteSuccess": "删除成功",
+    "deleteFailed": "删除失败",
+    "deleteCancelled": "已取消删除",
+    "logoutSuccess": "退出登录成功"
+  },
+  "common": {
+    "tip": "提示",
+    "confirm": "确认",
+    "ok": "确定",
+    "cancel": "取消"
+  },
+  "preferences": {
+    "title": "偏好设置",
+    "theme": "主题",
+    "language": "语言",
+    "notifications": "通知",
+    "privacy": "隐私设置"
+  },
+  "login": {
+    "title": "登录",
+    "subtitle": "基于Web的引擎系统级仿真软件",
+    "username": "用户名",
+    "password": "密码",
+    "inputusername": "请输入用户名",
+    "inputpassword": "请输入密码",
+    "checknamepwd": "请输入用户名和密码",
+    "loginError": "登录失败,请检查您的用户名和密码",
+    "rememberMe": "记住密码",
+    "loginButton": "登录",
+    "forgotPassword": "忘记密码?",
+    "register": "注册"
+  },
+  "index": {
+    "title": "首页",
+    "subtitle": "基于Web的引擎系统级仿真软件",
+    "headetag": "平台",
+    "project": "项目",
+    "preferences": "偏好设置",
+    "userinfo": "个人信息",
+    "changePassword": "修改密码",
+    "logout": "退出登录"
+  },
+  "project": {
+    "open": "打开",
+    "create": "新建项目",
+    "newcompound": "新建化合物",
+    "newlibrary": "新建库",
+    "projectlist": "项目列表",
+    "number": "编号",
+    "name": "项目名",
+    "dirsize": "大小",
+    "updateTime": "最后修改",
+    "uname": "作者",
+    "keywords": "关键词",
+    "description": "描述",
+    "total": "总计",
+    "selectAll": "全选",
+    "delete": "删除",
+    "ok": "确定"
+  },
+  "dialog": {
+    "ok": "确认",
+    "cancel": "取消",
+    "error": "错误",
+    "success": "成功",
+    "warning": "警告",
+    "info": "提示",
+    "loading": "加载中",
+    "new": "新建",
+    "edit": "编辑",
+    "delete": "删除",
+    "save": "保存",
+    "userinfo": "个人信息",
+    "changePassword": "修改密码",
+    "oldPassword": "原密码",
+    "newPassword": "新密码",
+    "confirmNewPassword": "确认密码",
+    "inputOldPassword": "请输入原密码",
+    "inputNewPassword": "请输入新密码",
+    "inputConfirmNewPassword": "请确认新密码",
+    "passwordMismatch": "两次输入的密码不一致",
+    "passwordChangeSuccess": "密码修改成功",
+    "passwordChangeFailed": "密码修改失败",
+    "username": "用户名",
+    "nickname": "昵称",
+    "email": "邮箱",
+    "regTime": "注册时间",
+    "mobileNo": "手机号码"
+  }
+
+}

+ 27 - 11
src/utils/i18n.js

@@ -1,18 +1,34 @@
-//中英文国际化配置
 import { createI18n } from 'vue-i18n'
-import en from '../locales/en.json'
-import zhCN from '../locales/zh-CN.json'
 
-const messages = {
-  en,
-  'zh-CN': zhCN
-}
+// 缓存已加载语言包,避免重复加载
+const loadedLanguages = []
 
 const i18n = createI18n({
-  legacy: false, // 使用Composition API模式
-  locale: 'zh-CN', // 默认语言
-  fallbackLocale: 'en', // 备用语言
-  messages
+  legacy: false,
+  locale: localStorage.getItem('locale') || 'zh-CN',
+  fallbackLocale: 'en',
+  messages: {}  // 初始为空,动态加载
 })
 
+// 动态加载语言包函数
+export async function loadLocaleMessages(locale) {
+  if (loadedLanguages.includes(locale)) {
+    return Promise.resolve()
+  }
+  try {
+    const response = await fetch(`/locales/${locale}.json`)
+    if (!response.ok) throw new Error('Failed to load locale messages')
+    const messages = await response.json()
+    i18n.global.setLocaleMessage(locale, messages)
+    loadedLanguages.push(locale)
+    return Promise.resolve()
+  } catch (error) {
+    console.error(error)
+    return Promise.reject(error)
+  }
+}
+
+// 应用初始化时,加载默认语言包
+loadLocaleMessages(i18n.global.locale.value)
+
 export default i18n

+ 13 - 7
src/views/preference/index.vue

@@ -31,6 +31,7 @@
 
 import { getCurrentInstance } from 'vue'
 import { useI18n } from 'vue-i18n'
+import { loadLocaleMessages } from '@/utils/i18n'
 import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
 import en from 'element-plus/dist/locale/en.mjs'
 
@@ -43,13 +44,18 @@ const languages = [
   { value: 'en', label: 'English' }
 ]
 
-watch(currentLang, (newVal) => {
-  locale.value = newVal
-  localStorage.setItem('locale', newVal)
-  
-  // 设置 Element Plus 的语言
-  const elementLocale = newVal === 'zh-CN' ? zhCn : en
-  appContext.config.globalProperties.$ELEMENT = { locale: elementLocale }
+watch(currentLang, async (newVal) => {
+  try {
+    await loadLocaleMessages(newVal) // 动态加载语言包
+    locale.value = newVal
+    localStorage.setItem('locale', newVal)
+
+    // 切换 Element Plus 语言
+    const elementLocale = newVal === 'zh-CN' ? zhCn : en
+    appContext.config.globalProperties.$ELEMENT = { locale: elementLocale }
+  } catch (error) {
+    console.error('切换语言失败', error)
+  }
 })