Skip to content
Merged
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
6 changes: 4 additions & 2 deletions example/locales/de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"Hello world!": "Hallo Welt!",
"Lazy term": "Fauler Begriff",
"This text contains {0} \\{} curly braces and \\\\{\\{}}\\{0}.": "Dieser Text enthält {0} \\{} geschweifte Klammern und \\\\{\\{}}\\{0}.",
"Untranslated text": ""
"Untranslated text": "",
"Unknown Error": "",
"An unexpected error occurred while processing your request.": "",
"This text contains {0} \\{} curly braces and \\\\\\{\\{}}\\{0}.": "Dieser Text enthält {0} \\{} geschweifte Klammern und \\\\\\{\\{}}\\{0}."
}
6 changes: 4 additions & 2 deletions example/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"Hello world!": "Hello world!",
"Lazy term": "Lazy term",
"This text contains {0} \\{} curly braces and \\\\{\\{}}\\{0}.": "This text contains {0} \\{} curly braces and \\\\{\\{}}\\{0}.",
"Untranslated text": "Untranslated text"
"Untranslated text": "Untranslated text",
"Unknown Error": "Unknown Error",
"An unexpected error occurred while processing your request.": "An unexpected error occurred while processing your request.",
"This text contains {0} \\{} curly braces and \\\\\\{\\{}}\\{0}.": "This text contains {0} \\{} curly braces and \\\\\\{\\{}}\\{0}."
}
5 changes: 4 additions & 1 deletion lib/common/escapeKey.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export function escapeKey(key: string): string {
return key.replaceAll(/{/g, "\\{");
// Escape all backslashes first
key = key.replace(/\\/g, "\\\\");
// Then escape curly braces
return key.replace(/[{]/g, "\\{");
}
2 changes: 1 addition & 1 deletion lib/common/unescapeKey.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function unescapeKey(key: string): string {
return key.replaceAll(/\\{/g, "{");
return key.replaceAll(/\\{/g, "{").replaceAll(/\\\\/g, "\\");
}
23 changes: 11 additions & 12 deletions lib/extract/TypeScriptSourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class TypeScriptSourceFile {
this.processNode(this.getSourceFile(), extractions);
}

private processNode(node: ts.Node, extractions = this.extractions) {
private processNode(node: ts.Node, extractions: Extractions) {
if (TypeScriptSourceFile.isTemplateString(node)) {
this.processTemplateString(node, this.getSourceFile(), extractions);
}
Expand All @@ -84,18 +84,17 @@ export class TypeScriptSourceFile {
sourceFile: ts.SourceFile,
extractions: Extractions,
) {
let templateString: string;
const tpl = node.template;
if (ts.isNoSubstitutionTemplateLiteral(tpl)) {
templateString = escapeKey(tpl.text);
} else if (ts.isTemplateExpression(tpl)) {
let templateParts = escapeKey(tpl.head.text);
tpl.templateSpans.forEach((span, index) => {
templateParts += "{" + index + "}" + escapeKey(span.literal.text);
let templateString = "";
const template = node.template;
if (ts.isNoSubstitutionTemplateLiteral(template)) {
templateString = escapeKey(template.text);
} else if (ts.isTemplateExpression(template)) {
let combinedTemplateParts = escapeKey(template.head.text);
template.templateSpans.forEach((span, index) => {
combinedTemplateParts += "{" + index + "}" +
escapeKey(span.literal.text);
});
templateString = templateParts;
} else {
templateString = "";
templateString = combinedTemplateParts;
}
const { line } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
extractions.addExtraction(templateString, this.fileName, line + 1);
Expand Down