diff --git a/README.md b/README.md index 506747ac..af276452 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,8 @@ translate.setTranslation('en', { - `use(lang: string): Observable`: Changes the lang currently used - `getTranslation(lang: string): Observable`: Gets an object of translations for a given language with the current loader - `setTranslation(lang: string, translations: Object)`: Manually sets an object of translations for a given language +- `setMissingTranslationHandler(handler: MissingTranslationHandler): void`: sets the Missing Translation Handler which will be +used when the requested translation is not available - `getLangs()`: Returns an array of currently available langs - `get(key: string|Array, interpolateParams?: Object): Observable`: Gets the translated value of a key (or an array of keys) - `instant(key: string|Array, interpolateParams?: Object): string|Object`: Gets the instant translated value of a key (or an array of keys) @@ -118,10 +120,10 @@ translate.setTranslation('en', { #### Write & use your own loader If you want to write your own loader, you need to create a class that implements `TranslateLoader`. The only required method is `getTranslation` that must return an `Observable`. If your loader is synchronous, just use `Observable.of` to create an observable from your static value. + +##### Example ```js class CustomLoader implements TranslateLoader { - constructor() {} - getTranslation(lang: string): Observable { return Observable.of({"KEY": "Value"}); } @@ -146,37 +148,16 @@ export class AppComponent { } ``` -### TranslatePipe -You can call the TranslatePipe with some optional parameters that will be transpolated into the translation for the given key. - -Example: -```html -

Say {{ 'HELLO' | translate:{value: "world"} }}

-``` - -With the given translation: `"HELLO": "hello {{value}}"`. +#### How to handle missing translations +You can use the method `setMissingTranslationHandler` to define a handler that will be called when the requested translation is not available. +The only required method is `handle` where you can do whatever you want. Just don't forget that it will be called synchronously from the `get` & `instant` methods. -### Parser -#### Methods: -- `interpolate(expr: string, params?: any): string`: Interpolates a string to replace parameters. - - `This is a {{ key }}` ==> `This is a value` with `params = { key: "value" }` -- `flattenObject(target: Object): Object`: Flattens an object - `{ key1: { keyA: 'valueI' }}` ==> `{ 'key1.keyA': 'valueI' }` - - -### Missing Translation Handler -#### Methods: -- `setMissingTranslationHandler(handler: MissingTranslationHandler): void`: sets the Missing Translation Handler which will be -used when the requested translation is not available - -#### Example: -Create an Missing Translation Handler +##### Example: +Create a Missing Translation Handler ```js import {MissingTranslationHandler} from 'ng2-translate/ng2-translate'; export class MyMissingTranslationHandler implements MissingTranslationHandler { - handle(key: string) { console.log(key); } @@ -191,3 +172,22 @@ constructor(translate: TranslateService) { ... } ``` + +### TranslatePipe +You can call the TranslatePipe with some optional parameters that will be transpolated into the translation for the given key. + +Example: +```html +

Say {{ 'HELLO' | translate:{value: "world"} }}

+``` + +With the given translation: `"HELLO": "hello {{value}}"`. + +### Parser +#### Methods: +- `interpolate(expr: string, params?: any): string`: Interpolates a string to replace parameters. + + `This is a {{ key }}` ==> `This is a value` with `params = { key: "value" }` +- `flattenObject(target: Object): Object`: Flattens an object + `{ key1: { keyA: 'valueI' }}` ==> `{ 'key1.keyA': 'valueI' }` +