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
This commit is contained in:
Pete Bacon Darwin 2020-08-19 12:25:10 +01:00 committed by Misko Hevery
parent 23f855b300
commit bdba1a062d
1 changed files with 7 additions and 2 deletions

View File

@ -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};
}
}