|
| 1 | +import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "fs/promises"; |
| 2 | +import { join } from "path"; |
| 3 | +import { tmpdir } from "os"; |
| 4 | +import { cwd } from "process"; |
| 5 | +import { execSync } from "child_process"; |
| 6 | + |
| 7 | +describe("config format tests", () => { |
| 8 | + type WithTempDirTest = (tmpDir: string) => Promise<any>; |
| 9 | + |
| 10 | + const withTempDir = async (testFunc: WithTempDirTest) => { |
| 11 | + let tmpDir; |
| 12 | + try { |
| 13 | + tmpDir = await mkdtemp(join(tmpdir(), "vue3-gettext-")); |
| 14 | + await testFunc(tmpDir); |
| 15 | + } finally { |
| 16 | + if (tmpDir) { |
| 17 | + await rm(tmpDir, { recursive: true, force: true }); |
| 18 | + } |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + const testConfigWithExtract = async (tmpDir: string, config: string, configFileName: string, isModule: boolean) => { |
| 23 | + const packageJson = { |
| 24 | + name: "test", |
| 25 | + version: "0.0.1", |
| 26 | + type: isModule ? "module" : "commonjs", |
| 27 | + }; |
| 28 | + for (const d of ["src", "scripts", "node_modules"]) { |
| 29 | + await symlink(join(cwd(), d), join(tmpDir, d)); |
| 30 | + } |
| 31 | + await writeFile(join(tmpDir, "package.json"), JSON.stringify(packageJson)); |
| 32 | + await writeFile(join(tmpDir, configFileName), config); |
| 33 | + await mkdir(join(tmpDir, "srctest", "lang"), { recursive: true }); |
| 34 | + await writeFile( |
| 35 | + join(tmpDir, "srctest", "example.js"), |
| 36 | + ` |
| 37 | +const { $gettext } = useGettext(); |
| 38 | +$gettext('Translate me'); |
| 39 | +`, |
| 40 | + ); |
| 41 | + execSync(`sh -c 'cd ${tmpDir}; ts-node ./scripts/gettext_extract.ts'`); |
| 42 | + const appEnPo = (await readFile(join(tmpDir, "srctest", "lang", "en", "app.po"))).toString(); |
| 43 | + const appEnPoLines = appEnPo.trim().split("\n"); |
| 44 | + expect(appEnPoLines).toContain('msgid "Translate me"'); |
| 45 | + expect(appEnPoLines[appEnPoLines.length - 1]).toEqual('msgstr "Translate me"'); |
| 46 | + const appFrPo = (await readFile(join(tmpDir, "srctest", "lang", "fr", "app.po"))).toString(); |
| 47 | + const appFrPoLines = appFrPo.trim().split("\n"); |
| 48 | + expect(appFrPoLines).toContain('msgid "Translate me"'); |
| 49 | + expect(appFrPoLines[appFrPoLines.length - 1]).toEqual('msgstr ""'); |
| 50 | + }; |
| 51 | + |
| 52 | + it("load a commonjs format", async () => { |
| 53 | + await withTempDir( |
| 54 | + async (tmpDir) => |
| 55 | + await testConfigWithExtract( |
| 56 | + tmpDir, |
| 57 | + ` |
| 58 | +module.exports = { |
| 59 | + input: { |
| 60 | + path: './srctest', |
| 61 | + }, |
| 62 | + output: { |
| 63 | + path: './srctest/lang', |
| 64 | + locales: ['en', 'fr'], |
| 65 | + }, |
| 66 | +};`, |
| 67 | + "gettext.config.js", |
| 68 | + false, |
| 69 | + ), |
| 70 | + ); |
| 71 | + }); |
| 72 | + it("load an ESM format", async () => { |
| 73 | + await withTempDir( |
| 74 | + async (tmpDir) => |
| 75 | + await testConfigWithExtract( |
| 76 | + tmpDir, |
| 77 | + ` |
| 78 | +export default { |
| 79 | + input: { |
| 80 | + path: './srctest', |
| 81 | + }, |
| 82 | + output: { |
| 83 | + path: './srctest/lang', |
| 84 | + locales: ['en', 'fr'], |
| 85 | + }, |
| 86 | +};`, |
| 87 | + "gettext.config.js", |
| 88 | + true, |
| 89 | + ), |
| 90 | + ); |
| 91 | + }); |
| 92 | +}); |
0 commit comments