|
1
|
import { outdent } from "outdent" |
|
2
|
import { normalizePath } from "../utils" |
|
3
|
import { getI18nIndexFilesFromSources } from "./helpers" |
|
4
|
|
|
5
|
export async function generateI18n(sources: Set<string>) { |
|
6
|
const indexFiles = await getI18nIndexFilesFromSources(sources) |
|
7
|
|
|
8
|
const imports = indexFiles.map((file, index) => { |
|
9
|
const normalizedPath = normalizePath(file) |
|
10
|
return `import i18nTranslations${index} from "${normalizedPath}"` |
|
11
|
}) |
|
12
|
|
|
13
|
let mergeCode = '{}' |
|
14
|
if (indexFiles.length === 1) { |
|
15
|
mergeCode = 'i18nTranslations0' |
|
16
|
} else if (indexFiles.length > 1) { |
|
17
|
// Only happens in dev mode if there are 2+ plugins linked with plugin:develop |
|
18
|
// Chain deepMerge calls since it only accepts 2 arguments |
|
19
|
mergeCode = indexFiles.slice(1).reduce((acc, _, index) => { |
|
20
|
return `deepMerge(${acc}, i18nTranslations${index + 1})` |
|
21
|
}, 'i18nTranslations0') |
|
22
|
} |
|
23
|
|
|
24
|
const code = outdent` |
|
25
|
resources: ${mergeCode} |
|
26
|
` |
|
27
|
|
|
28
|
return { |
|
29
|
imports, |
|
30
|
code, |
|
31
|
} |
|
32
|
} |
|
33
|
|