@@ -121,11 +121,11 @@ import {TranslateService} from 'ng2-translate';
121
121
@Component ({
122
122
selector: ' app' ,
123
123
template: `
124
- <div>{{ 'HELLO' | translate:{value: param} }}</div>
124
+ <div>{{ 'HELLO' | translate:param }}</div>
125
125
`
126
126
})
127
127
export class AppComponent {
128
- param: string = ' world' ;
128
+ param = {value: ' world' } ;
129
129
130
130
constructor (translate : TranslateService ) {
131
131
// this language will be used as a fallback when a translation isn't found in the current language
@@ -167,11 +167,11 @@ The `TranslateParser` understands nested JSON objects. This means that you can h
167
167
168
168
You can then access the value by using the dot notation, in this case ` HOME.HELLO ` .
169
169
170
- #### 4. Use the service or the pipe :
170
+ #### 4. Use the service, the pipe or the directive :
171
171
172
- You can either use the ` TranslateService ` or the ` TranslatePipe ` to get your translation values.
172
+ You can either use the ` TranslateService ` , the ` TranslatePipe ` or the ` TranslateDirective ` to get your translation values.
173
173
174
- With the service, it looks like this.
174
+ With the ** service** , it looks like this:
175
175
176
176
``` ts
177
177
translate .get (' HELLO' , {value: ' world' }).subscribe ((res : string ) => {
@@ -180,10 +180,25 @@ translate.get('HELLO', {value: 'world'}).subscribe((res: string) => {
180
180
});
181
181
```
182
182
183
- And this is how you do it with the pipe.
183
+ This is how you do it with the ** pipe** :
184
184
185
185
``` html
186
- <div >{{ 'HELLO' | translate:{value: param} }}</div >
186
+ <div >{{ 'HELLO' | translate:param }}</div >
187
+ ```
188
+
189
+ And in your component define ` param ` like this:
190
+ ``` ts
191
+ param = {value: ' world' };
192
+ ```
193
+
194
+ This is how you use the ** directive** :
195
+ ``` html
196
+ <div [translate] =" 'HELLO'" [translateparams] =" {param: 'world'}" ></div >
197
+ ```
198
+
199
+ Or even simpler using the content of your element as a key:
200
+ ``` html
201
+ <div translate [translateparams] =" {param: 'world'}" >HELLO</div >
187
202
```
188
203
189
204
#### 5. Use HTML tags:
@@ -196,7 +211,7 @@ You can easily use raw HTML tags within your translations.
196
211
}
197
212
```
198
213
199
- To render them, simply use the ` innerHTML ` attributeon any element.
214
+ To render them, simply use the ` innerHTML ` attribute with the pipe on any element.
200
215
201
216
``` html
202
217
<div [innerHTML] =" 'HELLO' | translate" ></div >
@@ -236,7 +251,7 @@ To render them, simply use the `innerHTML` attributeon any element.
236
251
- ` setTranslation(lang: string, translations: Object, shouldMerge: boolean = false) ` : Manually sets an object of translations for a given language , set ` shouldMerge ` to true if you want to append the translations instead of replacing them
237
252
- ` addLangs(langs: Array<string>) ` : Add new langs to the list
238
253
- ` getLangs() ` : Returns an array of currently available langs
239
- - ` get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object> ` : Gets the translated value of a key (or an array of keys )
254
+ - ` get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object> ` : Gets the translated value of a key (or an array of keys ) or the key if the value was not found
240
255
- ` instant(key: string|Array<string>, interpolateParams?: Object): string|Object ` : Gets the instant translated value of a key (or an array of keys ). / ! \ This method is ** synchronous ** and the default file loader is asynchronous . You are responsible for knowing when your translations have been loaded and it is safe to use this method . If you are not sure then you should use the ` get ` method instead .
241
256
- ` set(key: string, value: string, lang?: string) ` : Sets the translated value of a key
242
257
- ` reloadLang(lang: string): Observable<string|Object> ` : Calls resetLang and retrieves the translations object for the current loader
@@ -310,7 +325,7 @@ export class AppModule { }
310
325
311
326
### Parser
312
327
313
- If you need it for some reason , you can use the ` TranslateParser ` service .
328
+ If you need it for some reason , you can use the ` DefaultParser ` service .
314
329
315
330
#### Methods :
316
331
- ` interpolate(expr: string, params?: any): string ` : Interpolates a string to replace parameters .
@@ -344,6 +359,7 @@ Ionic 2 is still using angular 2 RC4, but ng2-translate uses RC5. You should fix
344
359
345
360
## Plugins
346
361
- [Localize Router ](https :// github.com/Greentube/localize-router) by @meeroslav: An implementation of routes localization for Angular 2. If you need localized urls (for example /fr/page and /en/page).
362
+ - [.po files Loader ](https :// www.npmjs.com/package/@biesbjerg/ng2-translate-po-loader) by @biesbjerg: Use .po translation files with ng2-translate
347
363
348
364
349
365
## Additional Framework Support
0 commit comments