@saykit/format-po 0.0.0-beta-20260309151609
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/dist/formatter.d.mts +7 -0
- package/dist/formatter.mjs +49 -0
- package/package.json +50 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import PO from "pofile";
|
|
2
|
+
|
|
3
|
+
//#region src/formatter.ts
|
|
4
|
+
function createFormatter() {
|
|
5
|
+
return {
|
|
6
|
+
extension: ".po",
|
|
7
|
+
async parse(content, context) {
|
|
8
|
+
const po = PO.parse(content);
|
|
9
|
+
if (!po.headers["X-Generator"]?.startsWith("saykit")) throw new Error("PO file was not generated by saykit");
|
|
10
|
+
if (po.headers.Language !== context.locale) throw new Error("PO file locale does not match the expected locale");
|
|
11
|
+
return po.items.map((item) => {
|
|
12
|
+
const id = item.extractedComments.find((c) => c.startsWith("id:"))?.slice(3);
|
|
13
|
+
const comments = item.extractedComments.filter((c) => !c.startsWith("id:")).map((c) => c.trim());
|
|
14
|
+
return {
|
|
15
|
+
id,
|
|
16
|
+
context: item.msgctxt,
|
|
17
|
+
message: item.msgid,
|
|
18
|
+
translation: item.msgstr[0],
|
|
19
|
+
comments,
|
|
20
|
+
references: item.references
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
async stringify(messages, context) {
|
|
25
|
+
const po = new PO();
|
|
26
|
+
po.headers["Content-Type"] = "text/plain; charset=UTF-8";
|
|
27
|
+
po.headers["Content-Transfer-Encoding"] = "8bit";
|
|
28
|
+
po.headers.Language = context.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
|
+
item.references = message.references;
|
|
40
|
+
po.items.push(item);
|
|
41
|
+
}
|
|
42
|
+
return po.toString();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { createFormatter as default };
|
|
49
|
+
//# sourceMappingURL=formatter.mjs.map
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@saykit/format-po",
|
|
3
|
+
"version": "0.0.0-beta-20260309151609",
|
|
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.0.0-beta-20260309151609"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@saykit/config": "0.0.0-beta-20260309151609"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"check": "tsc --noEmit",
|
|
48
|
+
"build": "tsdown"
|
|
49
|
+
}
|
|
50
|
+
}
|