-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathi18next.ts
More file actions
123 lines (106 loc) · 3.93 KB
/
i18next.ts
File metadata and controls
123 lines (106 loc) · 3.93 KB
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
import JavascriptOutputFile from "../../shared/fileTypes/JavascriptOutputFile";
import OutputFile from "../../shared/fileTypes/OutputFile";
import { applyMixins } from "../../shared";
import javascriptCodegenMixin from "../../mixins/javascriptCodegenMixin";
import JSONOutputFile from "../../shared/fileTypes/JSONOutputFile";
import BaseFramework from "./base";
export default class I18NextFramework extends applyMixins(
BaseFramework,
javascriptCodegenMixin
) {
process(
outputJsonFiles: Record<string, JSONOutputFile<{ variantId: string }>>
) {
let moduleType: "commonjs" | "module" = "commonjs";
if ("type" in this.output && this.output.type) {
moduleType = this.output.type;
}
const driverFile = new JavascriptOutputFile({
filename: "index",
path: this.outDir,
});
const filesGroupedByVariantId = Object.values(outputJsonFiles).reduce(
(acc, file) => {
const variantId = file.metadata.variantId;
acc[variantId] ??= [];
acc[variantId].push(file);
return acc;
},
{} as Record<string, OutputFile[]>
);
if (moduleType === "module") {
driverFile.content += this.generateImportStatements(outputJsonFiles);
driverFile.content += `\n`;
driverFile.content += this.codegenDefaultExport(
this.generateExportedObjectString(filesGroupedByVariantId)
);
} else {
driverFile.content += this.generateRequireStatements(outputJsonFiles);
driverFile.content += `\n`;
driverFile.content += this.codegenModuleExports(
this.generateExportedObjectString(filesGroupedByVariantId)
);
}
return [driverFile];
}
/**
* Generates the import statements for the driver file. One import per generated json file.
* @param outputJsonFiles - The output json files.
* @returns The import statements, stringified.
*/
private generateImportStatements(
outputJsonFiles: Record<string, JSONOutputFile<{ variantId: string }>>
) {
let importStatements = "";
for (const file of Object.values(outputJsonFiles)) {
importStatements += this.codegenDefaultImport(
this.sanitizeStringForJSVariableName(file.filename),
`./${file.filenameWithExtension}`
);
}
return importStatements;
}
/**
* Generates the require statements for the driver file. One require per generated json file.
* @param outputJsonFiles - The output json files.
* @returns The require statements, stringified.
*/
private generateRequireStatements(
outputJsonFiles: Record<string, JSONOutputFile<{ variantId: string }>>
) {
let requireStatements = "";
for (const file of Object.values(outputJsonFiles)) {
requireStatements += this.codegenDefaultRequire(
this.sanitizeStringForJSVariableName(file.filename),
`./${file.filenameWithExtension}`
);
}
return requireStatements;
}
/**
* Generates the default export for the driver file. By default this is an object with the json imports grouped by variant id.
* @param filesGroupedByVariantId - The files grouped by variant id.
* @returns The default export, stringified.
*/
private generateExportedObjectString(
filesGroupedByVariantId: Record<string, OutputFile[]>
) {
const variantIds = Object.keys(filesGroupedByVariantId);
let defaultExportObjectString = "{\n";
for (let i = 0; i < variantIds.length; i++) {
const variantId = variantIds[i];
const files = filesGroupedByVariantId[variantId];
defaultExportObjectString += `${this.codegenPad(1)}"${variantId}": {\n`;
for (const file of files) {
defaultExportObjectString += `${this.codegenPad(
2
)}...${this.sanitizeStringForJSVariableName(file.filename)},\n`;
}
defaultExportObjectString += `${this.codegenPad(1)}}${
i < variantIds.length - 1 ? `,\n` : `\n`
}`;
}
defaultExportObjectString += `}`;
return defaultExportObjectString;
}
}