|
1 | | -import path from "path"; |
2 | | -import getCurrentProject from "@/operation/getProjectType"; |
3 | | -import { |
4 | | - isFileExists, |
5 | | - moveAllFilesToSubDir, |
6 | | - writeFileFromConfig, |
7 | | -} from "@/utils/file"; |
8 | | -import fs from "fs"; |
9 | | -import i18NReactPlugin from "@/plugins/react/i18n"; |
10 | | -import i18nNextPlugin from "@/plugins/nextjs/i18n"; |
11 | | - |
12 | | -export default async function i18nAdder(addI18n: boolean) { |
13 | | - addI18n && (await addI18nInProject()); |
14 | | -} |
15 | | - |
16 | | -async function addI18nInProject() { |
17 | | - const projectType = getCurrentProject(); |
18 | | - |
19 | | - //based on the project type run the configuration |
20 | | - switch (projectType) { |
21 | | - case "next": |
22 | | - await addI18nInNext(); |
23 | | - break; |
24 | | - case "react": |
25 | | - await addI18nInReact(); |
26 | | - break; |
27 | | - default: |
28 | | - break; |
29 | | - } |
30 | | -} |
31 | | - |
32 | | -async function addI18nInNext() { |
33 | | - await writeFileFromConfig(i18nNextPlugin); |
34 | | - |
35 | | - const isTsProject = isFileExists(process.cwd(), "tsconfig.json"); |
36 | | - |
37 | | - const rootLayoutName = `${isTsProject ? "layout.tsx" : "layout.js"}`; |
38 | | - |
39 | | - const file = fs |
40 | | - .readFileSync(path.join(process.cwd(), "src", "app", rootLayoutName)) |
41 | | - .toString(); |
42 | | - |
43 | | - const rootLayoutParams = `RootLayout({ |
44 | | - children, |
45 | | - params, |
46 | | -}${ |
47 | | - isTsProject |
48 | | - ? `: { |
49 | | - children: React.ReactNode; |
50 | | - params: { lang: Locale }; |
51 | | -}` |
52 | | - : "" |
53 | | - })`; |
54 | | - |
55 | | - const newFileContent = file.replace(/RootLayout\([^)]*\)/, rootLayoutParams); |
56 | | - |
57 | | - fs.writeFileSync( |
58 | | - path.join(process.cwd(), "src", "app", rootLayoutName), |
59 | | - newFileContent |
60 | | - ); |
61 | | - |
62 | | - moveAllFilesToSubDir(path.join(process.cwd(), "src", "app"), "[lang]"); |
63 | | -} |
64 | | - |
65 | | -async function addI18nInReact() { |
66 | | - await writeFileFromConfig(i18NReactPlugin); |
67 | | -} |
| 1 | +export { default } from "./i18n"; |
0 commit comments