首页 > 其他分享 >vue3 + i18n

vue3 + i18n

时间:2023-06-19 17:35:33浏览次数:42  
标签:vue cn app js vue3 i18n import

vue3 + i18n

安装:

npm install vue-i18n

yarn add vue-i18n

main.js

如果在一个模块系统中使用它,你必须通过 Vue.use() 明确地安装 vue-i18n:

import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import router from './router/index'
import i18n from './lang/i18n'

const app = createApp(App)
app.use(ElementPlus)
app.use(router)
app.use(i18n)
app.mount('#app')

在src目录下创建lang文件夹,下包含ch.js(中文包) en.js(英文包) index.js i18n.js(配置i18n)

cn.js

export default{
message: {
username: '用户名',
password: '密码',
login: '登录',
captcha: '验证码',
forgetPassword: '忘记密码?',
loginTip: '当前登录结果随机。验证码随便填',
editpassword: '修改密码',
logout: '退出登录',
errMsg: {
inputRequired: '请输入{cont}',
selectRequired: '请选择{cont}'
}
},
}

en.js

export default{
message: {
username: 'User Name',
password: 'Password',
login: 'Login',
captcha: 'Captcha',
forgetPassword: 'Forget Password?',
loginTip: 'The login result is random. Just fill in the captcha',
editpassword: 'Edit Password',
logout: 'Logout',
errMsg: {
inputRequired: 'Please Input {cont}',
selectRequired: 'Please Select {cont}'
}
},
}

index.js

import cn from './cn'

import en from './en'

export default { cn, en };

i18n.js

import { createI18n } from 'vue-i18n' //引入vue-i18n组件
import messages from './index'
const i18n = createI18n({
fallbackLocale: 'cn',//找不到字段会以此语言展示
globalInjection:true,
legacy: false, // you must specify 'legacy: false' option
locale: language.split("-")[0] || "cn",//默认显示的语言
messages,//本地化的语言环境信息。
});

export default i18n

使用

<div>{{ $t(`message.login`) }}</div>

在setup中使用

<script setup lang="ts">
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

console.log(t('login.useName'))
</script>

切换语言

<template>
    <div class="menu">
        <div  @click="changeLang('en')">English</div>
        <div  @click="changeLang('zh')">中文</div>
    </div>
</template>
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
const { locale } = useI18n()

const changeLang = (lang: string) => {
  locale.value = lang
  localStorage.setItem('lang', lang)
}
</script>
 

标签:vue,cn,app,js,vue3,i18n,import
From: https://www.cnblogs.com/Dasate/p/17491678.html

相关文章

  • 谈谈Vue3中的ref和reactive
    谈谈Vue3中的ref和reactiveref和reactive是什么?ref和reactive是Vue3中用来实现数据响应式的API一般情况下,ref定义基本数据类型,reactive定义引用数据类型(我喜欢用它来定义对象,不用它定义数组,原因后面讲)我理解的ref本质上是reactive的再封装二、先聊reactivereactive定义引用数据......
  • vite+vue3项目中使用 lottie 动画,如何在 template 中直接使用本地 json 文件路径
    安装lottie-webyarnaddlottie-web封装 lottie组件<template><divref="animation":style="{width,height}"></div></template><script>import{defineComponent,ref,onMounted}from'vue'......
  • Vue3 - 实现文本复制粘贴功能
    1.安装库并导入npmivue-clipboard3--save2.在需要的前端文件中导入importclipboard3from'vue-clipboard3'html结构如下<template><divclass="hello"><inputtype="text"v-model="text"><button@cli......
  • 一文看完Vue3的渲染过程
    Vue3官网中有下面这样一张图,基本展现出了Vue3的渲染原理:本文会从源码角度来草率的看一下Vue3的运行全流程,旨在加深对上图的理解,从下面这个很简单的使用示例开始:import{createApp,ref}from"vue";createApp({template:`<divclass="card"><butt......
  • vue3+vite+web3.js报错ReferenceError: process is not defined
    在vite最新版本中使用web3会报错只需要在vite.config.ts添加如下代码即可解决报错import{fileURLToPath,URL}from'node:url'import{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'//引入import{resolve}from'path'export......
  • vue3Props
    一、Props声明一个组件需要显式声明它所接受的props,这样Vue才能知道外部传入的哪些是props,哪些是透传 attribute在使用SFC时,props可以使用defineProps()宏来声明:如子组件中(1)constprops=defineProps(['foo'])(2)constprops=defineProps({ title:String, age......
  • 基于uniapp+vite4+vue3搭建跨端项目|uni-app+uview-plus模板
    最近得空学习了下uniapp结合vue3搭建跨端项目。之前也有使用uniapp开发过几款聊天/仿抖音/后台管理等项目,但都是基于vue2开发。随着vite.js破局出圈,越来越多的项目偏向于vue3开发,就想着uniapp搭配vite4.x构建项目效果会如何?经过一番尝试果然真香~版本信息HBuilderX:3.8.4Vite......
  • vue3:vue+nginx+php进行服务端部署的配置(nginx/1.18.0 / [email protected])
    一,开发环境中的配置:1,前端:vue的vue.config.jsconst{defineConfig}=require('@vue/cli-service')module.exports=defineConfig({transpileDependencies:true,publicPath:process.env.NODE_ENV==="production"?"./":"/&qu......
  • Vue3 设置全局变量
    方式一:main.js设置全局变量importapifrom'@/api'app.config.globalProperties.$api=api使用全局变量,使用getCurrentInstance方法。//ctx.$api就是全局设置的变量const{proxy:{$api},}=getCurrentInstance();//ctx.$api就是全局设置的变量con......
  • 3d翻转动画 vue3 ts
    <template><section><divclass="flip-container"><divclass="cards":class="{flipped:isFlipped}"><divclass="front"></div><......