-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
translate-vue.ts
179 lines (171 loc) · 6.44 KB
/
translate-vue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { passthrough } from '@yeoman/transform';
import { Minimatch } from 'minimatch';
import CoreGenerator from '../../base-core/index.js';
type GetWebappTranslation = (s: string, data?: Record<string, any>) => string;
function replaceTranslationAttributes({ content, getWebappTranslation }: { content: string; getWebappTranslation: GetWebappTranslation }) {
return content.replaceAll(/v-bind:(?<tag>(?:placeholder|title|label))="(?<translate>t\$\([^"]+\))"/g, (_complete, ...args) => {
const groups: Record<string, string> = args.pop();
if (groups.translate.includes('+')) {
return '';
}
const translated = replaceTranslations({ content: groups.translate, type: 'vue', getWebappTranslation });
return `${groups.tag}="${translated}"`;
});
}
export function removeDeclarations({ content }: { content: string }) {
return content
.replaceAll(/\nimport {\s*useI18n\s*} from 'vue-i18n';/g, '')
.replaceAll(/\n\s*t\$,/g, '')
.replaceAll(/\n\s*t\$:\s*useI18n\(\).t,/g, '');
}
export function replaceTranslationTags(
this: CoreGenerator | void,
{
body,
enableTranslation,
getWebappTranslation,
}: {
body: string;
enableTranslation: boolean;
getWebappTranslation: GetWebappTranslation;
},
) {
body = body.replaceAll(
/(?<tagWithAttributes>(?<beforeTranslateTag><(?<tagName>a|b-(?:badge|link|button|alert)|button|div|h[1-9]|input|label|p|router-link|small|span|t[hd])(?:[^>]*)) v-(?:text|html)="(?<translate>t\$\([^)]*\))"(?<afterTranslateTag>(?:(?!\/?>)[^/>])*>))(?<tagContent>(?:(?!<\/\k<tagName>(?:>|\s|\n)).|\n)*)/g,
(_complete, ...args) => {
const groups: Record<string, string> = args.pop();
if (new RegExp(`<${groups.tagName}[\\s>]`, 'g').test(groups.tagContent)) {
throw new Error(`Nested tags identical to the translated tag are not supported: ${groups.tagWithAttributes}${groups.tagContent}`);
}
if (enableTranslation) {
return groups.tagWithAttributes;
}
if (groups.translate) {
if (groups.translate.includes('+')) {
this?.log.info(`Ignoring dynamic translation ${groups.translate}`);
} else {
const translated = replaceTranslations({
type: 'vue',
content: groups.translate,
getWebappTranslation,
});
if (translated !== groups.translate) {
return `${groups.beforeTranslateTag}${groups.afterTranslateTag}${translated}`;
}
throw new Error(`${translated}, ${groups.translate}`);
}
}
return `${groups.beforeTranslateTag}${groups.afterTranslateTag}${groups.tagContent}`;
},
);
if (enableTranslation) {
return body;
}
return replaceTranslationAttributes({ content: body, getWebappTranslation });
}
export function replaceTranslations({
content,
type,
getWebappTranslation,
}: {
content: string;
type: 'vue' | 'ts';
getWebappTranslation: GetWebappTranslation;
}) {
const regex =
type === 'ts'
? /(?:this.)?(t\$|i18n.t)\((?<key>[^),]*)(?:,\s*{(?<data>[^)]*)})?\)\.toString\(\)/g
: /t\$\((?<key>[^),]*)(?:,\s*{(?<data>[^)]*)})?\)/g;
return content.replaceAll(regex, (_complete, ...args) => {
const groups: Record<string, string> = args.pop();
const key = groups.key.substring(1, groups.key.length - 1).replaceAll("\\'", "'");
let data;
if (groups.data) {
const interpolateMatches = groups.data.matchAll(/(?<field>[^{\s:,}]+)(?:\s*:\s*(?<value>[^,}]+))?/g);
data = {};
let templateLiteral = false;
for (const interpolateMatch of interpolateMatches) {
let { field, value }: { field?: string; value?: string | number } = interpolateMatch.groups || {};
if (/^'.*'$/.test(field) || /^".*"$/.test(field)) {
// unwrap field
field = field.substring(1, field.length - 1);
}
if (value === undefined) {
value = field;
}
value = value.trim();
if (/^\d+$/.test(value)) {
// convert integer
value = parseInt(value, 10);
} else if (/^'.*'$/.test(value) || /^".*"$/.test(value)) {
// extract string value
value = value.substring(1, value.length - 1);
} else {
// wrap expression
if (type === 'vue') {
value = `{{ ${value} }}`;
} else {
value = `\${${value}}`;
}
templateLiteral = true;
}
data[field] = value;
}
if (templateLiteral && type === 'ts') {
return `\`${getWebappTranslation(key, data)}\``;
}
}
if (type === 'vue') {
return getWebappTranslation(key, data);
}
return `'${getWebappTranslation(key, data)}'`;
});
}
const minimatch = new Minimatch('**/*.{vue,ts}');
export const isTranslatedVueFile = file => minimatch.match(file.path);
function translateVueFilesTransform(
this: CoreGenerator | void,
{
enableTranslation,
getWebappTranslation,
}: {
enableTranslation: boolean;
getWebappTranslation: GetWebappTranslation;
},
) {
return passthrough(file => {
let newContent;
if (file.path.endsWith('.vue')) {
newContent = replaceTranslationTags.call(this, { body: file.contents.toString(), enableTranslation, getWebappTranslation });
} else if (!enableTranslation && file.path.endsWith('.ts')) {
newContent = replaceTranslations({ type: 'ts', content: file.contents.toString(), getWebappTranslation });
newContent = removeDeclarations({ content: newContent });
}
if (newContent) {
if (!enableTranslation && newContent.includes('t$')) {
throw new Error(`Could not translate ${file.path}:
${newContent}`);
}
file.contents = Buffer.from(newContent);
}
});
}
export default translateVueFilesTransform;