1. 布局
<!--国际化-->
<el-dropdown trigger="click" @command="configLang">
<el-button circle>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512"><path d="M478.33 433.6l-90-218a22 22 0 0 0-40.67 0l-90 218a22 22 0 1 0 40.67 16.79L316.66 406h102.67l18.33 44.39A22 22 0 0 0 458 464a22 22 0 0 0 20.32-30.4zM334.83 362L368 281.65L401.17 362z" fill="currentColor"></path><path d="M267.84 342.92a22 22 0 0 0-4.89-30.7c-.2-.15-15-11.13-36.49-34.73c39.65-53.68 62.11-114.75 71.27-143.49H330a22 22 0 0 0 0-44H214V70a22 22 0 0 0-44 0v20H54a22 22 0 0 0 0 44h197.25c-9.52 26.95-27.05 69.5-53.79 108.36c-31.41-41.68-43.08-68.65-43.17-68.87a22 22 0 0 0-40.58 17c.58 1.38 14.55 34.23 52.86 83.93c.92 1.19 1.83 2.35 2.74 3.51c-39.24 44.35-77.74 71.86-93.85 80.74a22 22 0 1 0 21.07 38.63c2.16-1.18 48.6-26.89 101.63-85.59c22.52 24.08 38 35.44 38.93 36.1a22 22 0 0 0 30.75-4.9z" fill="currentColor"></path></svg>
</el-button>
<template #dropdown >
<el-dropdown-menu>
<el-dropdown-item v-for="item in config.LANG" :key="item.value" :command="item">{{ item.name }}</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
2. 逻辑
//国际化
const config = reactive({
LANG:[
{name:"中文",value:'zh-cn'},
{name:'英文',value:'en'}
]
})
const configLang = ( item:{
name:string
value:string
})=>{
let $i18n = proxy?.$i18n;
$i18n!.locale = item.value;
localStorage.setItem('lang' , item.value );
}
3. main.ts导入i18n
//国际化
import i18n from './locales'
app.use(i18n)
4. 导包
npm i vue-i18n
5. src下 新建 文件夹 locales -- index.ts
import { createI18n } from 'vue-i18n'
// 自己的包
import zh from './lang/zh-cn'
import en from './lang/en'
//element语言包
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
import English from 'element-plus/dist/locale/en.mjs';
const messages = {
'zh-cn':{
el:zhCn,
...zh
},
'en':{
el:English,
...en
}
}
const i18n = createI18n({
locale: localStorage.getItem('lang') || 'zh-cn', // 初始化配置语言
messages
})
export default i18n
6. 配自己的包
结构如下:
en.ts
export default {
login: {
slogan: 'The most concise basic permission framework system.' ,
describe: 'Electron + Vue3 + element plus based front-end solutions in the background.',
signInTitle: 'Sign in',
accountLogin: 'Account sign in',
mobileLogin: 'Mobile sign in',
rememberMe: 'Remember me',
forgetPassword: 'Forget password',
signIn: 'Sign in',
signInOther: 'Sign in with',
userPlaceholder: 'user / phone / email',
userError: 'Please input a user name',
PWPlaceholder: 'Please input a password',
PWError: 'Please input a password',
admin: 'Administrator',
user: 'User',
mobilePlaceholder: 'Mobile',
mobileError: 'Please input mobile',
smsPlaceholder: 'SMS Code',
smsError: 'Please input sms code',
smsGet: 'Get SMS Code',
smsSent: 'SMS sent to mobile number',
noAccount: 'No account?',
createAccount: 'Create a new account',
wechatLoginTitle: 'QR code sign in',
wechatLoginMsg: 'Please use wechat to scan and log in | Auto scan after 3 seconds of simulation',
wechatLoginResult: 'Scanned | Please click authorize login in the device'
},
user: {
dynamic: 'Dynamic',
info: 'User Info',
settings: 'Settings',
nightmode: 'night mode',
nightmode_msg: 'Suitable for low light environment,The current night mode is beta',
language: 'language',
language_msg: 'Translation in progress,Temporarily translated the text of this view',
}
}
zh-cn.ts
export default {
login: {
slogan: '最精简的基础权限框架系统',
describe: '基于Electron + Vue3 + Element-Plus 的中后台前端解决方案。',
signInTitle: '用户登录',
accountLogin: '账号登录',
mobileLogin: '手机号登录',
rememberMe: '24小时免登录',
forgetPassword: '忘记密码',
signIn: '登录',
signInOther: '其他登录方式',
userPlaceholder: '用户名 / 手机 / 邮箱',
userError: '请输入用户名',
PWPlaceholder: '请输入密码',
PWError: '请输入密码',
admin: '管理员',
user: '用户',
mobilePlaceholder: '手机号码',
mobileError: '111请输入手机号码',
smsPlaceholder: '短信验证码',
smsError: '请输入短信验证码',
smsGet: '获取验证码',
smsSent: '已发送短信至手机号码',
noAccount: '还没有账号?',
createAccount: '创建新账号',
wechatLoginTitle: '二维码登录',
wechatLoginMsg: '请使用微信扫一扫登录 | 模拟3秒后自动扫描',
wechatLoginResult: '已扫描 | 请在设备中点击授权登录'
},
user: {
dynamic: '近期动态',
info: '个人信息',
settings: '设置',
nightmode: '黑夜模式',
nightmode_msg: '适合光线较弱的环境,当前黑暗模式为beta版本',
language: '语言',
language_msg: '翻译进行中,暂翻译了本视图的文本',
}
}
7. 使用
template:非绑定
<h4>{{ $t('login.slogan') }}</h4>
template:绑定
<el-tab-pane :label="$t('login.accountLogin')">
js:
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
proxy?.$t('login.mobileError')