Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e8bc023ea6 | ||
|
33d8c26a28 | ||
|
b07d929484 | ||
|
a0f2b69f36 | ||
|
13f46a524f | ||
|
6ed962fa6e | ||
|
8aa2774eca | ||
|
37ca29648a | ||
|
97d844c3d2 | ||
|
e7795c5349 | ||
|
9da4939f5d |
12
README.md
12
README.md
@@ -91,12 +91,12 @@ Options:
|
||||
patterns and multiple paths [array] [required]
|
||||
|
||||
Examples:
|
||||
cli.js -i ./src-a/ -i ./src-b/ -o strings.json Extract (ts, html) from multiple paths
|
||||
cli.js -i './{src-a,src-b}/' -o strings.json Extract (ts, html) from multiple paths using brace
|
||||
ngx-translate-extract -i ./src-a/ -i ./src-b/ -o strings.json Extract (ts, html) from multiple paths
|
||||
ngx-translate-extract -i './{src-a,src-b}/' -o strings.json Extract (ts, html) from multiple paths using brace
|
||||
expansion
|
||||
cli.js -i ./src/ -o ./i18n/da.json -o ./i18n/en.json Extract (ts, html) and save to da.json and en.json
|
||||
cli.js -i ./src/ -o './i18n/{en,da}.json' Extract (ts, html) and save to da.json and en.json
|
||||
ngx-translate-extract -i ./src/ -o ./i18n/da.json -o ./i18n/en.json Extract (ts, html) and save to da.json and en.json
|
||||
ngx-translate-extract -i ./src/ -o './i18n/{en,da}.json' Extract (ts, html) and save to da.json and en.json
|
||||
using brace expansion
|
||||
cli.js -i './src/**/*.{ts,tsx,html}' -o strings.json Extract from ts, tsx and html
|
||||
cli.js -i './src/**/!(*.spec).{ts,html}' -o Extract from ts, html, excluding files with ".spec"
|
||||
ngx-translate-extract -i './src/**/*.{ts,tsx,html}' -o strings.json Extract from ts, tsx and html
|
||||
ngx-translate-extract -i './src/**/!(*.spec).{ts,html}' -o Extract from ts, html, excluding files with ".spec"
|
||||
strings.json
|
10
package-lock.json
generated
10
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@biesbjerg/ngx-translate-extract",
|
||||
"version": "4.2.0",
|
||||
"version": "5.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -1978,6 +1978,14 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"rimraf": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
|
||||
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
|
||||
"requires": {
|
||||
"glob": "^7.1.3"
|
||||
}
|
||||
},
|
||||
"rxjs": {
|
||||
"version": "6.5.4",
|
||||
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.4.tgz",
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@biesbjerg/ngx-translate-extract",
|
||||
"version": "5.0.0",
|
||||
"version": "6.0.0",
|
||||
"description": "Extract strings from projects using ngx-translate",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
@@ -14,7 +14,7 @@
|
||||
"scripts": {
|
||||
"build": "npm run clean && tsc",
|
||||
"watch": "npm run clean && tsc --watch",
|
||||
"clean": "rm -rf ./dist",
|
||||
"clean": "rimraf ./dist",
|
||||
"lint": "tslint --force './src/**/*.ts'",
|
||||
"test": "mocha -r ts-node/register tests/**/*.spec.ts"
|
||||
},
|
||||
@@ -79,7 +79,8 @@
|
||||
"tslint": "^6.1.0",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
"tslint-eslint-rules": "^5.4.0",
|
||||
"tslint-etc": "^1.10.1"
|
||||
"tslint-etc": "^1.10.1",
|
||||
"rimraf": "^3.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@angular/compiler": "^9.0.7",
|
||||
|
@@ -14,7 +14,7 @@ export class DirectiveParser implements ParserInterface {
|
||||
|
||||
const nodes: TmplAstNode[] = this.parseTemplate(source, filePath);
|
||||
this.getTranslatableElements(nodes).forEach(element => {
|
||||
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
|
||||
const key = this.getElementTranslateAttrValue(element) || this.getElementContent(element);
|
||||
collection = collection.add(key);
|
||||
});
|
||||
|
||||
@@ -35,7 +35,7 @@ export class DirectiveParser implements ParserInterface {
|
||||
return [];
|
||||
}
|
||||
|
||||
// If element has translate attribute all its contents is translatable
|
||||
// If element has translate attribute all its content is translatable
|
||||
// so we don't need to traverse any deeper
|
||||
if (this.isTranslatable(node)) {
|
||||
return [node];
|
||||
@@ -73,10 +73,15 @@ export class DirectiveParser implements ParserInterface {
|
||||
return attr?.value ?? '';
|
||||
}
|
||||
|
||||
protected getElementContents(element: TmplAstElement): string {
|
||||
const contents = element.sourceSpan.start.file.content;
|
||||
protected getElementContent(element: TmplAstElement): string {
|
||||
const content = element.sourceSpan.start.file.content;
|
||||
const start = element.startSourceSpan.end.offset;
|
||||
const end = element.endSourceSpan.start.offset;
|
||||
return contents.substring(start, end).trim();
|
||||
const val = content.substring(start, end);
|
||||
return this.cleanKey(val);
|
||||
}
|
||||
|
||||
protected cleanKey(val: string): string {
|
||||
return val.replace(/\r?\n|\r|\t/g, '');
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
import { ClassDeclaration, CallExpression } from 'typescript';
|
||||
import { tsquery } from '@phenomnomnominal/tsquery';
|
||||
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { findClassDeclarations, findClassPropertyByType, findMethodCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
|
||||
import { findClassDeclarations, findClassPropertyByType, findPropertyCallExpressions, findMethodCallExpressions, getStringsFromExpression, findMethodParameterByType, findConstructorDeclaration } from '../utils/ast-helpers';
|
||||
|
||||
const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
|
||||
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
|
||||
@@ -19,12 +20,11 @@ export class ServiceParser implements ParserInterface {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
classDeclarations.forEach(classDeclaration => {
|
||||
const propName: string = findClassPropertyByType(classDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
|
||||
if (!propName) {
|
||||
return;
|
||||
}
|
||||
const callExpressions = [
|
||||
...this.findConstructorParamCallExpressions(classDeclaration),
|
||||
...this.findPropertyCallExpressions(classDeclaration)
|
||||
];
|
||||
|
||||
const callExpressions = findMethodCallExpressions(classDeclaration, propName, TRANSLATE_SERVICE_METHOD_NAMES);
|
||||
callExpressions.forEach(callExpression => {
|
||||
const [firstArg] = callExpression.arguments;
|
||||
if (!firstArg) {
|
||||
@@ -36,4 +36,21 @@ export class ServiceParser implements ParserInterface {
|
||||
});
|
||||
return collection;
|
||||
}
|
||||
|
||||
protected findConstructorParamCallExpressions(classDeclaration: ClassDeclaration): CallExpression[] {
|
||||
const constructorDeclaration = findConstructorDeclaration(classDeclaration);
|
||||
if (!constructorDeclaration) {
|
||||
return [];
|
||||
}
|
||||
const paramName = findMethodParameterByType(constructorDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
|
||||
return findMethodCallExpressions(constructorDeclaration, paramName, TRANSLATE_SERVICE_METHOD_NAMES);
|
||||
}
|
||||
|
||||
protected findPropertyCallExpressions(classDeclaration: ClassDeclaration): CallExpression[] {
|
||||
const propName: string = findClassPropertyByType(classDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
|
||||
if (!propName) {
|
||||
return [];
|
||||
}
|
||||
return findPropertyCallExpressions(classDeclaration, propName, TRANSLATE_SERVICE_METHOD_NAMES);
|
||||
}
|
||||
}
|
||||
|
@@ -1,15 +1,16 @@
|
||||
import { tsquery } from '@phenomnomnominal/tsquery';
|
||||
import {
|
||||
SyntaxKind,
|
||||
Node,
|
||||
NamedImports,
|
||||
Identifier,
|
||||
ClassDeclaration,
|
||||
CallExpression,
|
||||
ConstructorDeclaration,
|
||||
isStringLiteralLike,
|
||||
isArrayLiteralExpression,
|
||||
CallExpression,
|
||||
Expression,
|
||||
isBinaryExpression,
|
||||
SyntaxKind,
|
||||
isConditionalExpression,
|
||||
PropertyAccessExpression
|
||||
} from 'typescript';
|
||||
@@ -45,6 +46,30 @@ export function findClassPropertyByType(node: ClassDeclaration, type: string): s
|
||||
return findClassPropertyConstructorParameterByType(node, type) || findClassPropertyDeclarationByType(node, type);
|
||||
}
|
||||
|
||||
export function findConstructorDeclaration(node: ClassDeclaration): ConstructorDeclaration {
|
||||
const query = `Constructor`;
|
||||
const [result] = tsquery<ConstructorDeclaration>(node, query);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function findMethodParameterByType(node: Node, type: string): string | null {
|
||||
const query = `Parameter:has(TypeReference > Identifier[name="${type}"]) > Identifier`;
|
||||
const [result] = tsquery<Identifier>(node, query);
|
||||
if (result) {
|
||||
return result.text;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function findMethodCallExpressions(node: Node, propName: string, fnName: string | string[]): CallExpression[] {
|
||||
if (Array.isArray(fnName)) {
|
||||
fnName = fnName.join('|');
|
||||
}
|
||||
const query = `CallExpression > PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(Identifier[name="${propName}"]):not(:has(ThisKeyword)))`;
|
||||
const nodes = tsquery<PropertyAccessExpression>(node, query).map(n => n.parent as CallExpression);
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export function findClassPropertyConstructorParameterByType(node: ClassDeclaration, type: string): string | null {
|
||||
const query = `Constructor Parameter:has(TypeReference > Identifier[name="${type}"]):has(PublicKeyword,ProtectedKeyword,PrivateKeyword) > Identifier`;
|
||||
const [result] = tsquery<Identifier>(node, query);
|
||||
@@ -72,7 +97,7 @@ export function findFunctionCallExpressions(node: Node, fnName: string | string[
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export function findMethodCallExpressions(node: Node, prop: string, fnName: string | string[]): CallExpression[] {
|
||||
export function findPropertyCallExpressions(node: Node, prop: string, fnName: string | string[]): CallExpression[] {
|
||||
if (Array.isArray(fnName)) {
|
||||
fnName = fnName.join('|');
|
||||
}
|
||||
|
@@ -2,6 +2,12 @@ import * as os from 'os';
|
||||
import * as fs from 'fs';
|
||||
import * as braces from 'braces';
|
||||
|
||||
declare module 'braces' {
|
||||
interface Options {
|
||||
keepEscaping?: boolean; // Workaround for option not present in @types/braces 3.0.0
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeHomeDir(path: string): string {
|
||||
if (path.substring(0, 1) === '~') {
|
||||
return `${os.homedir()}/${path.substring(1)}`;
|
||||
@@ -10,7 +16,7 @@ export function normalizeHomeDir(path: string): string {
|
||||
}
|
||||
|
||||
export function expandPattern(pattern: string): string[] {
|
||||
return braces(pattern, { expand: true });
|
||||
return braces(pattern, { expand: true, keepEscaping: true });
|
||||
}
|
||||
|
||||
export function normalizePaths(patterns: string[], defaultPatterns: string[] = []): string[] {
|
||||
|
1
tests/app/login.html
Normal file
1
tests/app/login.html
Normal file
@@ -0,0 +1 @@
|
||||
{{ 'SOURCES.' + source.name + '.NAME_PLURAL' | translate }}
|
@@ -85,4 +85,32 @@ describe('DirectiveParser', () => {
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should extract contents without line breaks', () => {
|
||||
const contents = `
|
||||
<p translate>
|
||||
Please leave a message for your client letting them know why you
|
||||
rejected the field and what they need to do to fix it.
|
||||
</p>
|
||||
`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Please leave a message for your client letting them know why you rejected the field and what they need to do to fix it.']);
|
||||
});
|
||||
|
||||
it('should extract contents without indent spaces', () => {
|
||||
const contents = `
|
||||
<div *ngIf="!isLoading && studentsToGrid && studentsToGrid.length == 0" class="no-students" mt-rtl translate>There
|
||||
are currently no students in this class. The good news is, adding students is really easy! Just use the options
|
||||
at the top.
|
||||
</div>
|
||||
`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['There are currently no students in this class. The good news is, adding students is really easy! Just use the options at the top.']);
|
||||
});
|
||||
|
||||
it('should extract contents without indent spaces', () => {
|
||||
const contents = `<button mat-button (click)="search()" translate>client.search.searchBtn</button>`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['client.search.searchBtn']);
|
||||
});
|
||||
});
|
||||
|
@@ -35,4 +35,20 @@ describe('MarkerParser', () => {
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello world', 'This is a very very very very long line.', 'Mix of different types']);
|
||||
});
|
||||
|
||||
it('should extract the strings', () => {
|
||||
const contents = `
|
||||
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
|
||||
|
||||
export class AppModule {
|
||||
constructor() {
|
||||
marker('DYNAMIC_TRAD.val1');
|
||||
marker('DYNAMIC_TRAD.val2');
|
||||
}
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['DYNAMIC_TRAD.val1', 'DYNAMIC_TRAD.val2']);
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -147,4 +147,16 @@ describe('PipeParser', () => {
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['message']);
|
||||
});
|
||||
|
||||
it('should ignore calculated values', () => {
|
||||
const contents = `{{ 'SOURCES.' + source.name + '.NAME_PLURAL' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should not extract pipe argument', () => {
|
||||
const contents = `{{ value | valueToTranslationKey: 'argument' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
});
|
||||
|
@@ -11,6 +11,18 @@ describe('ServiceParser', () => {
|
||||
parser = new ServiceParser();
|
||||
});
|
||||
|
||||
it('should extract strings when TranslateService is accessed directly via constructor parameter', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class MyComponent {
|
||||
public constructor(protected translateService: TranslateService) {
|
||||
translateService.get('It works!');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['It works!']);
|
||||
});
|
||||
|
||||
it('should support extracting binary expressions', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
|
Reference in New Issue
Block a user