首页 > 其他分享 >vue3 使用 i18n

vue3 使用 i18n

时间:2023-10-03 23:22:43浏览次数:52  
标签:vue const locale vue3 ts 使用 i18n import

安装

官网:https://vue-i18n.intlify.dev/api/general.html

pnpm add vue-i18n@9

使用

// @/locale/index.ts

import appConfig from "@/configure/app.config.ts";
import {nextTick} from 'vue'
import type {Ref} from 'vue'
import {createI18n} from 'vue-i18n'
import type {I18n, Locale} from 'vue-i18n'


const DEFAULT_LANG: string = appConfig.defaultLanguage;

const i18n = createI18n({
    legacy: false, // 禁用传统模式,使用 composition api mode
    locale: DEFAULT_LANG,
    messages: {},
    fallbackLocale: DEFAULT_LANG,
    globalInjection: true,
});

const loadLocaleMessages = async (i18n: I18n, locale: Locale) => {
    const messages = await import(`./lang/${locale}/index.ts`)
    i18n.global.setLocaleMessage(locale, messages.default)

    if (i18n.mode === "legacy") {
        i18n.global.locale = locale;
    } else {
        (i18n.global.locale as Ref<Locale>).value = locale;
    }
    document.querySelector('html').setAttribute('lang', locale)

    return nextTick();
}

loadLocaleMessages(i18n, DEFAULT_LANG)

export default i18n;
// @/locale/zh-CN/index.ts

const menu = {
    "account": "用户菜单",
}

export default {
    ...menu
}
// @/main.ts

import i18n from "@/locale"

// const app = createApp(App)...
app.use(i18n)
// @/view/demo/demo.vue

<template>
    <h1>{{ t('account') }}</h1>
<template>

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

const {t} = useI18n()
</script>

标签:vue,const,locale,vue3,ts,使用,i18n,import
From: https://www.cnblogs.com/fires/p/17741801.html

相关文章

  • 在 WebStorm 里调试 vue3 项目
    官方说明:https://blog.jetbrains.com/webstorm/2018/01/working-with-vue-js-in-webstorm/#:~:text=Wecandebugourapplication,andstartthedebugsession.打开WebStorm编辑器右上角的Configuration的Edit,在URL填入项目的地址并选择想要使用的Brower,点击调试之......
  • vue3 使用 pinia
    安装pinia官网:https://pinia.vuejs.org/pnpmaddpinia使用新建pinia实例//@/store/index.tsimport{createPinia}from"pinia";importuseUserStorefrom"@/store/user.ts";exportuseUserStore;constpinia=createPinia();exportdefault......
  • vue3 使用 vue-router
    安装vue-routerpnpmivue-router使用vue-router创建自己的router//@/route/index.tsimport{createRouter,createWebHashHistory}from'vue-router'importtype{RouteRecordRaw}from"vue-router"constroutes:RouteRecordRaw[]=[{......
  • 某某乐跑与模拟器的使用
    ​写在前面免责声明:大多操作内容是来自网络,本人仅是用自己有限的知识储备进行组装,仅供参考学习,并非倡导,如有侵权,请联系删除。绝非抄袭,如有雷同,纯属巧合。友情提示:跑步能够强壮身体、提高免疫力、预防慢性疾病、改善睡眠。跑步能增强人体的肌肉骨骼,强壮身体;跑步也能够提高人体免......
  • 使用Springboot实现点击名称跳转到详情页面
    终于解决出来啦!!!嘎嘎嘎嘎~~~只需要在td标签里面嵌套上a标签就能实现啦!这里主要看一下功能,页面直接使用的白板~html页面的具体代码如下(将超链接标签a的样式进行了美化):<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>主界面</title></head>......
  • 使用安防视频监控/视频汇聚平台EasyCVR收不到音频流怎么办
    安防视频监控平台EasyCVR是一个具有强大拓展性、灵活的视频能力和轻便部署的平台。它支持多种主流标准协议,包括国标GB28181、RTSP/Onvif、RTMP等,还可以支持厂家的私有协议和SDK接入,例如海康Ehome、海大宇等设备的SDK。该平台不仅拥有传统安防视频监控的功能,还具备接入AI智能分析的......
  • 使用 Stable Diffusion 本地版时遇到显卡驱动过旧的问题
    我本地安装了一个StableDiffusion,使用它生成图片时,遇到了如下错误消息:BC:\WINDOWS\systvenv"C:\app\stable-diffusion-webui-master\venv\Scripts\Python.exe"Python3.10.8(tags/v3.10.8:aaaf517,Oct112022,16:50:30)[MScv.193364bit(AMD64)]Commithash:Trac......
  • 以下是一个复杂的 C 语言代码示例,展示了如何使用递归函数来计算斐波那契数列: ```c #i
    以下是一个复杂的C语言代码示例,展示了如何使用递归函数来计算斐波那契数列:#include<stdio.h>//递归函数计算斐波那契数列intfibonacci(intn){if(n<=1){returnn;}returnfibonacci(n-1)+fibonacci(n-2);}intmain(){intnum;......
  • Go 语言代码示例。使用并发和通道的并行计算素数的示例代码
    Go语言代码示例。使用并发和通道的并行计算素数的示例代码:packagemainimport( "fmt")funcmain(){ lowerLimit:=2 upperLimit:=100 //创建管道,用于在协程之间传递素数 primes:=make(chanint) //创建一个协程来生成素数序列 gogeneratePrimes(primes)......
  • C++ Thread 基础使用
    C++11Thread使用基础用法头文件#include<thread>函数初始化threadthread(<function_name>);线程分离thread.detach();线程阻塞thread.join()线程取消this_thread::yield();线程休眠this_thread::sleep_for(chrono::seconds(3));代码#in......