Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
4e351405fb | ||
|
39a335638b | ||
|
3b9561916b | ||
|
5cef383f3b | ||
|
677d2a35ca | ||
|
262a89206d |
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
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@biesbjerg/ngx-translate-extract",
|
"name": "@biesbjerg/ngx-translate-extract",
|
||||||
"version": "2.2.1",
|
"version": "2.2.5",
|
||||||
"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",
|
||||||
@@ -58,8 +58,8 @@
|
|||||||
"chai": "3.5.0",
|
"chai": "3.5.0",
|
||||||
"mocha": "3.2.0",
|
"mocha": "3.2.0",
|
||||||
"ts-node": "3.0.2",
|
"ts-node": "3.0.2",
|
||||||
"tslint": "4.5.1",
|
"tslint": "5.0.0",
|
||||||
"tslint-eslint-rules": "3.5.1",
|
"tslint-eslint-rules": "4.0.0",
|
||||||
"typescript": "2.2.2"
|
"typescript": "2.2.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@@ -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');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -15,10 +15,10 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac
|
|||||||
protected _parseTemplate(template: string): TranslationCollection {
|
protected _parseTemplate(template: string): TranslationCollection {
|
||||||
let collection: TranslationCollection = new 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;
|
let matches: RegExpExecArray;
|
||||||
while (matches = regExp.exec(template)) {
|
while (matches = regExp.exec(template)) {
|
||||||
collection = collection.add(matches[2]);
|
collection = collection.add(matches[2].replace('\\\'', '\''));
|
||||||
}
|
}
|
||||||
|
|
||||||
return collection;
|
return collection;
|
||||||
|
@@ -49,6 +49,11 @@ 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) {
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
export interface TranslationType {
|
export interface TranslationType {
|
||||||
[key: string]: string
|
[key: string]: string
|
||||||
};
|
}
|
||||||
|
|
||||||
export class TranslationCollection {
|
export class TranslationCollection {
|
||||||
|
|
||||||
|
@@ -18,6 +18,12 @@ describe('PipeParser', () => {
|
|||||||
expect(keys).to.deep.equal(['SomeKey_NotWorking']);
|
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', () => {
|
it('should extract interpolated strings using translate pipe', () => {
|
||||||
const contents = `Hello {{ 'World' | translate }}`;
|
const contents = `Hello {{ 'World' | translate }}`;
|
||||||
const keys = parser.extract(contents, templateFilename).keys();
|
const keys = parser.extract(contents, templateFilename).keys();
|
||||||
@@ -25,7 +31,7 @@ describe('PipeParser', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should extract strings with escaped quotes', () => {
|
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();
|
const keys = parser.extract(contents, templateFilename).keys();
|
||||||
expect(keys).to.deep.equal([`World's largest potato`]);
|
expect(keys).to.deep.equal([`World's largest potato`]);
|
||||||
});
|
});
|
||||||
|
@@ -151,4 +151,17 @@ 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([]);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user