Skip to content

Commit

Permalink
feat: handle getTranslations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Apr 12, 2024
1 parent 63a871c commit 2dbb51e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-donkeys-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labd/intl-extractor": patch
---

Handle `getTranslations` properly
37 changes: 37 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,41 @@ describe("Test parseSource", () => {
};
expect(result).toEqual(expected);
});

test("should parse source", async () => {
const source = `
"use client";
import { useTranslations } from "next-intl";
export const MyComponent = () => {
const t = await getTranslations({ namespace: "ProductListing", locale });
const foobar = t("foobar");
return (
<div>
<h1>{t("title")}</h1>
<div>
{t("results", {
total: products.total,
})}
</div>
</div>
)
`;

const result = await parseSource("MyComponent.tsx", source);
const expected = {
foobar: "",
title: "",
results: "",
};
expect(result).toEqual(expected);
});




});
21 changes: 21 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ export async function parseSource(filename: string, source: string) {
}
}

// Check for variable declarations that initialize with getTranslations
if (ts.isVariableDeclaration(node) && node.initializer) {
let callExpr = node.initializer;

// Check if the initializer is an AwaitExpression
if (ts.isAwaitExpression(callExpr) && callExpr.expression) {
callExpr = callExpr.expression;
}

if (
ts.isCallExpression(callExpr) &&
ts.isIdentifier(callExpr.expression) &&
callExpr.expression.text === "getTranslations"
) {
if (node.name && ts.isIdentifier(node.name)) {
currentScope.add(node.name.text);
}
}
}


// Check for calls using the translation function variable
if (
ts.isCallExpression(node) &&
Expand Down

0 comments on commit 2dbb51e

Please sign in to comment.