vue2 国际化的使用,自动翻译文件,自动生成国际化文件

vue2 国际化的使用,自动翻译文件,自动生成国际化文件

  1. npm i vue-i18n@6
    在这里插入图片描述
  2. 文件代码
// zh.js  用来写全局通用的国际化
export default {
    home:"首页"
}
//en.js  用来写全局通用的国际化
export default {
    home:"home page"
}
//kor.js  用来写全局通用的国际化
export default {
    home:"첫 페이지"
}
// index.js
/*
 * @Author: xyx
 * @Date: 2024-01-30 13:47:20
 * @LastEditors: xyx
 * @LastEditTime: 2024-01-30 14:26:14
 * @FilePath: \xyjc\idc_platform_h5\lang\index.js
 * @Description: 
 */
 
import zh from './zh.js'
import en from './en.js'
import kor from './kor.js'
 
import VueI18n from 'vue-i18n'; 
import Vue from 'vue'
Vue.use(VueI18n);
 

// 自动引入  ./module 下面的所有js (对自动翻译的文件,自动生成的国际化文件 进行自动引入)
// 上图中的 ./module 下的文件 为自动生成的文件
const modulesFiles = require.context("./module", true, /\.js$/);

const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  const value = modulesFiles(modulePath);
  modules[moduleName] = value.default;
  return modules;
}, {});

let zhList = {...zh}
let enList = {...en}
let korList = {...kor}
for (const key in modules) {
    if (Object.hasOwnProperty.call(modules, key)) {
        const el = modules[key];
        console.log(el)
        zhList = Object.assign({}, zhList, el.cn)
        enList = Object.assign({}, enList,el.en)
        korList = Object.assign({}, korList,el.kor)
    }
}

const i18n = new VueI18n({
	locale: 'kor',
	messages: {
		'cn': zhList,//简体
		'en':enList,//英文
		'kor':korList,//英文
	}
})
export default i18n;
// main.js 全局引入
import i18n from '@/lang/index.js';
const app = new Vue({
	i18n,
})
// 页面使用
  // 切换为英文
  this.$i18n.locale = "en"
  // 切换为中文
  this.$i18n.locale = "zh"
  // 切换为韩文
  this.$i18n.locale = "kor"

***重点来了。。。。。。***

  1. 开发工具使用 vscode
  2. vscode搜索插件Du I18N并安装
    在这里插入图片描述
    6.
  3. 安装好之后,点击设置,自动生成配置文件 du-i18n.config.json
    在这里插入图片描述
//du-i18n.config.json

{
	"quoteKeys": [
		"this.$t",
		"$t",
		"i18n.t"
	],
	"defaultLang": "zh",
	"tempLangs": [
		"zh",
		"en",
		"kor"
	],
	"langPaths": "**/utils/i18n/locale/**",
	"transSourcePaths": "**/utils/i18n/source/**",
	"tempPaths": "**/utils/i18n/temp/**",
	"tempFileName": "i18n",
	"multiFolders": [
		"src",
		"pages"
	],
	"uncheckMissKeys": [],
	"isSingleQuote": true,
	"isOnlineTrans": true,
	 // 下图中的APP ID
	"baiduAppid": "***************", 
	 // 下图中的秘钥
	"baiduSecrectKey": "***************"
}
  1. 插件官网 https://fanyi-api.baidu.com/doc/21 有详细的配置 生成文件的位置
  2. 需要登录翻译开放平台 申请key 并且认证
    在这里插入图片描述
  3. 开始使用
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    将生成的文件放到 module 下面 后缀改为js
    在这里插入图片描述
    在这里插入图片描述
    搞定! 以上为全部流程 解放翻译时间,缩短工期

博文虽然长 但是都很简单!

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中使用Protobuf插件来自动生成Protobuf文件,可以通过以下步骤进行: 1. 首先,确保您已经安装了Vue 3项目所需的依赖,包括protobufjs和protobufjs/cli。您可以使用以下命令进行安装: ``` npm install protobufjs protobufjs/cli --save-dev ``` 2. 创建一个.proto文件,该文件定义了您的消息类型和服务等。例如,创建一个名为example.proto的文件,在其中定义您的消息类型和服务: ```protobuf syntax = "proto3"; package example; message User { string name = 1; int32 age = 2; } service UserService { rpc GetUser(UserRequest) returns (UserResponse); } message UserRequest { string userId = 1; } message UserResponse { User user = 1; } ``` 3. 在项目的根目录下创建一个脚本文件,例如generate-protobuf.js,用来运行protobufjs/cli来生成Protobuf文件。在脚本文件中添加以下内容: ```javascript const pbjs = require('protobufjs/cli/pbjs');const pbts = require('protobufjs/cli/pbts'); const fs = require('fs'); const protoFile = 'example.proto'; const outputDir = 'src/protobuf'; pbjs.main(['-t', 'static-module', '-w', 'commonjs', '-o', `${outputDir}/example.js`, protoFile], (err, output) => { if (err) { console.error(err); return; } console.log(output); pbts.main(['-o', `${outputDir}/example.d.ts`, `${outputDir}/example.js`], (err, output) => { if (err) { console.error(err); return; } console.log(output); }); }); ``` 4. 运行脚本文件以生成Protobuf文件。在命令行中执行以下命令: ``` node generate-protobuf.js ``` 这将使用protobufjs/cli来生成相应的.js和.d.ts文件,并将其输出到指定的目录中(在上述示例中为src/protobuf)。 5. 现在,您可以在Vue 3项目中使用生成的Protobuf文件。在需要使用Protobuf的组件或模块中导入生成的.js和.d.ts文件,并使用其中定义的消息类型和服务等。 ```javascript import { User, UserRequest, UserResponse } from '@/protobuf/example'; // 使用生成的Protobuf消息类型和服务等 const user = new User(); user.name = 'John'; user.age = 25; const userRequest = new UserRequest(); userRequest.userId = '123'; // ... ``` 通过上述步骤,您可以在Vue 3项目中使用Protobuf插件来自动生成Protobuf文件,并使用生成的文件来进行Protobuf相关的操作。请确保按照您的项目结构和需要进行适当的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值