Skip to content

Commit

Permalink
fix: handle literals for getTranslations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Feb 25, 2025
1 parent 0e25a4f commit 7a0b827
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-sloths-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/intl-extractor": patch
---

Handle literals for `getTranslations()` call
50 changes: 48 additions & 2 deletions src/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ describe("Test parseSource", () => {
expect(result).toEqual(expected);
});

test("should parse translator object functions", () => {
const source = `
export const MyComponent = () => {
const t = useTranslations("MyComponent");
const foobar = t.html("foobar");
return (
<div>
<h1>{t.rich("title")}</h1>
</div>
)
}
`;

const result = extractLabels("MyComponent.tsx", source);
const expected = {
MyComponent: new Set(["foobar", "title"]),
};
expect(result).toEqual(expected);
});
});

describe("getTranslator usage", () => {
test("should parse source from server component using getTranslations", () => {
const source = `
export const MyComponent = async () => {
Expand Down Expand Up @@ -71,10 +95,32 @@ describe("Test parseSource", () => {
expect(result).toEqual(expected);
});

test("should parse translator object functions", () => {
test("should parse translator object functions (literal)", () => {
const source = `
export const MyComponent = () => {
const t = useTranslations("MyComponent");
const t = await getTranslations("MyComponent");
const foobar = t.html("foobar");
return (
<div>
<h1>{t.rich("title")}</h1>
</div>
)
}
`;

const result = extractLabels("MyComponent.tsx", source);
const expected = {
MyComponent: new Set(["foobar", "title"]),
};
expect(result).toEqual(expected);
});

test("should parse translator object functions (object) ", () => {
const source = `
export const MyComponent = () => {
const t = await getTranslations({ namespace: "MyComponent", locale });
const foobar = t.html("foobar");
Expand Down
8 changes: 8 additions & 0 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export function extractLabels(filename: string, source: string) {
) {
if (node.name && ts.isIdentifier(node.name)) {
const arg = callExpr.arguments[0];

// If the argument is an object, parse the object
if (ts.isObjectLiteralExpression(arg)) {
// Iterate over the object properties
for (const prop of arg.properties) {
Expand All @@ -82,6 +84,12 @@ export function extractLabels(filename: string, source: string) {
}
}
}
} else {
currentScope.variables.set(
node.name.text,
// Remove the surrounding quotes
callExpr.arguments[0].getText().slice(1, -1),
);
}
}
}
Expand Down

0 comments on commit 7a0b827

Please sign in to comment.