commit 4afc876499f6672999110330d4fd9c082019db96
parent 325b61fe74d6f21b827ed3ae948c843378e42283
Author: Sebastian <sebasjm@gmail.com>
Date: Sat, 9 Nov 2024 16:57:07 -0300
include screen id on i18n
Diffstat:
2 files changed, 94 insertions(+), 11 deletions(-)
diff --git a/packages/pogen/src/potextract.test.ts b/packages/pogen/src/potextract.test.ts
@@ -21,6 +21,7 @@ import { processFile2 } from "./potextract.js";
function wrapIntoFunction(src: string): string {
return `
function testing():VNode {
+const TALER_SCREEN_ID = 5;
return ${src}
}
`;
@@ -38,7 +39,8 @@ function process(src: string): string {
test("should extract the key from inner body", (t) => {
t.deepEqual(
process(`<i18n.Translate>something</i18n.Translate>`),
- `#: test.tsx:3
+ `#. screenid: 5
+#: test.tsx:4
#, c-format
msgid "something"
msgstr ""`,
@@ -52,7 +54,8 @@ test("should support context on tags", (t) => {
<i18n.Translate context="some_context" anotherkey="not the context" asd={"asd"} zxc={asd()}>something</i18n.Translate>
</div>`,
),
- `#: test.tsx:4
+ `#. screenid: 5
+#: test.tsx:5
#, c-format
msgctxt "some_context"
msgid "something"
@@ -63,7 +66,8 @@ msgstr ""`,
test("should support context on string template", (t) => {
t.deepEqual(
process(`return i18n.context("wire transfer")\`send\`;`),
- `#: test.tsx:3
+ `#. screenid: 5
+#: test.tsx:4
#, c-format
msgctxt "wire transfer"
msgid "send"
@@ -76,13 +80,15 @@ test("should support same message id with different context", (t) => {
process(
`return i18n.context("wire transfer")\`send\` + i18n.context("gift")\`send\`;`,
),
- `#: test.tsx:3
+ `#. screenid: 5
+#: test.tsx:4
#, c-format
msgctxt "wire transfer"
msgid "send"
msgstr ""
-#: test.tsx:3
+#. screenid: 5
+#: test.tsx:4
#, c-format
msgctxt "gift"
msgid "send"
@@ -95,8 +101,27 @@ test("should support on string template", (t) => {
process(`
// comment of the translation
return i18n.str\`another key\`;`),
- `#. comment of the translation
-#: test.tsx:5
+ `#. screenid: 5
+#. comment of the translation
+#: test.tsx:6
+#, c-format
+msgid "another key"
+msgstr ""`,
+ );
+});
+
+test("should override screen id", (t) => {
+ t.deepEqual(
+ process(`
+ {
+ const TALER_SCREEN_ID = 6;
+ // comment of the translation
+ return i18n.str\`another key\`;
+ }
+ `),
+ `#. screenid: 6
+#. comment of the translation
+#: test.tsx:8
#, c-format
msgid "another key"
msgstr ""`,
diff --git a/packages/pogen/src/potextract.ts b/packages/pogen/src/potextract.ts
@@ -185,6 +185,18 @@ function processTaggedTemplateExpression(
return res;
}
+function formatScreenId(
+ sourceFile: ts.SourceFile,
+ outChunks: string[],
+ screenId: string,
+) {
+ if (!screenId) {
+ console.error("missing screen id for file" + sourceFile.fileName);
+ } else {
+ outChunks.push(`#. screenid: ${screenId}\n`);
+ }
+}
+
function formatMsgComment(
sourceFile: ts.SourceFile,
outChunks: string[],
@@ -360,7 +372,44 @@ function getJsxPlural(node: ts.Node) {
return res;
}
+function searchScreenId(parents: ts.Node[], sourceFile: ts.SourceFile) {
+ var result = undefined;
+ parents.forEach((parent) => {
+ // console.log("parent => ", ts.SyntaxKind[parent.kind]);
+ if (result) return;
+ parent.forEachChild((node) => {
+ // console.log(" childs => ", ts.SyntaxKind[node.kind]);
+
+ switch (node.kind) {
+ case ts.SyntaxKind.VariableStatement: {
+ const v = node as ts.VariableStatement;
+ const found = v.declarationList.declarations.find(
+ (d) => d.name.getText(sourceFile) === "TALER_SCREEN_ID",
+ );
+ if (found) {
+ if (found.initializer.kind === ts.SyntaxKind.NumericLiteral) {
+ const id = found.initializer.getText(sourceFile);
+ result = id;
+ } else {
+ console.error("TALER_SCREEN_ID but is not a NumericLiteral");
+ }
+ return;
+ }
+ }
+ // case ts.SyntaxKind.VariableDeclaration: {
+ // const v = node as ts.VariableDeclaration;
+ // console.log(v);
+ // return;
+ // }
+ }
+ });
+ });
+ // console.log("");
+ return result;
+}
+
function processNode(
+ parents: ts.Node[],
node: ts.Node,
preLastTokLine: number,
lastTokLine: number,
@@ -384,6 +433,8 @@ function processNode(
const msgid = context + content;
if (!knownMessageIds.has(msgid)) {
knownMessageIds.add(msgid);
+ const screenId = searchScreenId(parents, sourceFile);
+ formatScreenId(sourceFile, outChunks, screenId);
formatMsgComment(sourceFile, outChunks, line, comment);
formatMsgLine(outChunks, "msgctxt", context);
formatMsgLine(outChunks, "msgid", content);
@@ -410,6 +461,8 @@ function processNode(
const msgid = context + singularForm;
if (!knownMessageIds.has(msgid)) {
knownMessageIds.add(msgid);
+ const screenId = searchScreenId(parents, sourceFile);
+ formatScreenId(sourceFile, outChunks, screenId);
formatMsgLine(outChunks, "msgctxt", context);
formatMsgLine(outChunks, "msgid", singularForm);
formatMsgLine(outChunks, "msgid_plural", pluralForm);
@@ -463,6 +516,8 @@ function processNode(
const msgid = path.ctx + t1.template;
if (!knownMessageIds.has(msgid)) {
knownMessageIds.add(msgid);
+ const screenId = searchScreenId(parents, sourceFile);
+ formatScreenId(sourceFile, outChunks, screenId);
formatMsgComment(sourceFile, outChunks, line, comment);
formatMsgLine(outChunks, "msgctxt", path.ctx);
formatMsgLine(outChunks, "msgid", t1.template);
@@ -495,6 +550,8 @@ function processNode(
const msgid = context + template;
if (!knownMessageIds.has(msgid)) {
knownMessageIds.add(msgid);
+ const screenId = searchScreenId(parents, sourceFile);
+ formatScreenId(sourceFile, outChunks, screenId);
formatMsgComment(sourceFile, outChunks, line, comment);
formatMsgLine(outChunks, "msgctxt", context);
formatMsgLine(outChunks, "msgid", template);
@@ -505,9 +562,10 @@ function processNode(
}
}
- ts.forEachChild(node, (n) => {
+ ts.forEachChild(node, (child) => {
processNode(
- n,
+ [node, ...parents],
+ child,
lastTokLine,
preLastTokLine,
sourceFile,
@@ -521,7 +579,7 @@ export function processFile2(sourceFile: ts.SourceFile): string {
// let lastTokLine = 0;
// let preLastTokLine = 0;
const result: string[] = new Array<string>();
- processNode(sourceFile, 0, 0, sourceFile, result, new Set<string>());
+ processNode([], sourceFile, 0, 0, sourceFile, result, new Set<string>());
return result.join("");
}
@@ -532,7 +590,7 @@ export function processFile(
) {
// let lastTokLine = 0;
// let preLastTokLine = 0;
- processNode(sourceFile, 0, 0, sourceFile, outChunks, knownMessageIds);
+ processNode([], sourceFile, 0, 0, sourceFile, outChunks, knownMessageIds);
}
export function potextract() {