Skip to content

Commit

Permalink
fix(i18n): update extractMessages function for better escaping of cha…
Browse files Browse the repository at this point in the history
…racters (#1079)

This PR fix a bug when a translation message has two single quote like "message": "hello 'world'",
the extractMessages function was escaping the message correcly.
  • Loading branch information
gauthier-th authored Nov 12, 2024
1 parent cb94ad5 commit a2d2fd3
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/i18n/extractMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ async function extractMessages(
try {
const formattedMessages = messages
.trim()
.replace(/^\s*(['"])?([a-zA-Z0-9_-]+)(['"])?:/gm, '"$2":')
.replace(
/'.*'/g,
(match) =>
`"${match
.match(/'(.*)'/)?.[1]
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')}"`
)
.replace(/^\s*(['"])?([a-zA-Z0-9_-]+)(['"])?:[\s\n]*/gm, '"$2":')
.replace(/^"[a-zA-Z0-9_-]+":'.*',?$/gm, (match) => {
const parts = /^("[a-zA-Z0-9_-]+":)'(.*)',?$/.exec(match);
if (!parts) return match;
return `${parts[1]}"${parts[2]
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')}",`;
})
.replace(/,$/, '');
const messagesJson = JSON.parse(`{${formattedMessages}}`);
return { namespace: namespace.trim(), messages: messagesJson };
Expand Down

0 comments on commit a2d2fd3

Please sign in to comment.