Add option to merge extracted strings with existing translations (Thanks to @ocombe)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
import * as $ from 'cheerio';
|
||||
|
||||
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): StringCollection {
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
}
|
||||
@@ -14,8 +14,8 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
template = this._normalizeTemplateAttributes(template);
|
||||
$(template)
|
||||
@@ -26,7 +26,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
const attr = $element.attr('translate') || $element.attr('ng2-translate');
|
||||
|
||||
if (attr) {
|
||||
collection.add(attr);
|
||||
collection = collection.add(attr);
|
||||
} else {
|
||||
$element
|
||||
.contents()
|
||||
@@ -34,7 +34,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
.filter(node => node.type === 'text')
|
||||
.map(node => node.nodeValue.trim())
|
||||
.filter(text => text.length > 0)
|
||||
.forEach(text => collection.add(text));
|
||||
.forEach(text => collection = collection.add(text));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export interface ParserInterface {
|
||||
|
||||
extract(contents: string, path?: string): StringCollection;
|
||||
extract(contents: string, path?: string): TranslationCollection;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): StringCollection {
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
}
|
||||
@@ -12,14 +12,14 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const regExp = new RegExp(/(['"`])([^\1\r\n]*)\1\s*\|\s*translate(:.*?)?/, 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(template)) {
|
||||
collection.add(matches[2]);
|
||||
collection = collection.add(matches[2]);
|
||||
}
|
||||
|
||||
return collection;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export class ServiceParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const translateServiceVar = this._extractTranslateServiceVar(contents);
|
||||
if (!translateServiceVar) {
|
||||
@@ -17,10 +17,9 @@ export class ServiceParser implements ParserInterface {
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
if (this._stringContainsArray(matches[1])) {
|
||||
const matchCollection = StringCollection.fromArray(this._stringToArray(matches[1]));
|
||||
collection.merge(matchCollection);
|
||||
collection = collection.addKeys(this._stringToArray(matches[1]));
|
||||
} else {
|
||||
collection.add(matches[3]);
|
||||
collection = collection.add(matches[3]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user