Skip to content

Commit 6e86224

Browse files
committed
index on master: 1daa081 Module forRoot update
1 parent 1daa081 commit 6e86224

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

src/I18NextCapPipe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class I18NextCapPipe extends I18NextPipe implements PipeTransform {
2121

2222
public transform(key: string | string[], options?: any): string {
2323
options = options || {};
24-
options.case = 'cap';
24+
options.format = 'cap';
2525
return super.transform(key, options);
2626
}
2727
}

src/I18NextFormatPipe.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Inject, Injectable, Pipe, PipeTransform } from '@angular/core';
2+
3+
import { I18NEXT_SERVICE } from './I18NEXT_TOKENS';
4+
import { ITranslationService } from './ITranslationService';
5+
6+
@Injectable()
7+
@Pipe({
8+
name: 'i18nextFormat'
9+
})
10+
export class I18NextFormatPipe implements PipeTransform {
11+
12+
constructor(
13+
@Inject(I18NEXT_SERVICE) private translateI18Next: ITranslationService
14+
) {}
15+
16+
public transform(value: string, options: Object | string): string {
17+
let opts: any = typeof(options) === 'string' ? { format: options } : options;
18+
return this.translateI18Next.format(value, opts.format, opts.lng);
19+
}
20+
}

src/I18NextService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export class I18NextService implements ITranslationService {
5252
return i18next.t.call(i18next, <any>key, options);
5353
}
5454

55+
public format(value: string, format: string, lng: string): string {
56+
return i18next.format.call(i18next, value, format, lng);
57+
}
58+
5559
public changeLanguage(lng: string): Promise<any> {
5660
return new Promise<any>(
5761
(resolve: (thenableOrResult?: any) => void,

src/ITranslationService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface ITranslationService {
2020

2121
t(key: string | string[], options?: any): string;
2222

23+
format(value: string, format: string, lng: string): string;
24+
2325
changeLanguage(lng: string): Promise<any>;
2426

2527
loadNamespaces(namespaces: string[]): Promise<any>;

src/index.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './I18NEXT_TOKENS';
22
export * from './I18NextPipe';
33
export * from './I18NextCapPipe';
4+
export * from './I18NextFormatPipe';
45
export * from './I18NextService';
56
export * from './I18NextTitle';
67
export * from './I18nextNamespaceResolver';
@@ -14,6 +15,7 @@ import { I18NEXT_NAMESPACE, I18NEXT_SCOPE, I18NEXT_SERVICE, I18NEXT_NAMESPACE_RE
1415
import { I18NextTitle } from './I18NextTitle';
1516
import { I18NextPipe } from './I18NextPipe';
1617
import { I18NextCapPipe } from './I18NextCapPipe';
18+
import { I18NextFormatPipe } from './I18NextFormatPipe';
1719
import { I18NextService } from './I18NextService';
1820
import { ITranslationService } from './ITranslationService';
1921
import { I18nextNamespaceResolver } from './I18nextNamespaceResolver';
@@ -31,15 +33,18 @@ import { I18nextNamespaceResolver } from './I18nextNamespaceResolver';
3133
},
3234
I18NextPipe,
3335
I18NextCapPipe,
36+
I18NextFormatPipe,
3437
I18NextTitle
3538
],
3639
declarations: [
3740
I18NextPipe,
38-
I18NextCapPipe
41+
I18NextCapPipe,
42+
I18NextFormatPipe
3943
],
4044
exports: [
4145
I18NextPipe,
42-
I18NextCapPipe
46+
I18NextCapPipe,
47+
I18NextFormatPipe
4348
]
4449
})
4550
export class I18NextModule {
@@ -52,6 +57,7 @@ export class I18NextModule {
5257
I18NextService,
5358
I18NextPipe,
5459
I18NextCapPipe,
60+
I18NextFormatPipe,
5561
I18nextNamespaceResolver
5662
];
5763

@@ -67,4 +73,33 @@ export class I18NextModule {
6773
providers: providers
6874
};
6975
}
76+
77+
static interpolationFormat(customFormat: Function = null): Function {
78+
return (value: string, format: string, lng: string): string => {
79+
let formatedValue: string;
80+
if (!value)
81+
formatedValue = value;
82+
switch (format) {
83+
case 'upper':
84+
case 'uppercase':
85+
formatedValue = value.toUpperCase();
86+
break;
87+
case 'lower':
88+
case 'lowercase':
89+
formatedValue = value.toLowerCase();
90+
break;
91+
case 'cap':
92+
case 'capitalize':
93+
formatedValue = value.charAt(0).toUpperCase() + value.slice(1);
94+
break;
95+
case null:
96+
case 'none':
97+
default:
98+
formatedValue = value;
99+
}
100+
if (customFormat === null)
101+
return formatedValue;
102+
return customFormat(formatedValue, format, lng);
103+
};
104+
}
70105
}

tests/mocks/MockTranslationService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export class MockI18NextService implements ITranslationService {
4141
return key;
4242
}
4343

44+
public format(value: string, format: string, lng: string): string {
45+
if (format === 'cap')
46+
return value.toUpperCase();
47+
return value;
48+
}
49+
4450
public changeLanguage(lng: string): Promise<any> {
4551
return new Promise<any>(
4652
(resolve: (thenableOrResult?: any) => void,

tests/pipes/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('I18NextPipe tests', function() {
2020
let pipe = new I18NextPipe(service, DEFAULT_NAMESPACE, DEFAULT_SCOPE);
2121
let capPipe = new I18NextCapPipe(service, DEFAULT_NAMESPACE, DEFAULT_SCOPE);
2222
let key = 'test';
23-
let transResult = pipe.transform(key, { case: 'cap' });
23+
let transResult = pipe.transform(key, { format: 'cap' });
2424
let transCapResult = capPipe.transform(key);
2525
expect(transResult[0]).toEqual(key[0].toUpperCase());
2626
expect(transResult).toEqual(transCapResult);

0 commit comments

Comments
 (0)