forked from webhintio/hint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-i18n.js
44 lines (36 loc) · 1.21 KB
/
create-i18n.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
const fs = require('fs');
const path = require('path');
const cwd = process.cwd();
// Generate i18n.import.ts
const i18nFile = path.join(cwd, 'src', '_locales', 'en', 'messages.json');
const packageJSON = require(path.join(cwd, 'package.json'));
const messages = require(i18nFile);
const rxValidKey = /^[_a-zA-Z0-9]+$/;
const filename = path.join(cwd, 'src', 'i18n.import.ts');
const content = `// autogenerated by scripts/create-i18n.js
import { getMessage as getMessageGlobal } from '${packageJSON.name === '@hint/utils-i18n' ? './get-message' : '@hint/utils-i18n'}';
export type MessageName =
${
Object.keys(messages).map((name) => {
if (!rxValidKey.test(name)) {
throw new Error(`Localization keys must match ${rxValidKey}. Found '${name}'.`);
}
return ` '${name}'`;
})
.join(' |\n')
};
export const getMessage = (message: MessageName, language: string, substitutions?: string | string[]) => {
const options = {
language,
substitutions
};
return getMessageGlobal(message, __dirname, options);
};
`;
fs.writeFile(filename, content, (err) => {
if (err) {
throw err;
} else {
console.log(`Created: ${filename} `);
}
});