Skip to content

Commit

Permalink
Transition from getter to observable for the locale class
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Oct 16, 2016
1 parent e92b0ec commit 7eea4f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/pipes/translate.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class TranslatePipe implements PipeTransform {

}

return key;
return "";

}

Expand Down
8 changes: 8 additions & 0 deletions src/services/locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ import { Injectable, EventEmitter, Output } from '@angular/core';
*/
private static referenceCounter: number = 0;

/**
* Output for event default locale changed.
*/
@Output() defaultLocaleChanged: EventEmitter<string> = new EventEmitter<string>(true);

/**
* Output for event current language code changed.
*/
Expand Down Expand Up @@ -547,6 +552,9 @@ import { Injectable, EventEmitter, Output } from '@angular/core';
// Sets the storage "locale".
this.setStorage("locale", this.defaultLocale);

// Sends an event.
this.defaultLocaleChanged.emit(this.defaultLocale);

}

/**
Expand Down
60 changes: 47 additions & 13 deletions src/services/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LocalizationService } from './localization.service';

/**
* Locale superclass.
* Provides the methods for localization.
* Provides the updates for localization.
*
* Extend this class in components to provide the necessary methods for localization:
*
Expand All @@ -29,26 +29,60 @@ import { LocalizationService } from './localization.service';
*/
export class Locale {

constructor(public locale?: LocaleService, public localization?: LocalizationService) { }
/**
* Language code of the LocalizationService.
*/
lang: string;

// Gets the language code for the LocalizationService.
public get lang(): string {
/**
* The default locale.
*/
defaultLocale: string;

return this.localization.languageCode;
/**
* The current currency.
*/
currency: string;

}
constructor(public locale?: LocaleService, public localization?: LocalizationService) {

// Gets the default locale.
public get defaultLocale(): string {
if (this.localization != null) {

return this.locale.getDefaultLocale();
this.lang = this.localization.languageCode;

}
// When the language changes, subscribes to the event & updates lang property.
this.localization.translationChanged.subscribe(

// Generator or next.
(language: string) => { this.lang = language; }

);

}

if (this.locale != null) {

this.defaultLocale = this.locale.getDefaultLocale();

// When the default locale changes, subscribes to the event & updates defaultLocale property.
this.locale.defaultLocaleChanged.subscribe(

// Generator or next.
(defaultLocale: string) => { this.defaultLocale = defaultLocale; }

);

this.currency = this.locale.getCurrentCurrency();

// When the currency changes, subscribes to the event & updates currency property.
this.locale.currencyCodeChanged.subscribe(

// Generator or next.
(currency: string) => { this.currency = currency; }

// Gets the current currency.
public get currency(): string {
);

return this.locale.getCurrentCurrency();
}

}

Expand Down
4 changes: 2 additions & 2 deletions src/services/localization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function extend(...args: any[]): any {
/**
* Output for event translation changed.
*/
@Output() translationChanged: EventEmitter<any> = new EventEmitter<any>();
@Output() translationChanged: EventEmitter<string> = new EventEmitter<string>();

/**
* The language code for the service.
Expand Down Expand Up @@ -690,7 +690,7 @@ export function extend(...args: any[]): any {
this.languageCode = language;

// Sends an event for the components.
this.translationChanged.emit(null);
this.translationChanged.emit(language);

}

Expand Down

0 comments on commit 7eea4f1

Please sign in to comment.