Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Nov 25, 2018
1 parent d0499af commit 37c3b7a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 40 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ Get the changelog by [releases](https://github.com/robisim74/angular-l10n/releas
- Fallback
- Custom
- Caching of the http requests
- Rollback on error
- Composed languages: `language[-script][-country]`
- Language, Default locale & Currency through _ISO codes_
- Timezone through _IANA_ time zone
- Cookies, Session & Local Storage, or custom storage (e.g. for using with _Ionic Storage_) available for storing the _ISO codes_
- _Intl API_ to localize dates & numbers
- Localized routing for SEO
- Pure pipes to get the translation
- Directives to get the translation:
- can dynamically change parameters and expressions values as the pipes
- can translate also attributes
- work also with complex UI components, like _Material_ or _Ionic_
- Methods to get the translation in component class
- Html tags in translations
- Parameters in translations
- Composed keys in translations (nested objects)
- Html tags in translations
- Methods to get the translation in component class
- AoT compliant
- Lazy loading compliant
- Server Side Rendering compliant
- `strict` TypeScript compiler option compliant
- Localized routing for SEO

## Angular i18n solutions
| _Feature_ | [Angular](https://angular.io/guide/i18n) _Official_ | [ngx-translate](http://www.ngx-translate.com) _External library_ | [angular-l10n](https://robisim74.github.io/angular-l10n) _External library_ |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It allows, in addition to translation, to localize numbers and dates of your app

---

Library version: 6.5.0 - [Changelog](https://github.com/robisim74/angular-l10n/releases)
Library version: 6.6.1 - [Changelog](https://github.com/robisim74/angular-l10n/releases)

---

Expand Down
9 changes: 4 additions & 5 deletions docs/spec/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Method | Function
## L10nLoader
Method | Function
------ | --------
`load(): Promise<void>` | Loads l10n services
`load(): Promise<any>` | Loads l10n services

---

Expand Down Expand Up @@ -60,23 +60,22 @@ Method | Function
`getCurrentTimezone(): string` |
`setCurrentLanguage(languageCode: string): void` |
`setDefaultLocale(languageCode: string, countryCode?: string, scriptCode?: string, numberingSystem?: string, calendar?: string): void` |
`setCurrentNumberingSystem(numberingSystem: string): void` |
`setCurrentCalendar(calendar: string): void` |
`setCurrentCurrency(currencyCode: string): void` |
`setCurrentTimezone(zoneName: string): void` |
`composeLocale(codes: ISOCode[]): string` |
`rollback(): void` |

---

## ITranslationService
Property | Value
-------- | -----
`translationError: Subject<any>` |
`translationError: Subject<any>` | Fired when the translation data could not been loaded. Returns the error

Method | Function
------ | --------
`getConfiguration(): TranslationConfig` |
`init(): Promise<void>` |
`init(): Promise<any>` |
`translationChanged(): Observable<string>` | Fired when the translation data has been loaded. Returns the translation language
`translate(keys: string | string[], args?: any, lang?: string): string | any` | Translates a key or an array of keys
`translateAsync(keys: string | string[], args?: any, lang?: string): Observable<string | any>` |
Expand Down
104 changes: 74 additions & 30 deletions docs/spec/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ Property | Value
-------- | -----
`translationData?: Array<{ languageCode: string; data: any; }>` | Direct loading: adds translation data
`providers?: any[]` | Asynchronous loading: adds translation providers
`caching?: Boolean` | Asynchronous loading: disables/enables the cache for translation providers
`version?: string` | Asynchronous loading: adds the query parameter 'ver' to the http requests
`timeout?: number` | Asynchronous loading: sets a timeout in milliseconds for the http requests
`caching?: Boolean` | Asynchronous loading: disables/enables the cache for translation providers. Provide it only at the root level
`version?: string` | Asynchronous loading: adds the query parameter `ver` to the http requests. Provide it only at the root level
`timeout?: number` | Asynchronous loading: sets a timeout in milliseconds for the http requests. Provide it only at the root level
`rollbackOnError?: boolean` | Asynchronous loading: rollbacks to previous default locale, currency and timezone on error
`composedLanguage?: ISOCode[]` | Sets a composed language for translations
`missingValue?: string | ((path: string) => string)` | Sets the value or the function to use for missing keys
`missingValue?: string | ((path: string) => string)` | Sets the value or the function to use for missing keys. Provide it only at the root level
`missingKey?: string` | Sets the key to use for missing keys
`composedKeySeparator?: string` | Sets composed key separator
`i18nPlural?: boolean` | Disables/enables the translation of numbers that are contained at the beginning of the keys
Expand Down Expand Up @@ -273,7 +274,7 @@ const l10nConfig: L10nConfig = {
};
```

> You can't use Direct and Asynchronous loading at the same time.
> You can use Direct and Asynchronous loading at the same time. Translation data of Direct loading will be merged before the data of Asynchronous loading.
### Asynchronous loading through a Web API
You can also load the data through a Web API:
Expand All @@ -289,14 +290,16 @@ const l10nConfig: L10nConfig = {
...
export class AppModule {
constructor(private translation: TranslationService) {
this.translation.translationError.subscribe((error: any) => console.log(error));
this.translation.translationError.subscribe((error) => {
if (error) {
console.log(error);
}
});
}
}
```
`[path]{languageCode}` will be the URL used by the Http GET requests. So the example URI will be something like: `http://localhost:54703/api/values/en`.

The example above also showed as you can perform a custom action if you get a bad response.

### Using fallback providers
if you need a cascade fallback when the key is not found, you can use fallback providers:
```TypeScript
Expand Down Expand Up @@ -352,6 +355,68 @@ See also [TranslationProvider](https://github.com/robisim74/angular-l10n/blob/ma

---

## Caching
You can enable the cache during configuration:
```TypeScript
const l10nConfig: L10nConfig = {
...
translation: {
providers: [
{ type: ProviderType.Static, prefix: './assets/global-' },
{ type: ProviderType.Static, prefix: './assets/locale-' }
],
caching: true
}
};
```
The next time a translation file will be required, will be taken from the cache without making a new _http request_, with a significant performance improvement:

* if the user returns to a language already selected;
* if you use a global file shared across _lazy loaded modules_.

---

## Error handling of data loading
To find out if an error occurred during asynchronous loading of translation data, you have the `translationError` event:
```TypeScript
...
export class AppComponent {
constructor(private translation: TranslationService) {
this.translation.translationError.subscribe((error) => {
if (error) {
console.log(error);
}
});
}
}
```
If the error occurs on the first loading of the application or a lazy loaded module, you can catch it with the `load` method:
```TypeScript
export class AppModule {
constructor(public l10nLoader: L10nLoader) {
this.l10nLoader.load()
.catch(err => console.error(err));
}
}
```

> If you use _advanced initialization_, you can catch it with the `bootstrapModule` method in `main.ts`.
### Rollback on errror
If the error occurs when the user changes language, you can enable `rollbackOnError` option during the configuration:
```TypeScript
const l10nConfig: L10nConfig = {
...
translation: {
...
rollbackOnError: true
}
};
```
In this way, the application will keep the previous locatization without changing settings.

---

## Using a composed language
By default, the `languageCode` is added as extension to the translation files. If you set `composedLanguage` during the configuration, the combination of supplied codes will be used as language:
```TypeScript
Expand Down Expand Up @@ -461,27 +526,6 @@ That's because not all browsers return `languageCode-countryCode`.

---

## Caching
You can enable the cache during configuration:
```TypeScript
const l10nConfig: L10nConfig = {
...
translation: {
providers: [
{ type: ProviderType.Static, prefix: './assets/global-' },
{ type: ProviderType.Static, prefix: './assets/locale-' }
],
caching: true
}
};
```
The next time a translation file will be required, will be taken from the cache without making a new _http request_, with a significant performance improvement:

* if the user returns to a language already selected;
* if you use a global file shared across _lazy loaded modules_.

---

## Intl API
To localize _dates and numbers_, this library uses the [Intl API](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Intl).

Expand Down Expand Up @@ -591,7 +635,7 @@ const l10nConfig: L10nConfig = {
};
```

### Setting the locale in _Accept-Language_ header on outgoing requests
## Setting the locale in _Accept-Language_ header on outgoing requests
To set the locale in _Accept-Language_ header on all outgoing requests, provide the _localeInterceptor_ option during the configuration:
```TypeScript
const l10nConfig: L10nConfig = {
Expand Down
22 changes: 22 additions & 0 deletions docs/spec/getting-the-translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ Using _directives_:

---

## Pluralization
The library implements pluralization through the official [i18nPluralPipe](https://angular.io/api/common/I18nPluralPipe). Just add to it the translate pipe:
```Html
<p>{{ messages.length | i18nPlural:messageMapping | translate:lang }}</p>
```
or the directive:
```Html
<p l10nTranslate>{{ messages.length | i18nPlural:messageMapping }}</p>
```
and enable `i18nPlural` option during the configuration:
```TypeScript
const l10nConfig: L10nConfig = {
...
translation: {
...
i18nPlural: true
}
};
```

---

## Getting the translation in component class
### Messages
To get the translation in component class, `TranslationService` has the following methods:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-l10n",
"private": true,
"version": "6.5.0",
"version": "6.6.1",
"description": "An Angular library to translate messages, dates and numbers",
"main": "./bundles/angular-l10n.umd.js",
"module": "./fesm5/angular-l10n.js",
Expand Down

0 comments on commit 37c3b7a

Please sign in to comment.