@open-xchange/rolldown-plugin-i18next-gettext 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## `0.0.1` – 2026-Apr-10
4
+
5
+ - initial release
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Open-Xchange GmbH, Germany <info@open-xchange.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # @open-xchange/rolldown-plugin-i18next-gettext
2
+
3
+ A [rolldown](https://rolldown.rs/) plugin that allows building libraries using [i18next](https://www.i18next.com/) in source code with gettext's `.po` and `.pot` files under the hood.
4
+
5
+ This plugin has the following responsibilities:
6
+
7
+ - All `.po` files imported in source code will be converted to i18next translation catalogs.
8
+ - A virtual module import makes all translation catalogs from selected `.po` files available in a single dictionary to be able to pick translation languages dynamically.
9
+ - The source code will be scanned for translation strings (`t` function calls), and a `.pot` file containing all strings will be generated into the output directory.
10
+
11
+ ## Configuration
12
+
13
+ Add the plugin to your rolldown configuration (or compatible configurations like Vite or tsdown):
14
+
15
+ ```ts
16
+ // rolldown.config.ts
17
+
18
+ import { defineConfig } from 'rolldown'
19
+ import i18nextPlugin from '@open-xchange/rolldown-plugin-i18next-gettext'
20
+
21
+ export default defineConfig(() => {
22
+ plugins: [
23
+ i18nextPlugin({
24
+ poFiles: 'i18n/*.po',
25
+ potFiles: {
26
+ srcFiles: 'src/**/*.{js,jsx,ts,tsx}',
27
+ outputPath: '[NAMESPACE].pot',
28
+ projectName: 'My Project',
29
+ },
30
+ }),
31
+ ],
32
+ })
33
+ ```
34
+
35
+ ### Options
36
+
37
+ | Name | Type | Default | Description |
38
+ | - | - | - | - |
39
+ | `poFiles` | `string\|string[]` | _required_ | Glob pattern(s) for all PO files containing the translations. |
40
+ | `potFiles` | | | Configuration for the source code scanner and POT file generator. If omitted, no POT files will be generated. |
41
+ | `potFiles.srcFiles` | `string\|string[]` | `src/**/*.{js,jsx,ts,tsx}` | Glob pattern(s) for all source files to be scanned for UI strings. |
42
+ | `potFiles.outputPath` | `string` | `[NAMESPACE].pot` | Path to and filename of the generated POT files, relative to the build output directory. Should contain the placeholder `[NAMESPACE]` if the project contains translation strings in different i18next namespaces. |
43
+ | `potFiles.projectName` | `string` | _required_ | The project name to be inserted into the POT file under the key 'Project-Id-Version'. May contain the placeholder `[NAMESPACE]` |
44
+
45
+ ## Usage
46
+
47
+ Register TypeScript type definitions for the `virtual:i18next-po-catalogs` module import:
48
+
49
+ ```json
50
+ // tsconfig.json
51
+ {
52
+ "types": [
53
+ "@open-xchange/rolldown-plugin-i18next-gettext/virtual"
54
+ ],
55
+ }
56
+ ```
57
+
58
+ Use the virtual modules in your code:
59
+
60
+ ```ts
61
+ // import catalogs for all languages
62
+ import catalogs from 'virtual:i18next-po-catalogs'
63
+ console.log(catalogs.de_DE)
64
+
65
+ // import catalog for one language
66
+ import catalog from 'i18n/de_DE.po'
67
+ console.log(catalog)
68
+ ```
@@ -0,0 +1,65 @@
1
+ import { Plugin } from "rolldown";
2
+
3
+ //#region src/index.d.ts
4
+ /**
5
+ * Configuration options for the plugin '@open-xchange/rolldown-plugin-i18next-gettext'.
6
+ */
7
+ interface RolldownPluginI18nextGettextOptions {
8
+ /**
9
+ * Glob pattern(s) for all PO files containing the translations. Matching PO
10
+ * files will be converted to i18next translation catalogs when imported in
11
+ * source code directly, and will be included in the virtual module providing
12
+ * a dictionary of all translation catalogs, mapped by the base names of the
13
+ * PO files.
14
+ */
15
+ poFiles: string | readonly string[];
16
+ /**
17
+ * Configuration for the source code scanner and POT file generator. If
18
+ * omitted, no POT files will be generated.
19
+ */
20
+ potFiles?: {
21
+ /**
22
+ * Glob pattern(s) for all source files to be scanned for UI strings.
23
+ *
24
+ * @default
25
+ * 'src/**\/*.{js,jsx,ts,tsx}'
26
+ */
27
+ srcFiles?: string | readonly string[];
28
+ /**
29
+ * Path to and filename of the generated POT files, relative to the build
30
+ * output directory. Should contain the placeholder `[NAMESPACE]` if the
31
+ * project contains translation strings in different i18next namespaces.
32
+ *
33
+ * @default
34
+ * '[NAMESPACE].pot'
35
+ */
36
+ outputPath?: string;
37
+ /**
38
+ * The project name to be inserted into the POT files under the key
39
+ * 'Project-Id-Version'. May contain the placeholder `[NAMESPACE]`.
40
+ */
41
+ projectName: string;
42
+ };
43
+ }
44
+ /**
45
+ * Identifier of this plugin.
46
+ */
47
+ declare const PROJECT_NAME = "@open-xchange/rolldown-plugin-i18next-gettext";
48
+ /**
49
+ * Identifier of the virtual module providing all translation catalogs in a
50
+ * single object.
51
+ */
52
+ declare const VIRTUAL_MODULE_ID = "virtual:i18next-po-catalogs";
53
+ /**
54
+ * Rolldown plugin for using i18next with gettext `.po` files under the hood.
55
+ *
56
+ * @param options
57
+ * Plugin configuration.
58
+ *
59
+ * @returns
60
+ * The rolldown plugin object.
61
+ */
62
+ declare function rolldownPluginI18nextGettext(options: RolldownPluginI18nextGettextOptions): Plugin;
63
+ //#endregion
64
+ export { PROJECT_NAME, RolldownPluginI18nextGettextOptions, VIRTUAL_MODULE_ID, rolldownPluginI18nextGettext as default };
65
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;AAaA;UAAiB,mCAAA;;;;;;;;EASf,OAAA;EA8Ba;AASf;;;EAjCE,QAAA;IAiCuB;AAMzB;;;;;IA/BI,QAAA;IA6DgD;;;;;;;;IAnDhD,UAAA;;;;;IAMA,WAAA;EAAA;AAAA;;;;cASS,YAAA;;;;;cAMA,iBAAA;;;;;;;;;;iBA8BW,4BAAA,CAA6B,OAAA,EAAS,mCAAA,GAAsC,MAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,82 @@
1
+ import * as path from "node:path";
2
+ import pm from "picomatch";
3
+ import { glob } from "tinyglobby";
4
+ import { parsePoFile, parseSourceFiles } from "@open-xchange/i18next-po-parser";
5
+ //#region src/index.ts
6
+ /**
7
+ * Identifier of this plugin.
8
+ */
9
+ const PROJECT_NAME = "@open-xchange/rolldown-plugin-i18next-gettext";
10
+ /**
11
+ * Identifier of the virtual module providing all translation catalogs in a
12
+ * single object.
13
+ */
14
+ const VIRTUAL_MODULE_ID = "virtual:i18next-po-catalogs";
15
+ /**
16
+ * Creates the source code of a module with a default export with the passed
17
+ * stringified JSON data.
18
+ *
19
+ * @param json
20
+ * The JSON data to be exported from the generated source file.
21
+ *
22
+ * @returns
23
+ * The generated source file.
24
+ */
25
+ function createModule(json) {
26
+ return {
27
+ code: `export default ${JSON.stringify(json)}`,
28
+ map: { mappings: "" }
29
+ };
30
+ }
31
+ /**
32
+ * Rolldown plugin for using i18next with gettext `.po` files under the hood.
33
+ *
34
+ * @param options
35
+ * Plugin configuration.
36
+ *
37
+ * @returns
38
+ * The rolldown plugin object.
39
+ */
40
+ function rolldownPluginI18nextGettext(options) {
41
+ const RESOLVED_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
42
+ return {
43
+ name: PROJECT_NAME,
44
+ resolveId(id) {
45
+ return id === "virtual:i18next-po-catalogs" ? RESOLVED_MODULE_ID : null;
46
+ },
47
+ async load(id) {
48
+ if (id === RESOLVED_MODULE_ID) {
49
+ const map = {};
50
+ for (const entry of await glob(options.poFiles)) {
51
+ const basename = path.parse(entry).name;
52
+ map[basename] = await parsePoFile(entry);
53
+ }
54
+ return createModule(map);
55
+ }
56
+ const relPath = path.relative(process.cwd(), id).replaceAll(path.sep, path.posix.sep);
57
+ if (!pm.isMatch(relPath, options.poFiles)) return;
58
+ return createModule(await parsePoFile(id));
59
+ },
60
+ async generateBundle() {
61
+ if (!options.potFiles) return;
62
+ const { srcFiles = "src/**/*.{js,jsx,ts,tsx}", outputPath = "[NAMESPACE].pot", projectName } = options.potFiles;
63
+ const catalogs = await parseSourceFiles({
64
+ files: srcFiles,
65
+ project: projectName
66
+ });
67
+ if (catalogs.size > 1 && !outputPath.includes("[NAMESPACE]")) this.error("multiple POT files written to same file location (missing placeholder `[NAMESPACE]` in option potFile)");
68
+ for (const [namespace, source] of catalogs) {
69
+ const fileName = outputPath.replaceAll("[NAMESPACE]", namespace);
70
+ this.emitFile({
71
+ type: "asset",
72
+ fileName,
73
+ source
74
+ });
75
+ }
76
+ }
77
+ };
78
+ }
79
+ //#endregion
80
+ export { PROJECT_NAME, VIRTUAL_MODULE_ID, rolldownPluginI18nextGettext as default };
81
+
82
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["\nimport * as path from 'node:path'\n\nimport type { LoadResult, Plugin } from 'rolldown'\nimport pm from 'picomatch'\nimport { glob } from 'tinyglobby'\nimport { parsePoFile, parseSourceFiles } from '@open-xchange/i18next-po-parser'\n\n// types ======================================================================\n\n/**\n * Configuration options for the plugin '@open-xchange/rolldown-plugin-i18next-gettext'.\n */\nexport interface RolldownPluginI18nextGettextOptions {\n\n /**\n * Glob pattern(s) for all PO files containing the translations. Matching PO\n * files will be converted to i18next translation catalogs when imported in\n * source code directly, and will be included in the virtual module providing\n * a dictionary of all translation catalogs, mapped by the base names of the\n * PO files.\n */\n poFiles: string | readonly string[]\n\n /**\n * Configuration for the source code scanner and POT file generator. If\n * omitted, no POT files will be generated.\n */\n potFiles?: {\n\n /**\n * Glob pattern(s) for all source files to be scanned for UI strings.\n *\n * @default\n * 'src/**\\/*.{js,jsx,ts,tsx}'\n */\n srcFiles?: string | readonly string[]\n\n /**\n * Path to and filename of the generated POT files, relative to the build\n * output directory. Should contain the placeholder `[NAMESPACE]` if the\n * project contains translation strings in different i18next namespaces.\n *\n * @default\n * '[NAMESPACE].pot'\n */\n outputPath?: string\n\n /**\n * The project name to be inserted into the POT files under the key\n * 'Project-Id-Version'. May contain the placeholder `[NAMESPACE]`.\n */\n projectName: string\n }\n}\n\n// constants ==================================================================\n\n/**\n * Identifier of this plugin.\n */\nexport const PROJECT_NAME = '@open-xchange/rolldown-plugin-i18next-gettext'\n\n/**\n * Identifier of the virtual module providing all translation catalogs in a\n * single object.\n */\nexport const VIRTUAL_MODULE_ID = 'virtual:i18next-po-catalogs'\n\n// functions ==================================================================\n\n/**\n * Creates the source code of a module with a default export with the passed\n * stringified JSON data.\n *\n * @param json\n * The JSON data to be exported from the generated source file.\n *\n * @returns\n * The generated source file.\n */\nfunction createModule(json: unknown): LoadResult {\n const code = `export default ${JSON.stringify(json)}`\n return { code, map: { mappings: '' } }\n}\n\n// plugin =====================================================================\n\n/**\n * Rolldown plugin for using i18next with gettext `.po` files under the hood.\n *\n * @param options\n * Plugin configuration.\n *\n * @returns\n * The rolldown plugin object.\n */\nexport default function rolldownPluginI18nextGettext(options: RolldownPluginI18nextGettextOptions): Plugin {\n\n const RESOLVED_MODULE_ID = `\\0${VIRTUAL_MODULE_ID}`\n\n return {\n name: PROJECT_NAME,\n\n // recognize virtual module identifier\n resolveId(id) {\n return (id === VIRTUAL_MODULE_ID) ? RESOLVED_MODULE_ID : null\n },\n\n // convert PO files to i18next JSON v4\n async load(id) {\n\n // virtual module: build single dictionary with all languages from PO files\n if (id === RESOLVED_MODULE_ID) {\n const map = {} as Record<string, unknown>\n for (const entry of await glob(options.poFiles)) {\n const basename = path.parse(entry).name\n map[basename] = await parsePoFile(entry)\n }\n return createModule(map)\n }\n\n // try to find a matching PO file\n const relPath = path.relative(process.cwd(), id).replaceAll(path.sep, path.posix.sep)\n if (!pm.isMatch(relPath, options.poFiles as pm.Glob)) { return }\n\n // read the PO file and convert it to i18next JSON\n return createModule(await parsePoFile(id))\n },\n\n // parse source files and create POT files per namespace\n async generateBundle() {\n\n // early exit if no POT files will be generated\n if (!options.potFiles) return\n const { srcFiles = 'src/**/*.{js,jsx,ts,tsx}', outputPath = '[NAMESPACE].pot', projectName } = options.potFiles\n\n // parse all source files and extract translations\n const catalogs = await parseSourceFiles({ files: srcFiles as pm.Glob, project: projectName })\n\n // warn when writing multiple POT files to same file location\n if ((catalogs.size > 1) && !outputPath.includes('[NAMESPACE]')) {\n this.error('multiple POT files written to same file location (missing placeholder `[NAMESPACE]` in option potFile)')\n }\n\n // add all POT files as assets to the bundle\n for (const [namespace, source] of catalogs) {\n const fileName = outputPath.replaceAll('[NAMESPACE]', namespace)\n this.emitFile({ type: 'asset', fileName, source })\n }\n },\n }\n}\n"],"mappings":";;;;;;;;AA6DA,MAAa,eAAe;;;;;AAM5B,MAAa,oBAAoB;;;;;;;;;;;AAcjC,SAAS,aAAa,MAA2B;AAE/C,QAAO;EAAE,MADI,kBAAkB,KAAK,UAAU,KAAK;EACpC,KAAK,EAAE,UAAU,IAAI;EAAE;;;;;;;;;;;AAcxC,SAAwB,6BAA6B,SAAsD;CAEzG,MAAM,qBAAqB,KAAK;AAEhC,QAAO;EACL,MAAM;EAGN,UAAU,IAAI;AACZ,UAAQ,OAAA,gCAA4B,qBAAqB;;EAI3D,MAAM,KAAK,IAAI;AAGb,OAAI,OAAO,oBAAoB;IAC7B,MAAM,MAAM,EAAE;AACd,SAAK,MAAM,SAAS,MAAM,KAAK,QAAQ,QAAQ,EAAE;KAC/C,MAAM,WAAW,KAAK,MAAM,MAAM,CAAC;AACnC,SAAI,YAAY,MAAM,YAAY,MAAM;;AAE1C,WAAO,aAAa,IAAI;;GAI1B,MAAM,UAAU,KAAK,SAAS,QAAQ,KAAK,EAAE,GAAG,CAAC,WAAW,KAAK,KAAK,KAAK,MAAM,IAAI;AACrF,OAAI,CAAC,GAAG,QAAQ,SAAS,QAAQ,QAAmB,CAAI;AAGxD,UAAO,aAAa,MAAM,YAAY,GAAG,CAAC;;EAI5C,MAAM,iBAAiB;AAGrB,OAAI,CAAC,QAAQ,SAAU;GACvB,MAAM,EAAE,WAAW,4BAA4B,aAAa,mBAAmB,gBAAgB,QAAQ;GAGvG,MAAM,WAAW,MAAM,iBAAiB;IAAE,OAAO;IAAqB,SAAS;IAAa,CAAC;AAG7F,OAAK,SAAS,OAAO,KAAM,CAAC,WAAW,SAAS,cAAc,CAC5D,MAAK,MAAM,yGAAyG;AAItH,QAAK,MAAM,CAAC,WAAW,WAAW,UAAU;IAC1C,MAAM,WAAW,WAAW,WAAW,eAAe,UAAU;AAChE,SAAK,SAAS;KAAE,MAAM;KAAS;KAAU;KAAQ,CAAC;;;EAGvD"}
@@ -0,0 +1,18 @@
1
+
2
+ declare module '*.po' {
3
+
4
+ export type Catalog = Record<string, string>
5
+
6
+ const catalog: Catalog
7
+ export default catalog
8
+ }
9
+
10
+ declare module 'virtual:i18next-po-catalogs' {
11
+
12
+ // eslint-disable-next-line n/no-missing-import
13
+ import { Catalog } from 'test.po'
14
+ export { Catalog }
15
+
16
+ const catalogs: Record<string, Catalog>
17
+ export default catalogs
18
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@open-xchange/rolldown-plugin-i18next-gettext",
3
+ "version": "0.0.1",
4
+ "description": "Rolldown integration of i18next using gettext",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://gitlab.open-xchange.com/fspd/commons/plugins.git",
8
+ "directory": "packages/rolldown-plugin-i18next-gettext"
9
+ },
10
+ "license": "MIT",
11
+ "engines": {
12
+ "node": ">=20.18"
13
+ },
14
+ "type": "module",
15
+ "exports": {
16
+ ".": "./dist/index.mjs",
17
+ "./package.json": "./package.json",
18
+ "./virtual": {
19
+ "types": "./dist/virtual.d.ts"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "CHANGELOG.*"
25
+ ],
26
+ "dependencies": {
27
+ "@open-xchange/i18next-po-parser": "^1.4.0",
28
+ "picomatch": "^4.0.4",
29
+ "tinyglobby": "^0.2.16"
30
+ },
31
+ "devDependencies": {
32
+ "@types/picomatch": "^4.0.3",
33
+ "@vitest/coverage-v8": "^4.1.3",
34
+ "i18next": "^26.0.4",
35
+ "rolldown": "1.0.0-rc.15",
36
+ "tsdown": "^0.21.7",
37
+ "vitest": "^4.1.3",
38
+ "@open-xchange/linter-presets": "^1.22.0",
39
+ "@open-xchange/vitest-plugin-extended": "^1.6.0",
40
+ "@open-xchange/tsconfig": "^0.0.3",
41
+ "@open-xchange/vitest-plugin-utils": "^0.0.3"
42
+ },
43
+ "peerDependencies": {
44
+ "rolldown": "*"
45
+ },
46
+ "scripts": {
47
+ "build": "tsdown",
48
+ "coverage": "vitest run --coverage",
49
+ "lint": "tsc && tsc --project src && tsc --project test && eslint .",
50
+ "test": "vitest run",
51
+ "verify": "pnpm build && pnpm lint && pnpm test"
52
+ }
53
+ }