Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7c06b66974 | ||
|
b2ae17697d | ||
|
5259da8fe3 | ||
|
2d73f056ff | ||
|
fde5245731 | ||
|
4ee7258a31 | ||
|
a6c7af0630 | ||
|
0949bf765b | ||
|
d416c6b9fd | ||
|
4e351405fb | ||
|
39a335638b | ||
|
3b9561916b | ||
|
5cef383f3b |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -10,8 +10,8 @@ npm-debug.log*
|
|||||||
dist
|
dist
|
||||||
|
|
||||||
# Extracted strings
|
# Extracted strings
|
||||||
template.json
|
strings.json
|
||||||
template.pot
|
strings.pot
|
||||||
|
|
||||||
# Dependency directory
|
# Dependency directory
|
||||||
node_modules
|
node_modules
|
||||||
|
28
package.json
28
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@biesbjerg/ngx-translate-extract",
|
"name": "@biesbjerg/ngx-translate-extract",
|
||||||
"version": "2.2.2",
|
"version": "2.3.2",
|
||||||
"description": "Extract strings from projects using ngx-translate",
|
"description": "Extract strings from projects using ngx-translate",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
@@ -47,28 +47,28 @@
|
|||||||
},
|
},
|
||||||
"config": {},
|
"config": {},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chai": "3.4.35",
|
"@types/chai": "4.0.1",
|
||||||
"@types/glob": "5.0.30",
|
"@types/glob": "5.0.30",
|
||||||
"@types/mocha": "2.2.40",
|
"@types/mocha": "2.2.41",
|
||||||
"@types/cheerio": "0.22.1",
|
"@types/cheerio": "0.22.1",
|
||||||
"@types/chalk": "0.4.31",
|
"@types/chalk": "0.4.31",
|
||||||
"@types/flat": "0.0.28",
|
"@types/flat": "0.0.28",
|
||||||
"@types/yargs": "6.6.0",
|
"@types/yargs": "8.0.0",
|
||||||
"@types/mkdirp": "0.3.29",
|
"@types/mkdirp": "0.3.29",
|
||||||
"chai": "3.5.0",
|
"chai": "4.0.2",
|
||||||
"mocha": "3.2.0",
|
"mocha": "3.4.2",
|
||||||
"ts-node": "3.0.2",
|
"ts-node": "3.1.0",
|
||||||
"tslint": "5.0.0",
|
"tslint": "5.4.3",
|
||||||
"tslint-eslint-rules": "4.0.0",
|
"tslint-eslint-rules": "4.1.1",
|
||||||
"typescript": "2.2.2"
|
"typescript": "2.4.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "1.1.3",
|
"chalk": "2.0.1",
|
||||||
"yargs": "7.0.2",
|
"yargs": "8.0.2",
|
||||||
"cheerio": "0.22.0",
|
"cheerio": "1.0.0-rc.2",
|
||||||
"fs": "0.0.1-security",
|
"fs": "0.0.1-security",
|
||||||
"gettext-parser": "1.2.2",
|
"gettext-parser": "1.2.2",
|
||||||
"glob": "7.1.1",
|
"glob": "7.1.2",
|
||||||
"path": "0.12.7",
|
"path": "0.12.7",
|
||||||
"mkdirp": "0.5.1",
|
"mkdirp": "0.5.1",
|
||||||
"flat": "2.0.1"
|
"flat": "2.0.1"
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
import { CompilerInterface } from './compiler.interface';
|
import { CompilerInterface } from './compiler.interface';
|
||||||
import { TranslationCollection } from '../utils/translation.collection';
|
import { TranslationCollection } from '../utils/translation.collection';
|
||||||
|
|
||||||
|
import * as flat from 'flat';
|
||||||
|
|
||||||
export class JsonCompiler implements CompilerInterface {
|
export class JsonCompiler implements CompilerInterface {
|
||||||
|
|
||||||
public indentation: string = '\t';
|
public indentation: string = '\t';
|
||||||
@@ -18,7 +20,15 @@ export class JsonCompiler implements CompilerInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public parse(contents: string): TranslationCollection {
|
public parse(contents: string): TranslationCollection {
|
||||||
return new TranslationCollection(JSON.parse(contents));
|
let values: any = JSON.parse(contents);
|
||||||
|
if (this._isNamespacedJsonFormat(values)) {
|
||||||
|
values = flat.flatten(values);
|
||||||
|
}
|
||||||
|
return new TranslationCollection(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected _isNamespacedJsonFormat(values: any): boolean {
|
||||||
|
return Object.keys(values).some(key => typeof values[key] === 'object');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -35,11 +35,7 @@ export abstract class AbstractAstParser {
|
|||||||
/**
|
/**
|
||||||
* Find all child nodes of a kind
|
* Find all child nodes of a kind
|
||||||
*/
|
*/
|
||||||
protected _findNodes(node: ts.Node, kind: ts.SyntaxKind, onlyOne: boolean = false): ts.Node[] {
|
protected _findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] {
|
||||||
if (node.kind === kind && onlyOne) {
|
|
||||||
return [node];
|
|
||||||
}
|
|
||||||
|
|
||||||
const childrenNodes: ts.Node[] = node.getChildren(this._sourceFile);
|
const childrenNodes: ts.Node[] = node.getChildren(this._sourceFile);
|
||||||
const initialValue: ts.Node[] = node.kind === kind ? [node] : [];
|
const initialValue: ts.Node[] = node.kind === kind ? [node] : [];
|
||||||
|
|
||||||
|
@@ -18,7 +18,7 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac
|
|||||||
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
|
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
|
||||||
let matches: RegExpExecArray;
|
let matches: RegExpExecArray;
|
||||||
while (matches = regExp.exec(template)) {
|
while (matches = regExp.exec(template)) {
|
||||||
collection = collection.add(matches[2].replace('\\\'', '\''));
|
collection = collection.add(matches[2].split('\\\'').join('\''));
|
||||||
}
|
}
|
||||||
|
|
||||||
return collection;
|
return collection;
|
||||||
|
@@ -8,26 +8,29 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
|||||||
|
|
||||||
protected _sourceFile: ts.SourceFile;
|
protected _sourceFile: ts.SourceFile;
|
||||||
|
|
||||||
protected _instancePropertyName: any;
|
|
||||||
protected _serviceClassName: string = 'TranslateService';
|
|
||||||
protected _serviceMethodNames: string[] = ['get', 'instant'];
|
|
||||||
|
|
||||||
public extract(contents: string, path?: string): TranslationCollection {
|
public extract(contents: string, path?: string): TranslationCollection {
|
||||||
let collection: TranslationCollection = new TranslationCollection();
|
let collection: TranslationCollection = new TranslationCollection();
|
||||||
|
|
||||||
this._sourceFile = this._createSourceFile(path, contents);
|
this._sourceFile = this._createSourceFile(path, contents);
|
||||||
|
const classNodes = this._findClassNodes(this._sourceFile);
|
||||||
this._instancePropertyName = this._getInstancePropertyName();
|
classNodes.forEach(classNode => {
|
||||||
if (!this._instancePropertyName) {
|
const constructorNode = this._findConstructorNode(classNode);
|
||||||
return collection;
|
if (!constructorNode) {
|
||||||
}
|
return;
|
||||||
|
|
||||||
const callNodes = this._findCallNodes();
|
|
||||||
callNodes.forEach(callNode => {
|
|
||||||
const keys: string[] = this._getCallArgStrings(callNode);
|
|
||||||
if (keys && keys.length) {
|
|
||||||
collection = collection.addKeys(keys);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const propertyName: string = this._findTranslateServicePropertyName(constructorNode);
|
||||||
|
if (!propertyName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const callNodes = this._findCallNodes(classNode, propertyName);
|
||||||
|
callNodes.forEach(callNode => {
|
||||||
|
const keys: string[] = this._getCallArgStrings(callNode);
|
||||||
|
if (keys && keys.length) {
|
||||||
|
collection = collection.addKeys(keys);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return collection;
|
return collection;
|
||||||
@@ -35,10 +38,9 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Detect what the TranslateService instance property
|
* Detect what the TranslateService instance property
|
||||||
* is called by inspecting constructor params
|
* is called by inspecting constructor arguments
|
||||||
*/
|
*/
|
||||||
protected _getInstancePropertyName(): string {
|
protected _findTranslateServicePropertyName(constructorNode: ts.ConstructorDeclaration): string {
|
||||||
const constructorNode = this._findConstructorNode();
|
|
||||||
if (!constructorNode) {
|
if (!constructorNode) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -49,13 +51,18 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parameter has no type
|
||||||
|
if (!parameter.type) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure className is of the correct type
|
// Make sure className is of the correct type
|
||||||
const parameterType: ts.Identifier = (parameter.type as ts.TypeReferenceNode).typeName as ts.Identifier;
|
const parameterType: ts.Identifier = (parameter.type as ts.TypeReferenceNode).typeName as ts.Identifier;
|
||||||
if (!parameterType) {
|
if (!parameterType) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const className: string = parameterType.text;
|
const className: string = parameterType.text;
|
||||||
if (className !== this._serviceClassName) {
|
if (className !== 'TranslateService') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,23 +75,26 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find first constructor
|
* Find class nodes
|
||||||
*/
|
*/
|
||||||
protected _findConstructorNode(): ts.ConstructorDeclaration {
|
protected _findClassNodes(node: ts.Node): ts.ClassDeclaration[] {
|
||||||
const constructors = this._findNodes(this._sourceFile, ts.SyntaxKind.Constructor, true) as ts.ConstructorDeclaration[];
|
return this._findNodes(node, ts.SyntaxKind.ClassDeclaration) as ts.ClassDeclaration[];
|
||||||
if (constructors.length) {
|
}
|
||||||
return constructors[0];
|
|
||||||
|
/**
|
||||||
|
* Find constructor
|
||||||
|
*/
|
||||||
|
protected _findConstructorNode(node: ts.ClassDeclaration): ts.ConstructorDeclaration {
|
||||||
|
const constructorNodes = this._findNodes(node, ts.SyntaxKind.Constructor) as ts.ConstructorDeclaration[];
|
||||||
|
if (constructorNodes) {
|
||||||
|
return constructorNodes[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all calls to TranslateService methods
|
* Find all calls to TranslateService methods
|
||||||
*/
|
*/
|
||||||
protected _findCallNodes(node?: ts.Node): ts.CallExpression[] {
|
protected _findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] {
|
||||||
if (!node) {
|
|
||||||
node = this._sourceFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||||
callNodes = callNodes
|
callNodes = callNodes
|
||||||
.filter(callNode => {
|
.filter(callNode => {
|
||||||
@@ -100,7 +110,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
|||||||
if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== ts.SyntaxKind.ThisKeyword) {
|
if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== ts.SyntaxKind.ThisKeyword) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (propAccess.name.text !== this._instancePropertyName) {
|
if (propAccess.name.text !== propertyIdentifier) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +118,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
|||||||
if (!methodAccess || methodAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
|
if (!methodAccess || methodAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!methodAccess.name || this._serviceMethodNames.indexOf(methodAccess.name.text) === -1) {
|
if (!methodAccess.name || (methodAccess.name.text !== 'get' && methodAccess.name.text !== 'instant' && methodAccess.name.text !== 'stream')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -36,6 +36,12 @@ describe('PipeParser', () => {
|
|||||||
expect(keys).to.deep.equal([`World's largest potato`]);
|
expect(keys).to.deep.equal([`World's largest potato`]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should extract strings with multiple escaped quotes', () => {
|
||||||
|
const contents = `{{ 'C\\'est ok. C\\'est ok' | translate }}`;
|
||||||
|
const keys = parser.extract(contents, templateFilename).keys();
|
||||||
|
expect(keys).to.deep.equal([`C'est ok. C'est ok`]);
|
||||||
|
});
|
||||||
|
|
||||||
it('should extract interpolated strings using translate pipe in attributes', () => {
|
it('should extract interpolated strings using translate pipe in attributes', () => {
|
||||||
const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
|
const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
|
||||||
const keys = parser.extract(contents, templateFilename).keys();
|
const keys = parser.extract(contents, templateFilename).keys();
|
||||||
|
@@ -60,6 +60,19 @@ describe('ServiceParser', () => {
|
|||||||
expect(keys).to.deep.equal(['Hello World']);
|
expect(keys).to.deep.equal(['Hello World']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should extract strings in TranslateService\'s stream() method', () => {
|
||||||
|
const contents = `
|
||||||
|
@Component({ })
|
||||||
|
export class AppComponent {
|
||||||
|
public constructor(protected _translateService: TranslateService) { }
|
||||||
|
public test() {
|
||||||
|
this._translateService.stream('Hello World');
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const keys = parser.extract(contents, componentFilename).keys();
|
||||||
|
expect(keys).to.deep.equal(['Hello World']);
|
||||||
|
});
|
||||||
|
|
||||||
it('should extract array of strings in TranslateService\'s get() method', () => {
|
it('should extract array of strings in TranslateService\'s get() method', () => {
|
||||||
const contents = `
|
const contents = `
|
||||||
@Component({ })
|
@Component({ })
|
||||||
@@ -86,7 +99,20 @@ describe('ServiceParser', () => {
|
|||||||
expect(key).to.deep.equal(['Hello', 'World']);
|
expect(key).to.deep.equal(['Hello', 'World']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not extract strings in get()/instant() methods of other services', () => {
|
it('should extract array of strings in TranslateService\'s stream() method', () => {
|
||||||
|
const contents = `
|
||||||
|
@Component({ })
|
||||||
|
export class AppComponent {
|
||||||
|
public constructor(protected _translateService: TranslateService) { }
|
||||||
|
public test() {
|
||||||
|
this._translateService.stream(['Hello', 'World']);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const key = parser.extract(contents, componentFilename).keys();
|
||||||
|
expect(key).to.deep.equal(['Hello', 'World']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not extract strings in get()/instant()/stream() methods of other services', () => {
|
||||||
const contents = `
|
const contents = `
|
||||||
@Component({ })
|
@Component({ })
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
@@ -97,6 +123,7 @@ describe('ServiceParser', () => {
|
|||||||
public test() {
|
public test() {
|
||||||
this._otherService.get('Hello World');
|
this._otherService.get('Hello World');
|
||||||
this._otherService.instant('Hi there');
|
this._otherService.instant('Hi there');
|
||||||
|
this._otherService.stream('Hi there');
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
const keys = parser.extract(contents, componentFilename).keys();
|
const keys = parser.extract(contents, componentFilename).keys();
|
||||||
@@ -151,4 +178,51 @@ describe('ServiceParser', () => {
|
|||||||
expect(keys).to.deep.equal(['You are expected at {{time}}']);
|
expect(keys).to.deep.equal(['You are expected at {{time}}']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not crash when constructor parameter has no type', () => {
|
||||||
|
const contents = `
|
||||||
|
@Component({ })
|
||||||
|
export class AppComponent {
|
||||||
|
public constructor(protected _translateService) { }
|
||||||
|
public test() {
|
||||||
|
this._translateService.instant('Hello World');
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const keys = parser.extract(contents, componentFilename).keys();
|
||||||
|
expect(keys).to.deep.equal([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should extract strings from all classes in the file', () => {
|
||||||
|
const contents = `
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
|
export class Stuff {
|
||||||
|
thing: string;
|
||||||
|
translate: any;
|
||||||
|
constructor(thing: string) {
|
||||||
|
this.translate.get('Not me');
|
||||||
|
this.thing = thing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Injectable()
|
||||||
|
export class MyComponent {
|
||||||
|
constructor(public translate: TranslateService) {
|
||||||
|
this.translate.instant("Extract me!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class OtherClass {
|
||||||
|
constructor(thing: string, _translate: TranslateService) {
|
||||||
|
this._translate.get("Do not extract me");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Injectable()
|
||||||
|
export class AuthService {
|
||||||
|
constructor(public translate: TranslateService) {
|
||||||
|
this.translate.instant("Hello!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const keys = parser.extract(contents, componentFilename).keys();
|
||||||
|
expect(keys).to.deep.equal(['Extract me!', 'Hello!']);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user