Skip to content

Commit

Permalink
ngx-translate explicit IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
martinroob committed May 3, 2017
1 parent dfbe2fb commit be19d10
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
1 change: 0 additions & 1 deletion ngx-i18nsupport.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions public_api.d.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
"../node_modules/@types"
]
},
"include": [
"includes": [
"**/*"
],
"exclude": [
"node_modules"
]
}
42 changes: 35 additions & 7 deletions src/xliffmerge/ngx-translate-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,46 @@ export class NgxTranslateExtractor {
private extract(): NgxMessage[] {
let result: NgxMessage[] = [];
this.messagesFile.forEachTransUnit((tu: ITransUnit) => {
let description = tu.description();
let id = tu.meaning();
if (description && description === 'ngx-translate') {
let messagetext = this.toTranslateString(tu.targetContentNormalized());
result.push({id: id, message: messagetext});
let ngxId = this.ngxTranslateIdFromTU(tu);
if (ngxId) {
let messagetext = tu.targetContentNormalized();
result.push({id: ngxId, message: messagetext});
}
});
return result;
}

private toTranslateString(contentFromTU: string): string {
return contentFromTU;
/**
* Check, wether this tu should be extracted for ngx-translate usage, and return its id for ngx-translate.
* There are 2 possibilities:
* 1. description is set to "ngx-translate" and meaning contains the id.
* 2. id is explicitly set to a string.
* @param tu
* @return an id or null, if this tu should not be extracted.
*/
private ngxTranslateIdFromTU(tu: ITransUnit): string {
if (this.isExplicitlySetId(tu.id)) {
return tu.id;
}
let description = tu.description();
if (description && description === 'ngx-translate') {
return tu.meaning();
}
}

/**
* Test, wether ID was explicitly set (via i18n="@myid).
* Just heuristic, an ID is explicitly that, if it does not look like a generated one.
* @param id
* @return {boolean}
*/
private isExplicitlySetId(id: string): boolean {
if (isNullOrUndefined(id)) {
return false;
}
// generated IDs are either decimal or sha1 hex
let reForGeneratedId = /^[0-9a-f]{11,}$/;
return !reForGeneratedId.test(id);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/xliffmerge/xliff-merge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ describe('XliffMerge test spec', () => {
expect(translation.myapp).toBeTruthy();
expect(translation.dateservice.monday).toBe("Montag");
expect(translation.dateservice.friday).toBe("Freitag");
expect(translation.explicitlysetids.test1).toBe("Explizit gesetzte ID");
done();
});

Expand Down Expand Up @@ -912,6 +913,8 @@ describe('XliffMerge test spec', () => {
expect(translation.myapp).toBeTruthy();
expect(translation.dateservice.monday).toBe("Montag");
expect(translation.dateservice.friday).toBe("Freitag");
expect(translation.explicitlysetids.test1).toBe("Explizit gesetzte ID");
expect(Object.keys(translation).length).toBe(5);
done();
});

Expand Down

0 comments on commit be19d10

Please sign in to comment.