Skip to content

Commit

Permalink
Added support for lazy routing & updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Jun 10, 2016
1 parent f74661b commit 3d36762
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/directives/locale-number-validator.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function validateLocaleNumber(locale: LocaleService, digits: string, MIN_

/**
* LocaleNumberValidator class.
* Validates a number by default locale.
*
* @author Roberto Simonetti
*/
Expand Down
3 changes: 2 additions & 1 deletion src/pipes/locale-date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import {IntlSupport} from '../services/Intl-support';

/**
* LocaleDatePipe class.
* Localizes dates.
*
* Getting the local date:
*
* expression | localedate[:defaultLocale[:format]]
*
* where 'expression' is a date object or a number (milliseconds since UTC epoch), 'defaultLocale' is the default locale and 'format' indicates which date/time components to include.
* where 'expression' is a date object or a number (milliseconds since UTC epoch) and 'format' indicates which date/time components to include.
*
* For example, to get the local date, add in the template:
*
Expand Down
7 changes: 5 additions & 2 deletions src/pipes/locale-number.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import {IntlSupport} from '../services/Intl-support';

/**
* LocaleDecimalPipe class.
* Localizes decimal numbers.
*
* Getting the local decimal:
*
* expression | localedecimal[:defaultLocale:[digitInfo]]
*
* where 'expression' is a number, 'defaultLocale' is the default locale and 'digitInfo' has the following format:
* where 'expression' is a number and 'digitInfo' has the following format:
*
* {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
*
Expand Down Expand Up @@ -102,6 +103,7 @@ import {IntlSupport} from '../services/Intl-support';

/**
* LocalePercentPipe class.
* Localizes percent numbers.
*
* Getting the local percentage:
*
Expand Down Expand Up @@ -177,12 +179,13 @@ import {IntlSupport} from '../services/Intl-support';

/**
* LocaleCurrencyPipe class.
* Localizes currencies.
*
* Getting the local currency:
*
* expression | localecurrency[:defaultLocale[:currency[:symbolDisplay[:digitInfo]]]]
*
* where 'currency' is the current currency and 'symbolDisplay' is a boolean indicating whether to use the currency symbol (e.g. $) or the currency code (e.g. USD) in the output.
* where 'symbolDisplay' is a boolean indicating whether to use the currency symbol (e.g. $) or the currency code (e.g. USD) in the output.
*
* For example, to get the local currency, add in the template:
*
Expand Down
3 changes: 3 additions & 0 deletions src/pipes/translate.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {IntlSupport} from '../services/Intl-support';

/**
* TranslatePipe class.
* Translates messages.
*
* Getting the message translation:
*
Expand Down Expand Up @@ -117,6 +118,8 @@ import {IntlSupport} from '../services/Intl-support';

}

return key;

}

}
1 change: 1 addition & 0 deletions src/services/Intl-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* IntlSupport class.
* Provides the methods to check if Intl is supported.
*
* @author Roberto Simonetti
*/
Expand Down
3 changes: 2 additions & 1 deletion src/services/locale-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {IntlSupport} from './Intl-support';

/**
* LocaleParser class.
* Parses a string and returns a number by default locale.
*
* @author Roberto Simonetti
*/
Expand All @@ -24,7 +25,7 @@ export class LocaleParser {
constructor() { }

/**
* Builds the regular expression of a number for the default locale.
* Builds the regular expression for a number by default locale.
*
* @param defaultLocale The default locale
* @param digits The digit info: {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
Expand Down
30 changes: 3 additions & 27 deletions src/services/locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Injectable, EventEmitter, Output} from '@angular/core';

/**
* LocaleService class.
* Defines language, default locale & currency.
*
* Instantiate this class only once in the route component in order to access the data of location from anywhere in the application:
*
Expand Down Expand Up @@ -92,20 +93,18 @@ import {Injectable, EventEmitter, Output} from '@angular/core';
*
* Changing language.
*
* To change language at runtime, call the following methods:
* To change language at runtime, call the following method:
*
* this.locale.setCurrentLanguage(language);
* this.localization.updateTranslation(); // Need to update the translation.
*
* where 'language' is the two-letter or three-letter code of the new language (ISO 639).
*
*
* Changing locale.
*
* To change locale at runtime, call the following methods:
* To change locale at runtime, call the following method:
*
* this.locale.setCurrentLocale(language, country);
* this.localization.updateTranslation(); // Need to update the translation.
*
* where 'language' is the two-letter or three-letter code of the new language (ISO 639)
* and 'country' is the two-letter, uppercase code of the new country (ISO 3166).
Expand Down Expand Up @@ -429,27 +428,6 @@ import {Injectable, EventEmitter, Output} from '@angular/core';

}

/**
* Sets the current country.
*
* @param country The two-letter, uppercase code of the new country
*/
setCurrentCountry(country: string) {

// Checks if the country has changed.
if (this.countryCode != country) {

// Assigns the value & sends an event.
this.countryCode = country;
this.countryCodeChanged.emit(country);

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

}

}

/**
* Sets the current locale.
*
Expand Down Expand Up @@ -522,10 +500,8 @@ import {Injectable, EventEmitter, Output} from '@angular/core';
this.defaultLocale = this.languageCode

this.defaultLocale += this.scriptCode != "" ? "-" + this.scriptCode : "";

this.defaultLocale += this.countryCode != "" ? "-" + this.countryCode : "";

// Builds the extension.
// Adds the 'u' (Unicode) extension.
this.defaultLocale += this.numberingSystem != "" || this.calendar != "" ? "-u" : "";
// Adds numbering system.
Expand Down
1 change: 1 addition & 0 deletions src/services/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {LocalizationService} from './localization.service';

/**
* Locale superclass.
* Provides the methods for localization.
*
* Extend this class in components to provide the necessary methods for localization:
*
Expand Down
33 changes: 23 additions & 10 deletions src/services/localization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {IntlSupport} from './Intl-support';

/**
* LocalizationService class.
* Gets the translation data and performs operations.
*
* Direct loading.
*
Expand Down Expand Up @@ -92,6 +93,14 @@ import {IntlSupport} from './Intl-support';
// Initializes the service state.
this.serviceState = ServiceState.isWaiting;

// When the language changes, subscribes to the event & call updateTranslation method.
this.locale.languageCodeChanged.subscribe(

// Generator or next.
(language: string) => this.updateTranslation(language)

);

}

/**
Expand Down Expand Up @@ -123,14 +132,16 @@ import {IntlSupport} from './Intl-support';

/**
* Gets the json data.
*
* @param language The two-letter or three-letter code of the language
*/
private getTranslation() {
private getTranslation(language: string) {

// Initializes the translation data & the service state.
this.translationData = {};
this.serviceState = ServiceState.isLoading;

var url: string = this.prefix + this.locale.getCurrentLanguage() + '.json';
var url: string = this.prefix + language + '.json';

// Angular 2 Http module.
this.http.get(url)
Expand All @@ -141,7 +152,7 @@ import {IntlSupport} from './Intl-support';
(res: any) => {

// Assigns the observer to the translation data.
this.translationData[this.locale.getCurrentLanguage()] = res;
this.translationData[language] = res;

},

Expand All @@ -155,12 +166,12 @@ import {IntlSupport} from './Intl-support';
// Complete.
() => {

// Updates the language code of the service.
this.languageCode = this.locale.getCurrentLanguage();

// Updates the service state.
this.serviceState = ServiceState.isReady;

// Updates the language code of the service.
this.languageCode = language;

});

}
Expand Down Expand Up @@ -217,21 +228,23 @@ import {IntlSupport} from './Intl-support';

/**
* Updates the language code and loads the translation data for the asynchronous loading.
*
* @param language The two-letter or three-letter code of the language
*/
updateTranslation() {
updateTranslation(language: string = this.locale.getCurrentLanguage()) {

if (this.locale.getCurrentLanguage() != "" && this.locale.getCurrentLanguage() != this.languageCode) {
if (language != "" && language != this.languageCode) {

// Asynchronous loading.
if (this.loadingMode == LoadingMode.Async) {

// Updates the translation data.
this.getTranslation();
this.getTranslation(language);

} else {

// Updates the language code of the service.
this.languageCode = this.locale.getCurrentLanguage();
this.languageCode = language;

// Updates the service state.
this.serviceState = ServiceState.isReady;
Expand Down

0 comments on commit 3d36762

Please sign in to comment.