@npm-questionpro/wick-ui-i18n 0.14.1 → 2.0.0-next.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -61
- package/index.d.ts +1 -0
- package/index.js +24 -24
- package/package.json +23 -6
- package/src/debug.js +25 -26
- package/src/processor.js +73 -70
- package/src/transform.js +104 -95
- package/src/transformJSXTextWithEntities.js +25 -25
- package/src/transformTemplateLiteral.js +22 -22
- package/src/transformWtCalls.js +41 -41
- package/wickuii18n.test.js +0 -873
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* <WuButton><><WuTranslate __i18nKey="hello" /> {name} <WuTranslate __i18nKey="how are you" /></></WuButton>
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {
|
|
13
|
+
import {getComponentTree} from './debug.js'
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Given a quasi's cooked string, extract leading whitespace, trimmed text,
|
|
@@ -21,10 +21,10 @@ import { getComponentTree } from "./debug.js";
|
|
|
21
21
|
* @returns {{ leading: string, text: string, trailing: string }}
|
|
22
22
|
*/
|
|
23
23
|
function splitQuasi(raw) {
|
|
24
|
-
const leading = raw.match(/^\s*/)[0]
|
|
25
|
-
const trailing = raw.match(/\s*$/)[0]
|
|
26
|
-
const text = raw.slice(leading.length, raw.length - trailing.length)
|
|
27
|
-
return {
|
|
24
|
+
const leading = raw.match(/^\s*/)[0]
|
|
25
|
+
const trailing = raw.match(/\s*$/)[0]
|
|
26
|
+
const text = raw.slice(leading.length, raw.length - trailing.length)
|
|
27
|
+
return {leading, text, trailing}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -52,39 +52,39 @@ export function transformTemplateLiteralExpression(
|
|
|
52
52
|
processor,
|
|
53
53
|
id,
|
|
54
54
|
) {
|
|
55
|
-
if (!processor.shouldTranslate(path)) return false
|
|
55
|
+
if (!processor.shouldTranslate(path)) return false
|
|
56
56
|
|
|
57
|
-
const container = path.node
|
|
58
|
-
const expr = container.expression
|
|
59
|
-
const {
|
|
60
|
-
const componentTree = getComponentTree(path)
|
|
57
|
+
const container = path.node
|
|
58
|
+
const expr = container.expression // TemplateLiteral
|
|
59
|
+
const {quasis, expressions} = expr
|
|
60
|
+
const componentTree = getComponentTree(path)
|
|
61
61
|
|
|
62
|
-
const parts = []
|
|
63
|
-
let hasTranslatable = false
|
|
62
|
+
const parts = []
|
|
63
|
+
let hasTranslatable = false
|
|
64
64
|
|
|
65
65
|
for (let i = 0; i < quasis.length; i++) {
|
|
66
|
-
const cooked = quasis[i].value.cooked ?? quasis[i].value.raw
|
|
67
|
-
const {
|
|
66
|
+
const cooked = quasis[i].value.cooked ?? quasis[i].value.raw
|
|
67
|
+
const {leading, text, trailing} = splitQuasi(cooked)
|
|
68
68
|
|
|
69
69
|
if (text) {
|
|
70
|
-
processor.record(text, text, id, componentTree)
|
|
70
|
+
processor.record(text, text, id, componentTree)
|
|
71
71
|
parts.push(
|
|
72
72
|
`${leading}<WuTranslate __i18nKey=${JSON.stringify(text)}></WuTranslate>${trailing}`,
|
|
73
|
-
)
|
|
74
|
-
hasTranslatable = true
|
|
73
|
+
)
|
|
74
|
+
hasTranslatable = true
|
|
75
75
|
}
|
|
76
76
|
// whitespace-only or empty quasi — emit nothing (no key, no node)
|
|
77
77
|
|
|
78
78
|
// Interleave: expression follows its preceding quasi (skip after last quasi)
|
|
79
79
|
if (i < expressions.length) {
|
|
80
|
-
const exprNode = expressions[i]
|
|
81
|
-
parts.push(`{${code.slice(exprNode.start, exprNode.end)}}`)
|
|
80
|
+
const exprNode = expressions[i]
|
|
81
|
+
parts.push(`{${code.slice(exprNode.start, exprNode.end)}}`)
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
if (!hasTranslatable) return false
|
|
85
|
+
if (!hasTranslatable) return false
|
|
86
86
|
|
|
87
87
|
// Replace the entire {`...`} container with a React fragment
|
|
88
|
-
ms.overwrite(container.start, container.end, `<>${parts.join(
|
|
89
|
-
return true
|
|
88
|
+
ms.overwrite(container.start, container.end, `<>${parts.join('')}</>`)
|
|
89
|
+
return true
|
|
90
90
|
}
|
package/src/transformWtCalls.js
CHANGED
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
* @returns {{ leading: string, text: string, trailing: string }}
|
|
27
27
|
*/
|
|
28
28
|
function splitQuasi(raw) {
|
|
29
|
-
const leading = raw.match(/^\s*/)[0]
|
|
30
|
-
const trailing = raw.match(/\s*$/)[0]
|
|
29
|
+
const leading = raw.match(/^\s*/)[0]
|
|
30
|
+
const trailing = raw.match(/\s*$/)[0]
|
|
31
31
|
const text = raw
|
|
32
32
|
.slice(leading.length, raw.length - trailing.length)
|
|
33
|
-
.replace(/\n/g,
|
|
34
|
-
.replace(/\s{2,}/g,
|
|
35
|
-
return {leading, text, trailing}
|
|
33
|
+
.replace(/\n/g, ' ')
|
|
34
|
+
.replace(/\s{2,}/g, ' ')
|
|
35
|
+
return {leading, text, trailing}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
@@ -45,31 +45,31 @@ function splitQuasi(raw) {
|
|
|
45
45
|
* @returns {boolean}
|
|
46
46
|
*/
|
|
47
47
|
export function recordWtCall(path, processor, id) {
|
|
48
|
-
const {
|
|
48
|
+
const {callee, arguments: args} = path.node
|
|
49
49
|
|
|
50
50
|
// Only handle bare `wt(...)` identifiers — not obj.wt(...) etc.
|
|
51
|
-
if (callee.type !==
|
|
52
|
-
if (args.length !== 1) return false
|
|
51
|
+
if (callee.type !== 'Identifier' || callee.name !== 'wt') return false
|
|
52
|
+
if (args.length !== 1) return false
|
|
53
53
|
|
|
54
|
-
const arg = args[0]
|
|
55
|
-
let text = null
|
|
54
|
+
const arg = args[0]
|
|
55
|
+
let text = null
|
|
56
56
|
|
|
57
|
-
if (arg.type ===
|
|
58
|
-
text = arg.value
|
|
59
|
-
} else if (arg.type ===
|
|
60
|
-
text = arg.quasis[0].value.cooked ?? arg.quasis[0].value.raw
|
|
57
|
+
if (arg.type === 'StringLiteral') {
|
|
58
|
+
text = arg.value
|
|
59
|
+
} else if (arg.type === 'TemplateLiteral' && arg.expressions.length === 0) {
|
|
60
|
+
text = arg.quasis[0].value.cooked ?? arg.quasis[0].value.raw
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
if (text === null) return false
|
|
63
|
+
if (text === null) return false
|
|
64
64
|
|
|
65
65
|
const cleanText = text
|
|
66
66
|
.trim()
|
|
67
|
-
.replace(/\n/g,
|
|
68
|
-
.replace(/\s{2,}/g,
|
|
69
|
-
if (!cleanText) return false
|
|
67
|
+
.replace(/\n/g, ' ')
|
|
68
|
+
.replace(/\s{2,}/g, ' ')
|
|
69
|
+
if (!cleanText) return false
|
|
70
70
|
|
|
71
|
-
processor.record(cleanText, cleanText, id,
|
|
72
|
-
return true
|
|
71
|
+
processor.record(cleanText, cleanText, id, '(wt)')
|
|
72
|
+
return true
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -95,42 +95,42 @@ export function recordWtCall(path, processor, id) {
|
|
|
95
95
|
* @returns {boolean} `true` when the call was rewritten.
|
|
96
96
|
*/
|
|
97
97
|
export function transformWtTemplateLiteral(path, code, ms, processor, id) {
|
|
98
|
-
const {callee, arguments: args} = path.node
|
|
98
|
+
const {callee, arguments: args} = path.node
|
|
99
99
|
|
|
100
|
-
if (callee.type !==
|
|
101
|
-
if (args.length !== 1) return false
|
|
100
|
+
if (callee.type !== 'Identifier' || callee.name !== 'wt') return false
|
|
101
|
+
if (args.length !== 1) return false
|
|
102
102
|
|
|
103
|
-
const arg = args[0]
|
|
104
|
-
if (arg.type !==
|
|
105
|
-
return false
|
|
103
|
+
const arg = args[0]
|
|
104
|
+
if (arg.type !== 'TemplateLiteral' || arg.expressions.length === 0)
|
|
105
|
+
return false
|
|
106
106
|
|
|
107
|
-
const {quasis, expressions} = arg
|
|
108
|
-
const parts = [
|
|
109
|
-
let hasTranslatable = false
|
|
107
|
+
const {quasis, expressions} = arg
|
|
108
|
+
const parts = ['`']
|
|
109
|
+
let hasTranslatable = false
|
|
110
110
|
|
|
111
111
|
for (let i = 0; i < quasis.length; i++) {
|
|
112
|
-
const cooked = quasis[i].value.cooked ?? quasis[i].value.raw
|
|
113
|
-
const {leading, text, trailing} = splitQuasi(cooked)
|
|
112
|
+
const cooked = quasis[i].value.cooked ?? quasis[i].value.raw
|
|
113
|
+
const {leading, text, trailing} = splitQuasi(cooked)
|
|
114
114
|
|
|
115
115
|
if (text) {
|
|
116
|
-
processor.record(text, text, id,
|
|
117
|
-
parts.push(`${leading}\${wt(${JSON.stringify(text)})}${trailing}`)
|
|
118
|
-
hasTranslatable = true
|
|
116
|
+
processor.record(text, text, id, '(wt)')
|
|
117
|
+
parts.push(`${leading}\${wt(${JSON.stringify(text)})}${trailing}`)
|
|
118
|
+
hasTranslatable = true
|
|
119
119
|
} else {
|
|
120
120
|
// empty or whitespace-only quasi — preserve as literal template text
|
|
121
|
-
parts.push(cooked)
|
|
121
|
+
parts.push(cooked)
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
if (i < expressions.length) {
|
|
125
|
-
const exprSrc = code.slice(expressions[i].start, expressions[i].end)
|
|
126
|
-
parts.push(`\${${exprSrc}}`)
|
|
125
|
+
const exprSrc = code.slice(expressions[i].start, expressions[i].end)
|
|
126
|
+
parts.push(`\${${exprSrc}}`)
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
parts.push(
|
|
130
|
+
parts.push('`')
|
|
131
131
|
|
|
132
|
-
if (!hasTranslatable) return false
|
|
132
|
+
if (!hasTranslatable) return false
|
|
133
133
|
|
|
134
|
-
ms.overwrite(path.node.start, path.node.end, parts.join(
|
|
135
|
-
return true
|
|
134
|
+
ms.overwrite(path.node.start, path.node.end, parts.join(''))
|
|
135
|
+
return true
|
|
136
136
|
}
|