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
180
181
182
183
184
185
186
187
|
/* eslint-disable */
/**
* This script will extract the internationalization messages from all components
and package them in the translation json files in the translations file.
*/
const fs = require('fs');
const nodeGlob = require('glob');
const transform = require('babel-core').transform;
const animateProgress = require('./helpers/progress');
const addCheckmark = require('./helpers/checkmark');
const pkg = require('../../package.json');
const presets = pkg.babel.presets;
const plugins = pkg.babel.plugins || [];
const i18n = require('../../app/i18n');
const DEFAULT_LOCALE = i18n.DEFAULT_LOCALE;
require('shelljs/global');
// Glob to match all js files except test files
const FILES_TO_PARSE = 'app/**/!(*.test).js';
const locales = i18n.appLocales;
const newLine = () => process.stdout.write('\n');
// Progress Logger
let progress;
const task = message => {
progress = animateProgress(message);
process.stdout.write(message);
return error => {
if (error) {
process.stderr.write(error);
}
clearTimeout(progress);
return addCheckmark(() => newLine());
};
};
// Wrap async functions below into a promise
const glob = pattern =>
new Promise((resolve, reject) => {
nodeGlob(
pattern,
(error, value) => (error ? reject(error) : resolve(value)),
);
});
const readFile = fileName =>
new Promise((resolve, reject) => {
fs.readFile(
fileName,
(error, value) => (error ? reject(error) : resolve(value)),
);
});
const writeFile = (fileName, data) =>
new Promise((resolve, reject) => {
fs.writeFile(
fileName,
data,
(error, value) => (error ? reject(error) : resolve(value)),
);
});
// Store existing translations into memory
const oldLocaleMappings = [];
const localeMappings = [];
// Loop to run once per locale
for (const locale of locales) {
oldLocaleMappings[locale] = {};
localeMappings[locale] = {};
// File to store translation messages into
const translationFileName = `app/translations/${locale}.json`;
try {
// Parse the old translation message JSON files
const messages = JSON.parse(fs.readFileSync(translationFileName));
const messageKeys = Object.keys(messages);
for (const messageKey of messageKeys) {
oldLocaleMappings[locale][messageKey] = messages[messageKey];
}
} catch (error) {
if (error.code !== 'ENOENT') {
process.stderr.write(
`There was an error loading this translation file: ${translationFileName}
\n${error}`,
);
}
}
}
/* push `react-intl` plugin to the existing plugins that are already configured in `package.json`
Example:
```
"babel": {
"plugins": [
["transform-object-rest-spread", { "useBuiltIns": true }]
],
"presets": [
"env",
"react"
]
}
```
*/
plugins.push(['react-intl']);
const extractFromFile = fileName => {
return readFile(fileName)
.then(code => {
// Use babel plugin to extract instances where react-intl is used
const { metadata: result } = transform(code, { presets, plugins });
for (const message of result['react-intl'].messages) {
for (const locale of locales) {
const oldLocaleMapping = oldLocaleMappings[locale][message.id];
// Merge old translations into the babel extracted instances where react-intl is used
const newMsg =
locale === DEFAULT_LOCALE ? message.defaultMessage : '';
localeMappings[locale][message.id] = oldLocaleMapping
? oldLocaleMapping
: newMsg;
}
}
})
.catch(error => {
process.stderr.write(`Error transforming file: ${fileName}\n${error}`);
});
};
const memoryTask = glob(FILES_TO_PARSE);
const memoryTaskDone = task('Storing language files in memory');
memoryTask.then(files => {
memoryTaskDone();
const extractTask = Promise.all(
files.map(fileName => extractFromFile(fileName)),
);
const extractTaskDone = task('Run extraction on all files');
// Run extraction on all files that match the glob on line 16
extractTask.then(result => {
extractTaskDone();
// Make the directory if it doesn't exist, especially for first run
mkdir('-p', 'app/translations');
let localeTaskDone;
let translationFileName;
for (const locale of locales) {
translationFileName = `app/translations/${locale}.json`;
localeTaskDone = task(
`Writing translation messages for ${locale} to: ${translationFileName}`,
);
// Sort the translation JSON file so that git diffing is easier
// Otherwise the translation messages will jump around every time we extract
let messages = {};
Object.keys(localeMappings[locale])
.sort()
.forEach(function(key) {
messages[key] = localeMappings[locale][key];
});
// Write to file the JSON representation of the translation messages
const prettified = `${JSON.stringify(messages, null, 2)}\n`;
try {
fs.writeFileSync(translationFileName, prettified);
localeTaskDone();
} catch (error) {
localeTaskDone(
`There was an error saving this translation file: ${translationFileName}
\n${error}`,
);
}
}
process.exit();
});
});
|