8
8
The internationalization (i18n) library for Angular
9
9
10
10
[ ![ Build Status] ( https://img.shields.io/travis/datorama/akita.svg?style=flat-square )] ( https://travis-ci.org/ngneat/transloco )
11
- [ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-10 -orange.svg?style=flat-square )] ( #contributors- )
11
+ [ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-11 -orange.svg?style=flat-square )] ( #contributors- )
12
12
[ ![ commitizen] ( https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square )] ( )
13
13
[ ![ PRs] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square )] ( )
14
14
[ ![ coc-badge] ( https://img.shields.io/badge/codeof-conduct-ff69b4.svg?style=flat-square )] ( )
@@ -219,6 +219,8 @@ without having to repeat the `dashboard` key in each translation.
219
219
220
220
<span [attr.alt] =" 'hello' | transloco" >Attribute</span >
221
221
<span [title] =" 'hello' | transloco" >Property</span >
222
+
223
+ <span >{{ 'alert' | transloco:params:'es' }}</span >
222
224
```
223
225
224
226
## Programmatical Translation
@@ -396,7 +398,7 @@ Now we can access each one of the `todos` keys by using the `todos` namespace:
396
398
``` html
397
399
{{ 'todos.title' | transloco }}
398
400
399
- <span transloco =" toods .submit" ></span >
401
+ <span transloco =" todos .submit" ></span >
400
402
```
401
403
402
404
By default, the namespace will be the scope name (camel cased), but we can override it in two ways:
@@ -620,23 +622,34 @@ export const environment = {
620
622
621
623
## Unit Testing
622
624
623
- When running specs, we want to have the languages available immediately, in a synchronous fashion. Transloco provides you with a ` TranslocoTestingModule ` , where you can pass the languages you need in your specs. For example:
625
+ When running specs, we want to have the languages available immediately, in a synchronous fashion. Transloco provides you with a ` TranslocoTestingModule ` , where you can pass the languages you need in your specs, and the config.
626
+ We recommend to be DRY and create a module factory function that we can use in each spec, For example:
624
627
625
628
``` ts
629
+ // transloco-testing.module.ts
626
630
import { TranslocoTestingModule } from ' @ngneat/transloco' ;
627
- import en from ' ../../ assets/i18n/en.json' ;
628
- import scopeScope from ' ../../ assets/i18n/some-scope/en .json' ;
631
+ import en from ' ../assets/i18n/en.json' ;
632
+ import es from ' ../assets/i18n/es .json' ;
629
633
634
+ export function getTranslocoModule(config : Partial <TranslocoConfig > = {}) {
635
+ return TranslocoTestingModule .withLangs (
636
+ { en , es },
637
+ {
638
+ availableLangs: [' en' , ' es' ],
639
+ defaultLang: ' en' ,
640
+ ... config
641
+ }
642
+ );
643
+ }
644
+ ```
645
+
646
+ Now we can use it in each spec:
647
+
648
+ ``` ts
630
649
describe (' AppComponent' , () => {
631
650
beforeEach (async (() => {
632
651
TestBed .configureTestingModule ({
633
- imports: [
634
- RouterTestingModule ,
635
- TranslocoTestingModule .withLangs ({
636
- en ,
637
- ' some-scope/en' : scopeScope
638
- }, translocoConfig ? )
639
- ],
652
+ imports: [getTranslocoModule ()],
640
653
declarations: [AppComponent ]
641
654
}).compileComponents ();
642
655
}));
@@ -649,6 +662,30 @@ describe('AppComponent', () => {
649
662
});
650
663
```
651
664
665
+ You can find an example [ here] ( https://github.com/ngneat/transloco/blob/master/src/app/on-push/on-push.component.spec.ts )
666
+
667
+ If you need to test scopes you should add them as languages, for example:
668
+
669
+ ``` ts
670
+ export function getTranslocoModule(config : Partial <TranslocoConfig > = {}) {
671
+ return TranslocoTestingModule .withLangs (
672
+ {
673
+ en ,
674
+ es ,
675
+ ' admin-page/en' : admin ,
676
+ ' admin-page/es' : adminSpanish
677
+ },
678
+ {
679
+ availableLangs: [' en' , ' es' ],
680
+ defaultLang: ' en' ,
681
+ ... config
682
+ }
683
+ );
684
+ }
685
+ ```
686
+
687
+ You can find an example [ here] ( https://github.com/ngneat/transloco/blob/master/src/app/lazy/lazy.component.spec.ts )
688
+
652
689
Note that in order to import JSON files, you need to configure the TypeScript compiler by adding the following properties in ` tsconfig.json ` :
653
690
654
691
``` json
@@ -759,12 +796,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
759
796
<td align="center"><a href="https://github.com/theblushingcrow"><img src="https://avatars3.githubusercontent.com/u/638818?v=4" width="100px;" alt="Inbal Sinai"/><br /><sub><b>Inbal Sinai</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=theblushingcrow" title="Documentation">📖</a></td>
760
797
<td align="center"><a href="http://www.larskniep.nl"><img src="https://avatars1.githubusercontent.com/u/1215195?v=4" width="100px;" alt="Lars Kniep"/><br /><sub><b>Lars Kniep</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=larscom" title="Code">💻</a> <a href="#ideas-larscom" title="Ideas, Planning, & Feedback">🤔</a></td>
761
798
<td align="center"><a href="https://github.com/fxck"><img src="https://avatars1.githubusercontent.com/u/1303561?v=4" width="100px;" alt="Aleš"/><br /><sub><b>Aleš</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=fxck" title="Code">💻</a> <a href="#ideas-fxck" title="Ideas, Planning, & Feedback">🤔</a></td>
762
- <td align="center"><a href="https://www.codamit.dev"><img src="https://avatars0.githubusercontent.com/u/8522558?v=4" width="100px;" alt="Koala"/><br /><sub><b>Koala</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=Edouardbozon" title="Documentation">📖</a></td>
799
+ <td align="center"><a href="https://www.codamit.dev"><img src="https://avatars0.githubusercontent.com/u/8522558?v=4" width="100px;" alt="Koala"/><br /><sub><b>Koala</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=Edouardbozon" title="Documentation">📖</a> <a href="https://github.com/ngneat/transloco/commits?author=Edouardbozon" title="Code">💻</a> </td>
763
800
</tr >
764
801
<tr >
765
802
<td align="center"><a href="https://github.com/DerSizeS"><img src="https://avatars3.githubusercontent.com/u/708090?v=4" width="100px;" alt="Oleg Teterin"/><br /><sub><b>Oleg Teterin</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=DerSizeS" title="Code">💻</a></td>
766
803
<td align="center"><a href="https://twitter.com/maxime1992"><img src="https://avatars0.githubusercontent.com/u/4950209?v=4" width="100px;" alt="Maxime"/><br /><sub><b>Maxime</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=maxime1992" title="Documentation">📖</a></td>
767
804
<td align="center"><a href="https://github.com/zufarzhan"><img src="https://avatars3.githubusercontent.com/u/22148960?v=4" width="100px;" alt="Zufar Ismanov"/><br /><sub><b>Zufar Ismanov</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=zufarzhan" title="Code">💻</a> <a href="#ideas-zufarzhan" title="Ideas, Planning, & Feedback">🤔</a></td>
805
+ <td align="center"><a href="https://github.com/sk222sw"><img src="https://avatars0.githubusercontent.com/u/8642363?v=4" width="100px;" alt="Sonny Kjellberg"/><br /><sub><b>Sonny Kjellberg</b></sub></a><br /><a href="https://github.com/ngneat/transloco/commits?author=sk222sw" title="Documentation">📖</a></td>
768
806
</tr >
769
807
</table >
770
808
0 commit comments