replace DirectiveParser with new version that uses Angular compiler
This commit is contained in:
parent
7d1bcd2a80
commit
6e161c83f8
@ -1,85 +0,0 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
|
||||
|
||||
import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler';
|
||||
|
||||
export class DirectiveAstParser implements ParserInterface {
|
||||
|
||||
public extract(template: string, path: string): TranslationCollection {
|
||||
if (path && isPathAngularComponent(path)) {
|
||||
template = extractComponentInlineTemplate(template);
|
||||
}
|
||||
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
throw new Error('noåpe');
|
||||
const nodes: TmplAstNode[] = this.parseTemplate(template, path);
|
||||
this.getTranslatableElements(nodes).forEach(element => {
|
||||
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
|
||||
collection = collection.add(key);
|
||||
});
|
||||
|
||||
console.log(collection);
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
protected getTranslatableElements(nodes: TmplAstNode[]): TmplAstElement[] {
|
||||
return nodes
|
||||
.filter(element => this.isElement(element))
|
||||
.reduce((result: TmplAstElement[], element: TmplAstElement) => {
|
||||
return result.concat(this.findChildrenElements(element));
|
||||
}, [])
|
||||
.filter(element => this.isTranslatable(element));
|
||||
}
|
||||
|
||||
protected findChildrenElements(node: TmplAstNode): TmplAstElement[] {
|
||||
if (!this.isElement(node)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// If element has translate attribute all its contents is translatable
|
||||
// so we don't need to traverse any deeper
|
||||
if (this.isTranslatable(node)) {
|
||||
return [node];
|
||||
}
|
||||
|
||||
return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => {
|
||||
if (this.isElement(childNode)) {
|
||||
const children = this.findChildrenElements(childNode);
|
||||
return result.concat(children);
|
||||
}
|
||||
return result;
|
||||
}, [node]);
|
||||
}
|
||||
|
||||
protected parseTemplate(template: string, path: string): TmplAstNode[] {
|
||||
return parseTemplate(template, path).nodes;
|
||||
}
|
||||
|
||||
protected isElement(node: any): node is TmplAstElement {
|
||||
return node
|
||||
&& node.attributes !== undefined
|
||||
&& node.children !== undefined;
|
||||
}
|
||||
|
||||
protected isTranslatable(node: TmplAstNode): boolean {
|
||||
if (this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getElementTranslateAttrValue(element: TmplAstElement): string {
|
||||
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
|
||||
return attr && attr.value || '';
|
||||
}
|
||||
|
||||
protected getElementContents(element: TmplAstElement): string {
|
||||
const contents = element.sourceSpan.start.file.content;
|
||||
const start = element.startSourceSpan.end.offset;
|
||||
const end = element.endSourceSpan.start.offset;
|
||||
return contents.substring(start, end).trim();
|
||||
}
|
||||
|
||||
}
|
@ -12,10 +12,10 @@ export class DirectiveParser implements ParserInterface {
|
||||
}
|
||||
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const nodes: TmplAstNode[] = this.parseTemplate(template, path);
|
||||
this.getTranslatableElements(nodes).forEach(element => {
|
||||
const translateAttr = this.getTranslateAttribute(element);
|
||||
const key = translateAttr.value || this.getContents(element);
|
||||
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
|
||||
collection = collection.add(key);
|
||||
});
|
||||
|
||||
@ -28,26 +28,26 @@ export class DirectiveParser implements ParserInterface {
|
||||
.reduce((result: TmplAstElement[], element: TmplAstElement) => {
|
||||
return result.concat(this.findChildrenElements(element));
|
||||
}, [])
|
||||
.filter(element => this.hasTranslateAttribute(element));
|
||||
.filter(element => this.isTranslatable(element));
|
||||
}
|
||||
|
||||
protected findChildrenElements(node: TmplAstNode): TmplAstElement[] {
|
||||
// Safe guard, since only elements have children
|
||||
if (!this.isElement(node)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// If element has translate attribute it means all of its contents
|
||||
// is translatable, so we don't need to go any deeper
|
||||
if (this.hasTranslateAttribute(node)) {
|
||||
// If element has translate attribute all its contents is translatable
|
||||
// so we don't need to traverse any deeper
|
||||
if (this.isTranslatable(node)) {
|
||||
return [node];
|
||||
}
|
||||
|
||||
return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => {
|
||||
if (!this.isElement(childNode)) {
|
||||
return result;
|
||||
if (this.isElement(childNode)) {
|
||||
const children = this.findChildrenElements(childNode);
|
||||
return result.concat(children);
|
||||
}
|
||||
return result.concat(this.findChildrenElements(childNode));
|
||||
return result;
|
||||
}, [node]);
|
||||
}
|
||||
|
||||
@ -55,22 +55,29 @@ export class DirectiveParser implements ParserInterface {
|
||||
return parseTemplate(template, path).nodes;
|
||||
}
|
||||
|
||||
protected isElement(node: TmplAstNode): node is TmplAstElement {
|
||||
return node instanceof TmplAstElement;
|
||||
protected isElement(node: any): node is TmplAstElement {
|
||||
return node
|
||||
&& node.attributes !== undefined
|
||||
&& node.children !== undefined;
|
||||
}
|
||||
|
||||
protected getContents(element: TmplAstElement): string {
|
||||
protected isTranslatable(node: TmplAstNode): boolean {
|
||||
if (this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getElementTranslateAttrValue(element: TmplAstElement): string {
|
||||
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
|
||||
return attr && attr.value || '';
|
||||
}
|
||||
|
||||
protected getElementContents(element: TmplAstElement): string {
|
||||
const contents = element.sourceSpan.start.file.content;
|
||||
const start = element.startSourceSpan.end.offset;
|
||||
const end = element.endSourceSpan.start.offset;
|
||||
return element.sourceSpan.start.file.content.substring(start, end).trim();
|
||||
}
|
||||
|
||||
protected hasTranslateAttribute(element: TmplAstElement): boolean {
|
||||
return !!this.getTranslateAttribute(element);
|
||||
}
|
||||
|
||||
protected getTranslateAttribute(element: TmplAstElement): TmplAstTextAttribute {
|
||||
return element.attributes.find(attribute => attribute.name === 'translate');
|
||||
return contents.substring(start, end).trim();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user