Skip to content

Commit 214e35d

Browse files
committed
feat: improved typings
1 parent d26ef0d commit 214e35d

7 files changed

+35
-26
lines changed

src/I18NextCapPipe.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Inject, Injectable, Pipe, PipeTransform } from '@angular/core';
33
import { I18NEXT_NAMESPACE, I18NEXT_SCOPE, I18NEXT_SERVICE } from './I18NEXT_TOKENS';
44
import { I18NextPipe } from './I18NextPipe';
55
import { ITranslationService } from './ITranslationService';
6+
import { PipeOptions } from './models';
67

78
@Injectable()
89
@Pipe({
@@ -17,7 +18,7 @@ export class I18NextCapPipe extends I18NextPipe implements PipeTransform {
1718
super(translateI18Next, ns, scope);
1819
}
1920

20-
public transform(key: string | string[], options?: any): string {
21+
public transform(key: string | string[], options?: PipeOptions): string {
2122
options = options || {};
2223
options.format = 'cap';
2324
return super.transform(key, options);

src/I18NextEagerPipe.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Subject } from 'rxjs';
44
import { takeUntil } from 'rxjs/operators';
55
import { I18NEXT_NAMESPACE, I18NEXT_SCOPE, I18NEXT_SERVICE } from './I18NEXT_TOKENS';
66
import { ITranslationService } from './ITranslationService';
7+
import { PipeOptions } from './models';
78

89
@Pipe({
910
name: 'i18nextEager',
@@ -32,7 +33,7 @@ constructor(
3233
});
3334
}
3435

35-
public transform(key: string | string[], options?: any): string {
36+
public transform(key: string | string[], options?: PipeOptions): string {
3637
const newKey = this.translateI18Next.language + '|' + JSON.stringify(key);
3738
if (!this.lastKey || this.lastKey !== newKey) {
3839
this.lastKey = newKey;

src/I18NextFormatPipe.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Inject, Injectable, Pipe, PipeTransform } from '@angular/core';
22

33
import { I18NEXT_SERVICE } from './I18NEXT_TOKENS';
44
import { ITranslationService } from './ITranslationService';
5+
import { FormatPipeOptions } from './models';
56

67
@Injectable()
78
@Pipe({
@@ -13,8 +14,8 @@ export class I18NextFormatPipe implements PipeTransform {
1314
@Inject(I18NEXT_SERVICE) private translateI18Next: ITranslationService
1415
) {}
1516

16-
public transform(value: any, options: Object | string): string {
17-
let opts: any = typeof(options) === 'string' ? { format: options } : options;
17+
public transform(value: any, options: FormatPipeOptions | string): string {
18+
let opts: FormatPipeOptions = typeof(options) === 'string' ? { format: options } : options;
1819
return this.translateI18Next.format(value, opts.format, opts.lng);
1920
}
2021
}

src/I18NextPipe.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Inject, Injectable, Pipe, PipeTransform } from '@angular/core';
2+
import * as i18n from 'i18next';
23

34
import { I18NEXT_NAMESPACE, I18NEXT_SCOPE, I18NEXT_SERVICE } from './I18NEXT_TOKENS';
45
import { ITranslationService } from './ITranslationService';
6+
import { PipeOptions } from './models';
57
@Injectable()
68
@Pipe({
79
name: 'i18next'
@@ -13,10 +15,10 @@ export class I18NextPipe implements PipeTransform {
1315
@Inject(I18NEXT_NAMESPACE) protected ns: string | string[],
1416
@Inject(I18NEXT_SCOPE) protected scope: string | string[]) {}
1517

16-
public transform(key: string | string[], options?: any): string {
18+
public transform(key: string | string[], options?: PipeOptions): string {
1719
options = this.prepareOptions(options);
1820

19-
let i18nOpts = this.translateI18Next.options;
21+
let i18nOpts: i18n.TOptions = this.translateI18Next.options;
2022
if (options.prependScope === undefined || options.prependScope === true) {
2123
if (this.scope) {
2224
key = this.prependScope(key, this.scope, i18nOpts.keySeparator, i18nOpts.nsSeparator);
@@ -28,12 +30,8 @@ export class I18NextPipe implements PipeTransform {
2830
}
2931
}
3032

31-
let result: string;
32-
if (options.defaultValue) {
33-
result = this.translateI18Next.t(key, options.defaultValue, options);
34-
} else {
35-
result = this.translateI18Next.t(key, options);
36-
}
33+
let result: string = this.translateI18Next.t(key, options);
34+
3735
if (options.format) {
3836
if (result) {
3937
result = this.translateI18Next
@@ -87,7 +85,7 @@ export class I18NextPipe implements PipeTransform {
8785
return key.indexOf(nsSeparator) !== -1;
8886
}
8987

90-
private prepareOptions(options: any) {
88+
private prepareOptions(options: PipeOptions) {
9189
options = options || {};
9290
if (options.context != null)
9391
options.context = options.context.toString();

src/I18NextService.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export class I18NextService implements ITranslationService {
2424

2525
constructor(@Inject(I18NEXT_ERROR_HANDLING_STRATEGY) private errorHandlingStrategy: I18NextErrorHandlingStrategy) {}
2626

27-
public use(plugin: any) {
28-
i18next.use.call(i18next, plugin);
27+
public use<T extends i18n.Module>(module: T | i18n.Newable<T> | i18n.ThirdPartyModule[] | i18n.Newable<i18n.ThirdPartyModule>[]) {
28+
i18next.use.call(i18next, module);
2929
return this;
3030
}
3131

32-
public init(options?: any): Promise<any> {
32+
public init(options?: i18n.InitOptions): Promise<any> {
3333
options = options || {};
3434

3535
this.subscribeEvents();
@@ -45,13 +45,16 @@ export class I18NextService implements ITranslationService {
4545

4646
}
4747

48-
public t(key: string | string[], options?: any): string;
49-
public t(key: string | string[], defaultValue?: any, options?: any): string {
50-
options = options || {};
51-
return i18next.t.call(i18next, <any>key, defaultValue, options);
48+
t(key: string | string[], optionsOrDefault?: string | i18n.TOptions, options?: i18n.TOptions): string {
49+
const hasDefault = optionsOrDefault && typeof(optionsOrDefault) === 'string';
50+
if (hasDefault) {
51+
return i18next.t.call(i18next, <any>key, optionsOrDefault, options);
52+
} else {
53+
return i18next.t.call(i18next, <any>key, options);
54+
}
5255
}
5356

54-
public format(value: any, format: string, lng: string): string {
57+
public format(value: any, format?: string, lng?: string): string {
5558
return i18next.format.call(i18next, value, format, lng);
5659
}
5760

src/ITranslationService.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as i18n from 'i18next';
12
import { ITranslationEvents } from './ITranslationEvents';
23

34
export interface ITranslationService {
@@ -8,14 +9,13 @@ export interface ITranslationService {
89

910
options: any;
1011

11-
use(plugin: any);
12+
use<T extends i18n.Module>(module: T | i18n.Newable<T> | i18n.ThirdPartyModule[] | i18n.Newable<i18n.ThirdPartyModule>[]);
1213

13-
init(options?: any): Promise<any>;
14+
init(options?: i18n.InitOptions): Promise<any>;
1415

15-
t(key: string | string[], options?: any): string;
16-
t(key: string | string[], defaultValue?: any, options?: any): string;
16+
t(key: string | string[], optionsOrDefault?: string | i18n.TOptions, options?: i18n.TOptions): string;
1717

18-
format(value: any, format: string, lng: string): string;
18+
format: i18n.FormatFunction;
1919

2020
exists(key, options);
2121

src/models.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as i18n from 'i18next';
2+
3+
export type FormatPipeOptions = { format?: string, lng?: string };
4+
export type PrependPipeOptions = { prependScope?: boolean, prependNamespace?: boolean };
5+
export type PipeOptions = i18n.TOptions & FormatPipeOptions & PrependPipeOptions;

0 commit comments

Comments
 (0)