当前位置: 首页 > news >正文

【chrome扩展开发】vue-i18n使用问题及解决方案

记录chrome扩展开发时调用vue-i18n的一些问题和解决方法

环境

  • vue: ^3.3.4
  • vue-i18n: ^9.2.2
  • vite: ^4.4.8

错误1

Uncaught (in promise) EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost:* http://127.0.0.1:*".

原因

  • https://vue-i18n.intlify.dev/guide/advanced/optimization.html#improve-performance-and-reduce-bundle-size-with-runtime-build-only
    在这里插入图片描述

解决方案1:
方案来源:

  • https://github.com/intlify/bundle-tools/issues/23#issuecomment-893353418
  • https://github.com/intlify/bundle-tools/tree/main/packages/vue-i18n-loader#why-do-we-need-to-require-the-configuration

这个方法虽然可以解决报错问题,但是t函数还是无法正常使用,如果只是简单的语言调用可以使用tm函数

<script setup>
import {useI18n} from 'vue-i18n'
const {tm} = useI18n()
</script><template><div>{{ tm('test') }}</div>
</template>
// 在createI18n时,增加一个导出方法,来简单解决`t`函数不能正常用的问题
export function ts(key: string, arg: object) {const {tm,locale} = useI18n()let _text = tm(key);let reg;for(let LKey in arg){reg = new RegExp('\{\\s*?'+LKey+'\\s*?\}','g');// @ts-ignore_text = _text.replace(reg, arg[LKey])}return _text;
}// 使用:ts('test', {key1: 'key1Val', key2: 'key2Val'})

解决方案2:
方案来源:

  • https://www.cnblogs.com/guangzan/p/14999292.html
  • https://vue-i18n.intlify.dev/guide/advanced/optimization.html#vite-plugin-vue-i18n

使用 vite 插件 vite-plugin-vue-i18n 处理这个问题

npm i --save-dev @intlify/vite-plugin-vue-i18n# 包文档: https://www.npmjs.com/package/@intlify/vite-plugin-vue-i18n (停更)
# 包作者提醒:
# This plugin support until Vite 3. If you would like to use on Vite 4, please use @intlify/unplugin-vue-i18nnpm i --save-dev @intlify/unplugin-vue-i18n
# 包文档: https://www.npmjs.com/package/@intlify/unplugin-vue-i18n

vue-i18n官方文档示例:
vue-i18n官方文档示例

错误2

You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.

解决方案:

// vite.config.ts
resolve: {alias: {// ...'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js'},
},

其他相关文献:

  • 【vue-i18n踩坑】The message format compilation is not supported in this build.
  • 【VUE】vue-i18n: Uncaught SyntaxError: Not available in legacy mode
  • vue-i18n-next bundle includes new Function()
  • Interpolations in production mode does not work
  • vue3 with vue-i18n-next only works with unsafe-eval CSP header
http://www.lryc.cn/news/114056.html

相关文章:

  • 【Vue3】localStorage读取数组并赋值的问题
  • 华为harmonyos4.0鸿蒙4.0安装谷歌服务框架Play商店,解决从服务器检索信息时出错
  • pcl 滤波
  • 前端js--旋转幻灯片
  • 解决mvn clean install遇到testng单元测试失败时打包也失败的问题
  • RISC-V基础之函数调用(二)栈与寄存器(包含实例)
  • 解析器模式(C++)
  • 电子元器件选型与实战应用—02 电容选型第1篇(8000字)
  • 试图将更改推送到 GitHub,但是远程仓库已经包含了您本地没有的工作(可能是其他人提交的修改)
  • Lamport向量时钟算法的C++实现:在分布式系统中生成事件的部分排序并检测因果关系违规
  • 多个excel的sheet合并到一个excel下
  • 【Fegin技术专题】「原生态」打开Fegin之RPC技术的开端,你会使用原生态的Fegin吗?(中)
  • leetcode--每日一题--822--344(使用异或来进行数据交换)
  • OpenStreetMap数据转3D场景【Python + PostgreSQL】
  • 动力节点|MyBatis入门实战到深入源码
  • 分布式规则引擎框架的设计
  • C#开发FFMPEG例子(API方式) FFmpeg推送udp组播流
  • nvm下载node导致npm报错无法使用
  • LeetCode 热题 100JavaScript--2. 两数相加
  • zookeeper总结
  • 【程序环境与预处理玩转指南】
  • 搭建简易syslog日志中转服务器
  • MongoDB文档-进阶使用-spring-boot整合使用MongoDB---MongoRepository完成增删改查
  • 什么是线程局部变量?
  • Jmeter响应中的乱码问题
  • MongoDB文档-进阶使用-MongoDB索引-createindex()与dropindex()-在MongoDB中使用正则表达式来查找
  • CentOS下ZLMediaKit的可视化管理网站MediaServerUI使用
  • 回归预测 | MATLAB实现POA-CNN-BiGRU鹈鹕算法优化卷积双向门控循环单元多输入单输出回归预测
  • Rust 原生支持龙架构指令集
  • 为生成式AI提速,亚马逊云科技Amazon EC2 P5满足GPU需求