Skip to content

Commit

Permalink
fixing types for issue: #10
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvanstrijp committed Jul 3, 2019
1 parent 53273e8 commit bc8bc59
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/services/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class I18nService {
public translate(
lang: string,
key: string,
args: Array<{ [k: string]: any } | string> = [],
args?: Array<{ [k: string]: any } | string> | { [k: string]: any },
) {
let translation = this.translations[lang][key];

Expand All @@ -37,8 +37,11 @@ export class I18nService {
return undefined;
}
}
if (args && args.length > 0) {
translation = format(translation, ...(args || []));
if (args || (args instanceof Array && args.length > 0)) {
translation = format(
translation,
...(args instanceof Array ? args || [] : [args]),
);
}
return translation;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-i18n",
"version": "2.2.1",
"version": "2.2.2",
"description": "",
"author": "Toon van Strijp",
"repository": {
Expand Down
4 changes: 4 additions & 0 deletions tests/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('i18n module', () => {
expect(i18nService.translate('nl', 'PRODUCT.NEW', [{ name: 'Test' }])).toBe(
'Nieuw Product: Test',
);

expect(i18nService.translate('nl', 'PRODUCT.NEW', { name: 'Test' })).toBe(
'Nieuw Product: Test',
);
});

it('i18n service should return array translation', async () => {
Expand Down

0 comments on commit bc8bc59

Please sign in to comment.