Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/extract/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import { Token, tokenize, TokenKind } from "./tokenizer.js";
import { KeywordMapping } from "../typeDefs.js";
import { assertIsDefined } from "../utilities.js";

// Interpolation RegExp - matches Vue.js %{} syntax
const INTERPOLATION_RE = /%\{((?:.|\n)+?)\}/g;

function hasInterpolation(message: string): boolean {
return INTERPOLATION_RE.test(message);
}

type BaseMsg = { message: string; messagePlural?: string; context?: string };

export type MsgInfo = BaseMsg & {
lineNumber: number;
hasInterpolation: boolean;
};

type MsgInfoWithCharIdx = BaseMsg & { idx: number };
Expand Down Expand Up @@ -138,6 +146,8 @@ export function parseSrc(src: string, options?: { mapping?: KeywordMapping; over
return {
...i,
lineNumber: src.substring(0, info.idx).split("\n").length,
hasInterpolation:
hasInterpolation(info.message) || (info.messagePlural ? hasInterpolation(info.messagePlural) : false),
};
});
}
Expand All @@ -152,6 +162,7 @@ export function makePO(fileName: string, msgs: MsgInfo[]): PO {
item.msgid_plural = msg.messagePlural;
item.msgctxt = msg.context;
item.references = [`${fileName}:${msg.lineNumber}`];
item.flags["vue-format"] = msg.hasInterpolation;
po.items.push(item);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ describe("parser", () => {
{
message: "Welcome, %{ name }",
lineNumber: 6,
hasInterpolation: true,
},
{
message: "asdf",
lineNumber: 10,
hasInterpolation: false,
},
{
lineNumber: 19,
message: "%{count} book",
messagePlural: "%{count} books",
hasInterpolation: true,
},
]);

Expand All @@ -47,6 +50,7 @@ describe("parser", () => {
msgstr \"\"

#: testFile:6
#, vue-format
msgid \"Welcome, %{ name }\"
msgstr \"\"

Expand All @@ -55,6 +59,7 @@ msgid \"asdf\"
msgstr \"\"

#: testFile:19
#, vue-format
msgid \"%{count} book\"
msgid_plural \"%{count} books\"
msgstr[0] \"\"
Expand All @@ -73,6 +78,7 @@ Line breaks\`)`),
message: `Test
With
Line breaks`,
hasInterpolation: false,
},
]);
});
Expand All @@ -84,6 +90,7 @@ Line breaks`,
{
message: unicodeTestPage,
lineNumber: 1,
hasInterpolation: false,
},
]);
});
Expand All @@ -93,20 +100,23 @@ Line breaks`,
{
message: `t'\`e"st`,
lineNumber: 1,
hasInterpolation: false,
},
]);

expect(parseSrc(`$gettext('t\\'\`e"st')`)).toEqual([
{
message: `t\'\`e"st`,
lineNumber: 1,
hasInterpolation: false,
},
]);

expect(parseSrc("$gettext(`t'\\`est`)")).toEqual([
{
message: "t'`est",
lineNumber: 1,
hasInterpolation: false,
},
]);

Expand All @@ -115,6 +125,7 @@ Line breaks`,
message: "t'`e\"st",
messagePlural: `t'\`e"st`,
lineNumber: 1,
hasInterpolation: false,
},
]);
});
Expand All @@ -124,6 +135,7 @@ Line breaks`,
{
message: `\\`,
lineNumber: 1,
hasInterpolation: false,
},
]);
});
Expand All @@ -134,6 +146,7 @@ Line breaks`,
message: `te(st)(()`,
messagePlural: "test)(()",
lineNumber: 1,
hasInterpolation: false,
},
]);
});
Expand All @@ -143,6 +156,7 @@ Line breaks`,
{
message: `test`,
lineNumber: 1,
hasInterpolation: false,
},
]);
});
Expand Down Expand Up @@ -170,6 +184,7 @@ export default {
{
message: `%{fullName} wants to say hello`,
lineNumber: 15,
hasInterpolation: true,
},
]);
});
Expand Down Expand Up @@ -198,6 +213,7 @@ export default {
{
message: `Hello there`,
lineNumber: 16,
hasInterpolation: false,
},
]);
});
Expand Down