Skip to content

Commit

Permalink
Updated LocaleService
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Apr 22, 2016
1 parent 720b228 commit 026c1be
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ import {TranslatePipe} from 'angular2localization/angular2localization';
pipes: [TranslatePipe]
})
```
With `I18nSelectPipe` that displays the string that matches the current value:
With Angular 2 `I18nSelectPipe` that displays the string that matches the current value:
```Html
{{ gender | i18nSelect: inviteMapping | translate }}
```
With `I18nPluralPipe` that pluralizes the value properly:
With Angular 2 `I18nPluralPipe` that pluralizes the value properly:
```Html
{{ messages.length | i18nPlural: messageMapping | translate }}
```
Expand Down Expand Up @@ -123,8 +123,7 @@ export class AppComponent {
constructor(public locale: LocaleService,) {

// Initializes LocaleService.
this.locale.addLanguage('en'); // Required: adds a new language (ISO 639 two-letter code).
this.locale.definePreferredLanguage('en'); // Required: default language.
this.locale.definePreferredLanguage('en'); // Required: default language (ISO 639 two-letter code).

// Default country for date & numbers (ISO 3166 two-letter, uppercase code).
this.locale.definePreferredCountry('US');
Expand Down Expand Up @@ -221,7 +220,7 @@ export class I18nComponent {

}
```
In this way, application performance and memory usage are optimized. See the [demo](http://robisim74.github.io/angular2localization) for a sample code.
In this way, application performance and memory usage are optimized.

### Changing language
To change language at runtime, call the following method:
Expand Down
57 changes: 37 additions & 20 deletions src/services/locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {Injectable} from 'angular2/core';
* constructor(public locale: LocaleService) {
*
* // Initializes LocaleService.
* this.locale.addLanguage('en'); // Required: adds a new language (ISO 639 two-letter code).
* this.locale.addLanguage('en'); // Optional: adds a new language (ISO 639 two-letter code).
* // Add a new language here.
* this.locale.definePreferredLanguage('en', 30); // Required: default language and expiry (No days). If the expiry is omitted, the cookie becomes a session cookie.
*
Expand All @@ -40,11 +40,6 @@ import {Injectable} from 'angular2/core';
*
* }
*
* Also add in the main:
*
* bootstrap(AppComponent, [HTTP_PROVIDERS]);
*
*
* Changing language.
*
* To change language at runtime, call the following method:
Expand Down Expand Up @@ -96,7 +91,7 @@ import {Injectable} from 'angular2/core';
private defaultLocale: string;

/**
* The available languages codes.
* The available language codes.
*/
private languageCodes: Array<string> = [];

Expand All @@ -115,7 +110,7 @@ import {Injectable} from 'angular2/core';
}

/**
* Asynchronous loading: adds a new language.
* Adds a new language.
*
* @params language The two-letter code of the new language
*/
Expand All @@ -126,7 +121,7 @@ import {Injectable} from 'angular2/core';
}

/**
* Direct & asynchronous loading: defines the preferred language.
* Defines the preferred language.
* Selects the current language of the browser if it has been added, else the default language.
*
* @params defaultLanguage The two-letter code of the default language
Expand All @@ -148,7 +143,7 @@ import {Injectable} from 'angular2/core';

browserLanguage = browserLanguage.substring(0, 2); // Gets the two-letter code.

if (this.languageCodes.indexOf(browserLanguage) != -1) {
if (this.languageCodes.length > 0 && this.languageCodes.indexOf(browserLanguage) != -1) {

this.languageCode = browserLanguage;

Expand All @@ -161,8 +156,17 @@ import {Injectable} from 'angular2/core';
// Sets the default locale.
this.setDefaultLocale();

// Sets the cookie "locale".
this.setCookie("locale", this.defaultLocale, this.expiry);
if (this.languageCodes.length > 0) {

// Sets the cookie "locale".
this.setCookie("locale", this.defaultLocale, this.expiry);

}

} else {

// Sets the default locale.
this.setDefaultLocale();

}

Expand All @@ -184,13 +188,22 @@ import {Injectable} from 'angular2/core';

this.countryCode = defaultCountry.toUpperCase();

}
// Sets the default locale.
this.setDefaultLocale();

if (this.languageCodes.length > 0) {

// Sets the cookie "locale".
this.setCookie("locale", this.defaultLocale, this.expiry);

}

// Sets the default locale.
this.setDefaultLocale();
} else {

// Sets the cookie "locale".
this.setCookie("locale", this.defaultLocale, this.expiry);
// Sets the default locale.
this.setDefaultLocale();

}

}

Expand All @@ -208,10 +221,14 @@ import {Injectable} from 'angular2/core';

this.currencyCode = defaultCurrency.toUpperCase();

}
if (this.languageCodes.length > 0) {

// Sets the cookie "currency".
this.setCookie("currency", this.currencyCode, this.expiry);

// Sets the cookie "currency".
this.setCookie("currency", this.currencyCode, this.expiry);
}

}

}

Expand Down
2 changes: 1 addition & 1 deletion src/services/localization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {LocaleService} from './locale.service';
private prefix: string;

/**
* The translations data: {locale: {key: value}}.
* The translations data: {languageCode: {key: value}}.
*/
private translationsData: any = {};

Expand Down

0 comments on commit 026c1be

Please sign in to comment.