From bdba1a062dced20ea0c76d78ee3d0d7d11668877 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 19 Aug 2020 12:25:10 +0100 Subject: [PATCH] refactor(localize): include text in original location of extracted messages (#38536) When extracting messages, source-mapping information is used to find the original location of the message being extracted. This commit will now include the text from the original source in the message location so that it can be serialized into the translation file. PR Close #38536 --- packages/localize/src/tools/src/extract/extraction.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/localize/src/tools/src/extract/extraction.ts b/packages/localize/src/tools/src/extract/extraction.ts index 242e88df9e..61331064ee 100644 --- a/packages/localize/src/tools/src/extract/extraction.ts +++ b/packages/localize/src/tools/src/extract/extraction.ts @@ -5,7 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import {AbsoluteFsPath, FileSystem, PathSegment} from '@angular/compiler-cli/src/ngtsc/file_system'; +import {AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system'; import {Logger} from '@angular/compiler-cli/src/ngtsc/logging'; import {SourceFile, SourceFileLoader} from '@angular/compiler-cli/src/ngtsc/sourcemaps'; import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize'; @@ -103,6 +103,11 @@ export class MessageExtractor { const end = (originalEnd !== null && originalEnd.file === originalStart.file) ? {line: originalEnd.line, column: originalEnd.column} : start; - return {file: originalStart.file, start, end}; + const originalSourceFile = + sourceFile.sources.find(sf => sf?.sourcePath === originalStart.file)!; + const startPos = originalSourceFile.startOfLinePositions[start.line] + start.column; + const endPos = originalSourceFile.startOfLinePositions[end.line] + end.column; + const text = originalSourceFile.contents.substring(startPos, endPos); + return {file: originalStart.file, start, end, text}; } }