Skip to content

Commit de1a28d

Browse files
authored
fix: detect getTranslate in server components (#152)
1 parent bcc2461 commit de1a28d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/extractor/parserReact/tokenMergers/useTranslateMerger.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export const useTranslateMerger = {
1515

1616
switch (state) {
1717
case S.Idle:
18-
if (type === 'function.call' && token === 'useTranslate') {
18+
if (
19+
type === 'function.call' &&
20+
(token === 'useTranslate' || token === 'getTranslate')
21+
) {
1922
return S.ExpectBracket;
2023
}
2124
break;

test/unit/extractor/react/useTranslate.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function extractReactKeys(
2020
describe.each(['js', 'ts', 'jsx', 'tsx'])('useTranslate (.%s)', (ext) => {
2121
const FILE_NAME = `test.${ext}`;
2222

23-
it('extracts from the t call with signature t(string))', async () => {
23+
it('extracts from the t call with signature t(string) and useTranslate', async () => {
2424
const expected = [{ keyName: 'key1', line: 5 }];
2525

2626
const code = `
@@ -36,6 +36,22 @@ describe.each(['js', 'ts', 'jsx', 'tsx'])('useTranslate (.%s)', (ext) => {
3636
expect(extracted.keys).toEqual(expected);
3737
});
3838

39+
it('extracts from the t call with signature t(string) and getTranslate', async () => {
40+
const expected = [{ keyName: 'key1', line: 5 }];
41+
42+
const code = `
43+
import '@tolgee/react'
44+
async function Test () {
45+
const t = await getTranslate()
46+
t('key1')
47+
}
48+
`;
49+
50+
const extracted = await extractReactKeys(code, FILE_NAME);
51+
expect(extracted.warnings).toEqual([]);
52+
expect(extracted.keys).toEqual(expected);
53+
});
54+
3955
it('extracts from the t call with signature t(string, string)', async () => {
4056
const expected = [
4157
{ keyName: 'key1', defaultValue: 'default value', line: 5 },

0 commit comments

Comments
 (0)