-
Notifications
You must be signed in to change notification settings - Fork 151
/
translations-new.js
127 lines (111 loc) · 3.45 KB
/
translations-new.js
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
// @noflow
/* eslint-disable no-param-reassign */
const COMPONENTS = [
"Popover",
"Tooltip",
"Modal",
"Drawer",
"Wizard",
"Alert",
"MobileDialogPrimitive",
"MobileDialog",
"NavigationBar",
"Breadcrumbs",
"Card",
];
const getPropName = name => {
if (name === "Drawer") return "labelHide";
if (name === "NavigationBar") return "openTitle";
if (name === "Breadcrumbs") return "goBackTitle";
return "labelClose";
};
const getTranslationKey = name => {
if (name === "Drawer") return "orbit.drawer_hide";
if (name === "NavigationBar") return "orbit.navigationbar_open_menu";
if (name === "Breadcrumbs") return "orbit.breadcrumbs_back";
return "orbit.button_close";
};
const getDefaultMessage = component => {
if (component === "Drawer") return "Hide";
if (component === "NavigationBar") return "Open menu";
if (component === "Breadcrumbs") return "Back";
return "Close";
};
function translations(fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
const findComponent = name => root.find(j.JSXOpeningElement, { name: { name } });
const ReactIntl = root.find(j.ImportDeclaration, {
source: { value: "react-intl" },
});
if (ReactIntl.size() === 0) {
root
.find(j.Program)
.get("body", 0)
.insertAfter(
j.importDeclaration(
[j.importSpecifier(j.identifier("FormattedMessage"))],
j.literal("react-intl"),
),
);
}
COMPONENTS.forEach(component => {
findComponent(component).forEach(path => {
const propName = getPropName(component);
const translationKey = getTranslationKey(component);
path.node.attributes = [
...path.node.attributes,
j.jsxAttribute(
j.jsxIdentifier(propName),
j.jsxExpressionContainer(
j.jsxElement(
j.jsxOpeningElement(
j.jsxIdentifier("FormattedMessage"),
[
j.jsxAttribute(j.jsxIdentifier("id"), j.stringLiteral(translationKey)),
j.jsxAttribute(
j.jsxIdentifier("defaultMessage"),
j.stringLiteral(getDefaultMessage(component)),
),
],
true,
),
),
),
),
];
});
});
root.find(j.JSXOpeningElement, { name: { name: "Wizard" } }).forEach(path => {
const total = path.node.attributes.find(attr => attr.name.name === "completedSteps");
const active = path.node.attributes.find(attr => attr.name.name === "activeStep");
path.node.attributes = [
...path.node.attributes,
j.jsxAttribute(
j.jsxIdentifier("labelProgress"),
j.jsxExpressionContainer(
j.jsxElement(
j.jsxOpeningElement(
j.jsxIdentifier("FormattedMessage"),
[
j.jsxAttribute(j.jsxIdentifier("id"), j.stringLiteral("orbit.wizard_progress")),
j.jsxAttribute(
j.jsxIdentifier("values"),
j.jsxExpressionContainer(
j.objectExpression([
j.objectProperty(j.identifier("number"), active.value.expression),
j.objectProperty(j.identifier("total"), total.value.expression),
]),
),
),
],
true,
),
),
),
),
];
});
return root.toSource();
}
module.exports = translations;