vue3.0 + vue-i18n:使用方法和自动引入多个语言文件

server/2024/10/21 5:31:08/

1、下载依赖

npm install i18n>vue-i18n --save

2、看文件结构:
在这里插入图片描述
en、zh 中 index.ts 作用就是自动引入 文本文件,因为 vue3.0不支持 require,所以我们用 import 替换,方法如下:

// 批量引入英文文件
const modulesFilesEn:any = import.meta.glob("./*.js",{ eager: true })
const modulesEn:any = {};
for (const path in modulesFilesEn) {const moduleName: any = path.replace(/(.*\/)*([^.]+).*/gi, "$2")modulesEn[moduleName] = modulesFilesEn[path].default
}
export default modulesEn
// 批量引入中文
const modulesFilesZhCN:any = import.meta.glob("./*.js",{ eager: true })
const modulesZhCN:any = {};
for (const path in modulesFilesZhCN) {const moduleName = path.replace(/(.*\/)*([^.]+).*/gi, "$2")modulesZhCN[moduleName] = modulesFilesZhCN[path].default;
}
export default modulesZhCN

最终在 index.ts 中

import { createI18n } from 'i18n>vue-i18n';
import zh from './zh'
import en from './en'
import settings from '../settings'
const i18n = createI18n({locale: 'zh_CN', // 本地持久化时的 localStorage key  英文-en// fallbackLocale: 'zh_CN', //没有英文的时候默认中文语言messages: {zh_CN: zh,	//引入语言文件en: en}
})
export default i18n

main.ts中

import i18n from './i18n/index'
app.use(i18n)

文件中使用

<span class="title">{{ $t('component.title') }}</span>

或者

<span class="title">{{ t('component.title') }}</span>import { useI18n } from 'i18n>vue-i18n';
const { t } = useI18n();

require用法

const en = {}
// 当前目录下所有 js 文件
const ctx = require.context('./', true, /.js$/)
ctx.keys().forEach(file => (en[file.replace(/^\.\/(\w+)\.js$/, '$1')] = ctx(file).default))
export default en

import 又或者可以这样写

const en = {language: {   //示例zh: 'Chinese',en: 'English',},
}
// 当前目录下所有 js 文件
const ctx:any = import.meta.globEager('./*.js')
for (const file in ctx) {en[file.replace(/^\.\/(\w+)\.js$/, '$1')] = ctx[file].default
}
export default en

http://www.ppmy.cn/server/133540.html

相关文章

maven本地打jar包依赖

本地工程的pom文件中引入了mysql依赖&#xff0c;但是在maven库中没有拉下来&#xff0c;可以到mysql官网下载jar包&#xff0c;使用maven手动打包到本地仓库中&#xff1a; 官网地址&#xff1a;MySQL :: Download MySQL Connector/J (Archived Versions) 在jar包所在位置的路…

企业网站设计之网站结构设计

创意主题网站的结构设计更是需要更为精心的策划和执行&#xff0c;以确保吸引并保持用户的注意力。以下是创意主题网站结构设计的关键要素&#xff1a; 1. 目标明确的导航栏 导航栏是用户进入网站并浏览内容的入口。在创意主题网站中&#xff0c;导航栏的设计要突显网站的主…

如何设置JMeter界面的永久汉化?

1、找到jMeter安装目录下的bin目录 2、打开jmeter.properties文件&#xff0c;把第37行修改为"languagezh_CN"&#xff0c;保存&#xff0c;关闭 3、重启JMeter即可

深度学习:开启人工智能的新纪元

深度学习&#xff1a;开启人工智能的新纪元 深度学习是机器学习的一个子领域&#xff0c;它基于人工神经网络的学习算法&#xff0c;特别是那些具有多个非线性变换的层&#xff08;即“深度”&#xff09;。这些算法模仿人脑处理信息的方式&#xff0c;通过学习数据的多层次表…

ConcurrentHashMap实现缓存功能

public class CacheUtil<K, V>{private final ConcurrentHashMap<K, CacheEntry<V>> cache new ConcurrentHashMap<>();private final long ttl; // 缓存过期时间&#xff0c;单位&#xff1a;毫秒// 缓存条目类private static class CacheEntry<V…

028 elasticsearch索引管理-ElasticsearchRestTemplate

文章目录 pom.xmlapplication.ymlCubemallSearchApplication.javaRestClientTest.java使用ElasticsearchRestTemplate对象Blog.javaRestTemplateTest.java pom.xml <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-s…

OpenCV高级图形用户界面(14)交互式地选择一个或多个感兴趣区域函数selectROIs()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 允许用户在给定的图像上选择多个 ROI。 该函数创建一个窗口&#xff0c;并允许用户使用鼠标来选择多个 ROI。控制方式&#xff1a;使用空格键或…

DFS算法经典题目: Leetcode 51.N皇后

DFS算法经典题目&#xff1a; Leetcode 51.N皇后 题目详情如下 这道题如果使用暴力解法的话&#xff0c;需要对N个皇后放在每个地方都进行枚举并判断是否可行&#xff0c;时间复杂度非常之高&#xff0c;肯定是过不了的&#xff0c;所以需要使用其他解法。 根据题目可以知道每…