From 52131ade2abc970729416c3fba4e0cc13c80ecfe Mon Sep 17 00:00:00 2001 From: zhangshun <453905631@qq.com> Date: Wed, 27 May 2026 17:12:02 +0800 Subject: [PATCH] =?UTF-8?q?i18n=20=E4=BB=8E=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=A5=BD=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/lang/en-US.js | 14 +++++ src/lang/index.js | 36 +++++++++++ src/lang/zh-CN.js | 14 +++++ src/main.js | 3 +- .../InquiryTransitInformation/index.vue | 26 ++++---- src/views/layout/components/Navbar.vue | 61 +++++++++++++++++-- 7 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 src/lang/en-US.js create mode 100644 src/lang/index.js create mode 100644 src/lang/zh-CN.js diff --git a/package.json b/package.json index e2df713..a500e7b 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "vue": "^2.7.16", "vue-count-to": "^1.0.13", "vue-direction-key": "^1.0.5", + "vue-i18n": "^8.28.2", "vue-json-viewer": "^2.2.22", "vue-print-nb": "^1.7.5", "vue-router": "^3.6.5", diff --git a/src/lang/en-US.js b/src/lang/en-US.js new file mode 100644 index 0000000..fcf72ea --- /dev/null +++ b/src/lang/en-US.js @@ -0,0 +1,14 @@ +import enLocale from 'element-ui/lib/locale/lang/en' + +export default { + ...enLocale, + common: { + confirm: 'Confirm', + cancel: 'Cancel', + home: 'Home', + logout: 'Logout', + language: 'Language', + chinese: 'Chinese', + english: 'English' + } +} diff --git a/src/lang/index.js b/src/lang/index.js new file mode 100644 index 0000000..1a86b8d --- /dev/null +++ b/src/lang/index.js @@ -0,0 +1,36 @@ +import Vue from 'vue' +import VueI18n from 'vue-i18n' +import elementLocale from 'element-ui/lib/locale' +import zhCN from './zh-CN' +import enUS from './en-US' + +Vue.use(VueI18n) + +export const DEFAULT_LOCALE = 'zh-CN' +export const LOCALE_STORAGE_KEY = 'language' +export const SUPPORT_LOCALES = ['zh-CN', 'en-US'] + +function getSavedLocale() { + const savedLocale = localStorage.getItem(LOCALE_STORAGE_KEY) + return SUPPORT_LOCALES.indexOf(savedLocale) > -1 ? savedLocale : DEFAULT_LOCALE +} + +const i18n = new VueI18n({ + locale: getSavedLocale(), + fallbackLocale: DEFAULT_LOCALE, + silentTranslationWarn: true, + messages: { + 'zh-CN': zhCN, + 'en-US': enUS + } +}) + +elementLocale.i18n((key, value) => i18n.t(key, value)) + +export function setLocale(locale) { + const nextLocale = SUPPORT_LOCALES.indexOf(locale) > -1 ? locale : DEFAULT_LOCALE + i18n.locale = nextLocale + localStorage.setItem(LOCALE_STORAGE_KEY, nextLocale) +} + +export default i18n diff --git a/src/lang/zh-CN.js b/src/lang/zh-CN.js new file mode 100644 index 0000000..199e628 --- /dev/null +++ b/src/lang/zh-CN.js @@ -0,0 +1,14 @@ +import zhLocale from 'element-ui/lib/locale/lang/zh-CN' + +export default { + ...zhLocale, + common: { + confirm: '确定', + cancel: '取消', + home: '主页', + logout: '退出', + language: '语言', + chinese: '中文', + english: 'English' + } +} diff --git a/src/main.js b/src/main.js index 312e9ac..a425d62 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,5 @@ import Vue from 'vue' -console.log(123) import 'normalize.css/normalize.css' // A modern alternative to CSS resets import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' @@ -11,6 +10,7 @@ import '@/icon/iconfont.css' import App from './App' import router from './router' import store from './store' +import i18n from './lang' import * as echarts from 'echarts' import '@/icons' // icon import '@/permission' // permission control @@ -49,6 +49,7 @@ new Vue({ el: '#app', router, store, + i18n, render: h => h(App) }) diff --git a/src/views/QualityAssurance/InquiryTransitInformation/index.vue b/src/views/QualityAssurance/InquiryTransitInformation/index.vue index 02f9386..a5b35a7 100644 --- a/src/views/QualityAssurance/InquiryTransitInformation/index.vue +++ b/src/views/QualityAssurance/InquiryTransitInformation/index.vue @@ -120,14 +120,19 @@ tooltip-effect="dark"> + + + - + @@ -135,11 +140,7 @@ {{ scope.row.机型 }} - - - - - + @@ -181,11 +182,11 @@ {{ scope.row.是否合格 }} - - - + + + + + @@ -286,7 +287,6 @@ export default { var param = [] var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param) this.ExecDatabase(Data).then(response => { - if (response.data.length) this.stationNumberValue = response.data[0].工位号 for (let i = 0; i < response.data.length; i++) { this.stationNumber.push({ label: `【${response.data[i].工位号}】${response.data[i].工位名称}`, diff --git a/src/views/layout/components/Navbar.vue b/src/views/layout/components/Navbar.vue index a97eabe..fbc6a51 100644 --- a/src/views/layout/components/Navbar.vue +++ b/src/views/layout/components/Navbar.vue @@ -3,6 +3,22 @@ + + + {{ currentLocaleLabel }} + + + + + {{ item.label }} + + +
@@ -11,11 +27,11 @@ - 主页 + {{ $t('common.home') }} - 退出 + {{ $t('common.logout') }} @@ -41,6 +57,7 @@ import { mapGetters } from 'vuex' import Breadcrumb from '@/components/Breadcrumb' import Hamburger from '@/components/Hamburger' +import { setLocale, SUPPORT_LOCALES } from '@/lang' // import { initApproval, initApprovalOffline } from '@/api/PurchasingCenter/FundApproval' // import { searchUrgentItem } from '@/api/ManufacturingCenter/InitialProduction' // import { searchTable1 as searchTableInvoice } from '@/api/PurchasingCenter/InvoiceSettlementApprove' @@ -92,7 +109,11 @@ export default { PurchaseContractSearch: null, DeliveryNotice: null, ScrapReplenishment: null, - OrderConfirmationShipment: null + OrderConfirmationShipment: null, + localeOptions: [ + { value: 'zh-CN', label: '中文' }, + { value: 'en-US', label: 'English' } + ] } }, computed: { @@ -103,7 +124,14 @@ export default { 'id', 'token', 'token' - ]) + ]), + currentLocale() { + return this.$i18n.locale + }, + currentLocaleLabel() { + const current = this.localeOptions.find(item => item.value === this.currentLocale) + return current ? current.label : this.$t('common.language') + } }, created() { // this.showOrder() @@ -433,6 +461,11 @@ export default { toggleSideBar() { this.$store.dispatch('ToggleSideBar') }, + changeLanguage(locale) { + if (SUPPORT_LOCALES.indexOf(locale) === -1 || locale === this.currentLocale) return + setLocale(locale) + this.$message.success(this.$t('common.language') + ': ' + this.currentLocaleLabel) + }, logout() { this.$store.dispatch('FedLogOut').then(() => { location.reload() // 为了重新实例化vue-router对象 避免bug @@ -459,6 +492,26 @@ export default { top: 16px; color: red; } + .language-container { + height: 50px; + display: inline-block; + position: absolute; + right: 95px; + top: 0; + .language-button { + height: 50px; + min-width: 76px; + padding: 0 8px; + color: #fff; + font-size: 13px; + line-height: 50px; + cursor: pointer; + } + .language-button:hover, + .language-button:focus { + color: #f2f6fc; + } + } .avatar-container { height: 50px; display: inline-block;