@saykit/format-po 0.0.0 → 0.2.0

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/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # @saykit/format-po
2
+
3
+ > Gettext PO file formatter for [SayKit](https://saykit.js.org).
4
+
5
+ The default formatter used in every SayKit example.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ pnpm add -D @saykit/format-po
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts title="saykit.config.ts"
16
+ import po from '@saykit/format-po';
17
+
18
+ // inside a bucket:
19
+ formatter: po();
20
+ ```
21
+
22
+ ### Options
23
+
24
+ - `includeReferences` (default `true`): include `#: file:line` source references.
25
+ - `includeLineNumbers` (default `true`): include line numbers in those references.
26
+
27
+ ## Documentation
28
+
29
+ See [saykit.js.org/core-concepts/formats](https://saykit.js.org/core-concepts/formats).
@@ -0,0 +1,18 @@
1
+ import { Formatter } from "@saykit/config";
2
+
3
+ //#region src/formatter.d.ts
4
+ interface FormatterOptions {
5
+ /**
6
+ * Include source references in the generated PO file.
7
+ * @default true
8
+ */
9
+ includeReferences?: boolean;
10
+ /**
11
+ * Include line numbers in source references.
12
+ * @default true
13
+ */
14
+ includeLineNumbers?: boolean;
15
+ }
16
+ declare function createPoFormatter(options?: FormatterOptions): Formatter;
17
+ //#endregion
18
+ export { createPoFormatter as default };
@@ -0,0 +1,51 @@
1
+ import PO from "pofile";
2
+ //#region src/formatter.ts
3
+ function createPoFormatter(options = {}) {
4
+ return {
5
+ extension: ".po",
6
+ parse(content) {
7
+ return PO.parse(content).items.map((item) => {
8
+ const id = item.extractedComments.find((c) => c.startsWith("id:"))?.slice(3);
9
+ const comments = item.extractedComments.filter((c) => !c.startsWith("id:")).map((c) => c.trim());
10
+ return {
11
+ id,
12
+ context: item.msgctxt,
13
+ message: item.msgid,
14
+ translation: item.msgstr[0],
15
+ comments,
16
+ references: item.references
17
+ };
18
+ });
19
+ },
20
+ stringify(messages, { locale, existingContent }) {
21
+ const po = new PO();
22
+ if (existingContent) {
23
+ const existing = PO.parse(existingContent);
24
+ Object.assign(po.headers, existing.headers);
25
+ }
26
+ po.headers["Content-Type"] ||= "text/plain; charset=UTF-8";
27
+ po.headers["Content-Transfer-Encoding"] ||= "8bit";
28
+ po.headers["Language"] ||= locale;
29
+ po.headers["X-Generator"] ||= "saykit";
30
+ for (const message of messages.sort((a, b) => a.message.localeCompare(b.message))) {
31
+ const item = new PO.Item();
32
+ item.msgid = message.message;
33
+ if (message.context) item.msgctxt = message.context;
34
+ item.msgstr = [message.translation ?? ""];
35
+ const comments = [];
36
+ if (message.id) comments.push(`id:${message.id}`);
37
+ if (message.comments.length) comments.push(...message.comments);
38
+ item.extractedComments = comments;
39
+ if (options.includeReferences !== false) {
40
+ let references = message.references ?? [];
41
+ if (options.includeLineNumbers === false) references = references.map((r) => r.replace(/:\d+$/, ""));
42
+ item.references = Array.from(new Set(references)).sort();
43
+ }
44
+ po.items.push(item);
45
+ }
46
+ return po.toString();
47
+ }
48
+ };
49
+ }
50
+ //#endregion
51
+ export { createPoFormatter as default };
package/package.json CHANGED
@@ -1,4 +1,50 @@
1
- {
2
- "name": "@saykit/format-po",
3
- "version": "0.0.0"
1
+ {
2
+ "name": "@saykit/format-po",
3
+ "version": "0.2.0",
4
+ "description": "PO file formatter for saykit",
5
+ "keywords": [
6
+ "formatter",
7
+ "gettext",
8
+ "i18n",
9
+ "po",
10
+ "saykit"
11
+ ],
12
+ "homepage": "https://github.com/k0d13/saykit#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/k0d13/saykit/issues"
15
+ },
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/k0d13/saykit.git",
20
+ "directory": "packages/format-po"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "!dist/**/*.map"
25
+ ],
26
+ "type": "module",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/formatter.d.mts",
30
+ "default": "./dist/formatter.mjs"
31
+ }
32
+ },
33
+ "publishConfig": {
34
+ "access": "public",
35
+ "provenance": true
36
+ },
37
+ "dependencies": {
38
+ "pofile": "^1.1.4"
39
+ },
40
+ "devDependencies": {
41
+ "@saykit/config": "^0.2.0"
42
+ },
43
+ "peerDependencies": {
44
+ "@saykit/config": "*"
45
+ },
46
+ "scripts": {
47
+ "check": "tsc --noEmit",
48
+ "build": "tsdown"
49
+ }
4
50
  }