Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0949bf765b | ||
|
d416c6b9fd | ||
|
4e351405fb | ||
|
39a335638b | ||
|
3b9561916b | ||
|
5cef383f3b | ||
|
677d2a35ca | ||
|
262a89206d | ||
|
bcb4a9c069 | ||
|
5ad1fe6a18 | ||
|
bc5ce7e80d | ||
|
030ab145d6 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -10,8 +10,8 @@ npm-debug.log*
|
||||
dist
|
||||
|
||||
# Extracted strings
|
||||
template.json
|
||||
template.pot
|
||||
strings.json
|
||||
strings.pot
|
||||
|
||||
# Dependency directory
|
||||
node_modules
|
||||
|
@@ -82,6 +82,8 @@ Options:
|
||||
--output, -o Paths where you would like to save extracted
|
||||
strings. You can use path expansion, glob patterns
|
||||
and multiple paths [array] [required]
|
||||
--marker, -m Extract strings passed to a marker function
|
||||
[string] [default: false]
|
||||
--format, -f Output format
|
||||
[string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
|
||||
--format-indentation, --fi Output format indentation [string] [default: "\t"]
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@biesbjerg/ngx-translate-extract",
|
||||
"version": "2.2.0",
|
||||
"version": "2.3.0",
|
||||
"description": "Extract strings from projects using ngx-translate",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
@@ -58,8 +58,8 @@
|
||||
"chai": "3.5.0",
|
||||
"mocha": "3.2.0",
|
||||
"ts-node": "3.0.2",
|
||||
"tslint": "4.5.1",
|
||||
"tslint-eslint-rules": "3.5.1",
|
||||
"tslint": "5.0.0",
|
||||
"tslint-eslint-rules": "4.0.0",
|
||||
"typescript": "2.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
|
@@ -104,7 +104,7 @@ export class ExtractTask implements TaskInterface {
|
||||
|
||||
if (this._options.clean) {
|
||||
const collectionCount = processedCollection.count();
|
||||
processedCollection = processedCollection.intersect(processedCollection);
|
||||
processedCollection = processedCollection.intersect(collection);
|
||||
const removeCount = collectionCount - processedCollection.count();
|
||||
if (removeCount > 0) {
|
||||
this._out(chalk.dim('- removed %d obsolete strings'), removeCount);
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import { CompilerInterface } from './compiler.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
import * as flat from 'flat';
|
||||
|
||||
export class JsonCompiler implements CompilerInterface {
|
||||
|
||||
public indentation: string = '\t';
|
||||
@@ -18,7 +20,15 @@ export class JsonCompiler implements CompilerInterface {
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -48,7 +48,11 @@ export abstract class AbstractAstParser {
|
||||
}, initialValue);
|
||||
}
|
||||
|
||||
protected _printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0) {
|
||||
protected _syntaxKindToName(kind: ts.SyntaxKind): string {
|
||||
return ts.SyntaxKind[kind];
|
||||
}
|
||||
|
||||
protected _printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0): void {
|
||||
console.log(
|
||||
new Array(depth + 1).join('----'),
|
||||
`[${node.kind}]`,
|
||||
@@ -62,8 +66,4 @@ export abstract class AbstractAstParser {
|
||||
node.getChildren(sourceFile).forEach(childNode => this._printAllChildren(sourceFile, childNode, depth));
|
||||
}
|
||||
|
||||
protected _syntaxKindToName(kind: ts.SyntaxKind) {
|
||||
return ts.SyntaxKind[kind];
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -15,10 +15,10 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const regExp: RegExp = /(['"`])([^>\1\r\n]*?)\1\s*\|\s*translate/g;
|
||||
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
|
||||
let matches: RegExpExecArray;
|
||||
while (matches = regExp.exec(template)) {
|
||||
collection = collection.add(matches[2]);
|
||||
collection = collection.add(matches[2].replace('\\\'', '\''));
|
||||
}
|
||||
|
||||
return collection;
|
||||
|
@@ -8,26 +8,25 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
|
||||
protected _sourceFile: ts.SourceFile;
|
||||
|
||||
protected _instancePropertyName: any;
|
||||
protected _serviceClassName: string = 'TranslateService';
|
||||
protected _serviceMethodNames: string[] = ['get', 'instant'];
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._sourceFile = this._createSourceFile(path, contents);
|
||||
|
||||
this._instancePropertyName = this._getInstancePropertyName();
|
||||
if (!this._instancePropertyName) {
|
||||
return collection;
|
||||
}
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const callNodes = this._findCallNodes();
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
const constructorNodes: ts.ConstructorDeclaration[] = this._findConstructorNodes();
|
||||
constructorNodes.forEach(constructorNode => {
|
||||
const propertyName: string = this._getPropertyName(constructorNode);
|
||||
if (!propertyName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const callNodes = this._findCallNodes(this._sourceFile, propertyName);
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return collection;
|
||||
@@ -37,8 +36,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
* Detect what the TranslateService instance property
|
||||
* is called by inspecting constructor params
|
||||
*/
|
||||
protected _getInstancePropertyName(): string {
|
||||
const constructorNode = this._findConstructorNode();
|
||||
protected _getPropertyName(constructorNode: ts.ConstructorDeclaration): string {
|
||||
if (!constructorNode) {
|
||||
return null;
|
||||
}
|
||||
@@ -49,13 +47,18 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parameter has no type
|
||||
if (!parameter.type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure className is of the correct type
|
||||
const parameterType: ts.Identifier = (parameter.type as ts.TypeReferenceNode).typeName as ts.Identifier;
|
||||
if (!parameterType) {
|
||||
return false;
|
||||
}
|
||||
const className: string = parameterType.text;
|
||||
if (className !== this._serviceClassName) {
|
||||
if (className !== 'TranslateService') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -68,23 +71,19 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Find first constructor
|
||||
* Find constructor nodes
|
||||
*/
|
||||
protected _findConstructorNode(): ts.ConstructorDeclaration {
|
||||
protected _findConstructorNodes(): ts.ConstructorDeclaration[] {
|
||||
const constructors = this._findNodes(this._sourceFile, ts.SyntaxKind.Constructor, true) as ts.ConstructorDeclaration[];
|
||||
if (constructors.length) {
|
||||
return constructors[0];
|
||||
return constructors;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all calls to TranslateService methods
|
||||
*/
|
||||
protected _findCallNodes(node?: ts.Node): ts.CallExpression[] {
|
||||
if (!node) {
|
||||
node = this._sourceFile;
|
||||
}
|
||||
|
||||
protected _findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] {
|
||||
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
callNodes = callNodes
|
||||
.filter(callNode => {
|
||||
@@ -100,7 +99,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== ts.SyntaxKind.ThisKeyword) {
|
||||
return false;
|
||||
}
|
||||
if (propAccess.name.text !== this._instancePropertyName) {
|
||||
if (propAccess.name.text !== propertyIdentifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -108,7 +107,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
if (!methodAccess || methodAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
|
||||
return false;
|
||||
}
|
||||
if (!methodAccess.name || this._serviceMethodNames.indexOf(methodAccess.name.text) === -1) {
|
||||
if (!methodAccess.name || (methodAccess.name.text !== 'get' && methodAccess.name.text !== 'instant')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
export interface TranslationType {
|
||||
[key: string]: string
|
||||
};
|
||||
}
|
||||
|
||||
export class TranslationCollection {
|
||||
|
||||
|
@@ -1,5 +1,3 @@
|
||||
import * as ts from 'typescript';
|
||||
|
||||
export function _(key: string | string[]): string | string[] {
|
||||
return key;
|
||||
}
|
||||
|
@@ -18,6 +18,12 @@ describe('PipeParser', () => {
|
||||
expect(keys).to.deep.equal(['SomeKey_NotWorking']);
|
||||
});
|
||||
|
||||
it('should extract string using pipe, but between quotes only', () => {
|
||||
const contents = `<input class="form-control" type="text" placeholder="{{'user.settings.form.phone.placeholder' | translate}}" [formControl]="settingsForm.controls['phone']">`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['user.settings.form.phone.placeholder']);
|
||||
});
|
||||
|
||||
it('should extract interpolated strings using translate pipe', () => {
|
||||
const contents = `Hello {{ 'World' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
@@ -25,7 +31,7 @@ describe('PipeParser', () => {
|
||||
});
|
||||
|
||||
it('should extract strings with escaped quotes', () => {
|
||||
const contents = `Hello {{ 'World\'s largest potato' | translate }}`;
|
||||
const contents = `Hello {{ 'World\\'s largest potato' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([`World's largest potato`]);
|
||||
});
|
||||
|
@@ -151,4 +151,38 @@ describe('ServiceParser', () => {
|
||||
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;
|
||||
constructor(thing: string) {
|
||||
this.thing = thing;
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(public translate: TranslateService) {
|
||||
console.log(this.translate.instant("Hello!"));
|
||||
}
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello!']);
|
||||
});
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user