Skip to content

Commit

Permalink
fix: 解决在uniapp下执行voerkai18n init由于不存在package.json文件时的错误提示问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Mar 5, 2024
1 parent afbdb60 commit b84af9b
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/zh/guide/intro/history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 更新日志<!-- {docsify-ignore-all} -->

## V2.1.7
- **[BUG]** 解决在`uniapp`下执行`voerkai18n init`由于不存在`package.json`文件时的错误提示问题。

## V2.1.6
- **[特性]** `@voerkai18n/cli``translate`执行时提供错误信息增强
- **[BUG]** 修复`@voerkai18n/vue2``activeLanguage`不会实时渲染更新的问题。
Expand Down
20 changes: 20 additions & 0 deletions docs/zh/guide/intro/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,23 @@ const messages = {

`settings.json`中,**默认语言****激活语言**可以相同,也可以不同。


## 执行`voerkai18n extract`提取到一些不需要翻译的文本,怎么办?

简单地说,就是提取要翻译的文本出现误伤的问题了,怎么办?

`voerkai18n extract`的工作机制是这样的:

1. 先使用正则表达式过滤掉注释内容。
2. 再提取正则表达式提取`t(".....")`文本内容。

总之,就是利用正则表达式来匹配提取的,如果出现提取错误时,存在几种可能的原因:

- 不同`nodejs`版本的正则表达式引擎存在差异,导致提取错误。可以尝试升级`nodejs`版本。
- 用来提取的正则表达式不够准确,某此边界问题没有覆盖,导致提取错误。请提出`issue`,我们会尽快修复。
- `extract`操作`不能处理模板字符串`的情况,比如**t(`xx${xxx}xx`)**就不能正确提取,请转换为使用插值变量的方式。





234 changes: 234 additions & 0 deletions examples/uniapp-vue3/apply.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<!--访客预约申请-填写信息--->
<template>
<div class="px-5 pt-10">
<div class="bg-white rounded-lg shadow-lg">
<van-form class="px-2 pt-2 pb-1" @submit="onSubmit">
<h3 class="text-base ml-1 my-2">接访人信息</h3>
<text>{{t('你好')}}</text>
<div>
<van-field v-model="username" name="phone" label="接访人手机号" placeholder="请输入接访人手机号" :rules="telRules"
:label-width="100">
</van-field>
<van-field v-model="password" type="password" name="name" label="接访人姓名" placeholder="请输入接访人姓名"
:rules="[{ required: true, message: '请输入接访人姓名' }]" :label-width="100">
</van-field>
</div>
<h3 class="text-base ml-1 my-2">访客信息</h3>
<div>
<van-field v-model="password" type="password" name="name" label="访客姓名" placeholder="请输入访客姓名"
:rules="[{ required: true, message: '请输入访客姓名' }]" :label-width="100">
</van-field>
<van-field v-model="password" type="password" name="name" label="来访单位" placeholder="请输入来访单位名称"
:rules="[{ required: true, message: '请输入来访单位名称' }]" :label-width="100">
</van-field>
<van-field v-model="username" name="phone" label="手机号" placeholder="请输入手机号" :rules="telRules"
:label-width="100">
</van-field>
<van-field v-model="password" type="password" name="name" label="邮箱地址" placeholder="请输入邮箱地址"
:label-width="100">
</van-field>
<!-- 是否驾车 -->
<van-field name="radio" label="是否驾车">
<template #input>
<van-radio-group v-model="radio" direction="horizontal">
<div class="flex items-center">
<van-radio name="2">否</van-radio>
<van-radio name="1">是</van-radio>
</div>
</van-radio-group>
</template>
</van-field>
<!-- 车牌号 -->
<div class="flex items-center">
<van-field readonly clickable label="车牌号" :value="plateNumber" placeholder="请选择"
@click="showPicker = true" />
<van-popup v-model="showPicker" round position="bottom">
<van-picker show-toolbar :columns="chineseList" @cancel="showPicker = false" @confirm="onCarConfirm" />
</van-popup>
<van-field v-model="carNumber" type="text" name="carnumber" placeholder="请输入车牌号">
</van-field>
</div>
<div>
<van-cell title="拜访时间" :value="date" @click="showDate = true" />
<van-calendar v-model="showDate" type="range" @confirm="onDateConfirm" />
</div>
<!-- 拜访时间 -->
<div class="flex items-center">
<van-field
readonly
clickable
:value="beginTime"
placeholder="请选择开始时间"
@click="showBeginTimePicker = true"
/>
<van-popup v-model="showBeginTimePicker" round position="bottom">
<van-datetime-picker
v-model="beginTime"
type="time"
title="选择时间"
:min-hour="10"
:max-hour="20"
@confirm="showBeginTimePicker = false"
/>
</van-popup>
<van-field
readonly
clickable
:value="endTime"
placeholder="请选择结束时间"
@click="showEndTimePicker = true"
/>
<van-popup v-model="showEndTimePicker" round position="bottom">
<van-datetime-picker
v-model="endTime"
type="time"
title="选择时间"
:min-hour="10"
:max-hour="20"
@confirm="showBeginTimePicker = false"
/>
</van-popup>
</div>
<!-- 拜访园区 -->
<div class="flex items-center">
<van-field readonly clickable label="拜访园区" :value="park" placeholder="请选择园区"
@click="showPark = true" class="flex-auto w-64"/>
<van-popup v-model="showPark" round position="bottom">
<van-picker show-toolbar :columns="parkList" @cancel="showPark = false" @confirm="onParkConfirm" />
</van-popup>
<van-field readonly clickable :value="floor" placeholder="请选择楼层"
@click="showFloor = true" class="flex-auto w-28"/>
<van-popup v-model="showFloor" round position="bottom">
<van-picker show-toolbar :columns="floorList" @cancel="showFloor = false" @confirm="onFloorConfirm" />
</van-popup>
<div class="flex-1">楼</div>
</div>
<!-- 有无随行人员 -->
<van-field name="retinue" label="有无随行人员">
<template #input>
<van-radio-group v-model="retinue" direction="horizontal">
<div class="flex items-center">
<van-radio name="2">无</van-radio>
<van-radio name="1">有</van-radio>
</div>
</van-radio-group>
</template>
</van-field>
<div v-if="retinue == '1'">
<van-button round block type="info" @click="showQrCode">邀请随行人员添加个人信息</van-button>
<van-popup v-model="qrCode">
<div class="flex items-center justify-center w-60 h-60">
二维码图片
</div>
</van-popup>
<van-button round block type="info" @click="showQrCode">点击添加随行人员添加个人信息</van-button>
</div>
</div>
<div class="m-4">
<van-button round block type="info" native-type="submit">登录</van-button>
</div>
</van-form>
</div>
</div>
</template>

<script>
export default {
name: '访客申请预约',
data() {
return {
value: '',
value1: '',
value2: '',
value3: '',
pattern: /\d{6}/,
username: '',
password: '',
checked: false,
radio: '1',
plateNumber: '',
showPicker: false,
chineseList: [
'','','','','','','','','','','',
'','','','','','','','','','','',
'','','','','','','','','',
],
telRules: [
{ required: true, message: '手机号不能为空', trigger: true },
{
validator: value => {
return /^1[3-9]\d{9}$/.test(value)
},
message: '请输入正确的手机号',
trigger: 'blur'
},
],
date: '',
showDate: false,
beginTime: '',
endTime: '',
showBeginTimePicker: false,
showEndTimePicker: false,
carNumber: '', // 车牌号
showPark: false,
park: '',
parkList: ['集团总部园区', '胶州产业园', '新大楼园区'],
showFloor: false,
floorList: ['1','2','3','4','5'],
floor: '',
retinue: '1', // 1有随行人员 2无
qrCode: false,
};
},
methods: {
// 校验函数返回 true 表示校验通过,false 表示不通过
validator(val) {
return /1\d{10}/.test(val);
},
// 异步校验函数返回 Promise
asyncValidator(val) {
return new Promise((resolve) => {
// Toast.loading('验证中...');
setTimeout(() => {
// Toast.clear();
resolve(/\d{6}/.test(val));
}, 1000);
});
},
onFailed(errorInfo) {
console.log('failed', errorInfo);
},
onSubmit() {
console.log('submit');
this.$router.push('/travel/mauthenticat')
},
onCarConfirm(value) {
this.plateNumber = value;
this.showPicker = false;
},
formatDate(date) {
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onDateConfirm(date) {
const [start, end] = date;
this.showDate = false;
this.date = `${this.formatDate(start)} - ${this.formatDate(end)}`;
},
onParkConfirm(value) {
this.park = value;
this.showPark = false;
},
onFloorConfirm(value) {
this.floor = value;
this.showFloor = false;
},
showQrCode() {
this.qrCode = true;
}
},
};
</script>
1 change: 1 addition & 0 deletions examples/uniapp-vue3/languages/idMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
39 changes: 39 additions & 0 deletions examples/uniapp-vue3/languages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 初始化入口
*
* 注意:执行compile命令会生新后成本文件,所以请不要修改本文件
* 本文件仅供在执行voerkai18n extract&compile前提供t函数占位引用
*/

import {translate,VoerkaI18nScope} from "@voerkai18n/runtime"


// 语言作用域
const scope = new VoerkaI18nScope({
id : "voerka-i18n", // 当前作用域的id,自动取当前工程的package.json的name
debug : false, // 是否在控制台输出高度信息
messages : {}, // 当前语言包
idMap : {}, // 消息id映射列表
library : false, // 开发库时设为true
formatters : {}, // 扩展自定义格式化器
languages: [
{
name: "zh",
title: "中文",
default: true,
active:true
},
{
name: "en",
title: "英文"
}
],
namespaces: {}
})
// 翻译函数
const scopedTtranslate = translate.bind(scope)

export {
scopedTtranslate as t,
scope as i18nScope
}
15 changes: 15 additions & 0 deletions examples/uniapp-vue3/languages/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"languages": [
{
"name": "zh",
"title": "中文",
"default": true,
"active": true
},
{
"name": "en",
"title": "英语"
}
],
"namespaces": {}
}
8 changes: 8 additions & 0 deletions examples/uniapp-vue3/languages/translates/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"你好": {
"en": "你好",
"$files": [
"apply.vue"
]
}
}
4 changes: 4 additions & 0 deletions examples/uniapp-vue3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name":"uniapp-demo",
"private":true
}
3 changes: 3 additions & 0 deletions packages/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ function fileIsExists(filename){
*/
function getCurrentPackageJson(folder,exclueCurrent=true){
let projectFolder = getProjectRootFolder(folder,exclueCurrent)
if(!projectFolder){
throw new Error("当前不是有效的JS工程目录,未找到package.json文件")
}
let packageJsonFile = path.join(projectFolder,"package.json")
if(fs.existsSync(packageJsonFile)){
return fs.readJSONSync(path.join(projectFolder,"package.json"))
Expand Down

0 comments on commit b84af9b

Please sign in to comment.