src/modules/language-selector/components/language-selector.component.ts
Component to select the application's language from a list of available languages passed as parameter.
OnInit
OnDestroy
changeDetection | ChangeDetectionStrategy.OnPush |
encapsulation | ViewEncapsulation.None |
host | { |
selector | stark-language-selector |
templateUrl | ./language-selector.component.html |
Properties |
|
Methods |
|
Inputs |
Public
constructor(appMetadata: StarkApplicationMetadata, logger: StarkLoggingService, sessionService: StarkSessionService, dateAdapter: DateAdapter
|
||||||||||||||||||||||||||||||||
Class constructor
Parameters :
|
languageSelectorId | |
Type : string
|
|
Default value : ""
|
|
HTML id of the language selector component. |
mode | |
Type : StarkLanguageSelectorMode
|
|
Default value : "dropdown"
|
|
In which 'mode' the language selector is displayed: dropdown / toolbar |
selectedLanguage | |
Type : string
|
|
Default value : "undefined"
|
|
The currently selected language |
color | |
Type : string
|
|
Inherited from
AbstractStarkUiComponent
|
|
Defined in
AbstractStarkUiComponent:16
|
|
Color theme |
Public changeLanguage | ||||||||
changeLanguage(language: string)
|
||||||||
Change the current language based on the selection made by the user
Parameters :
Returns :
void
|
Public ngOnDestroy |
ngOnDestroy()
|
Component lifecycle hook
Returns :
void
|
Public ngOnInit |
ngOnInit()
|
Inherited from
AbstractStarkUiComponent
|
Defined in
AbstractStarkUiComponent:96
|
Component lifecycle hook
Returns :
void
|
Public appMetadata |
Type : StarkApplicationMetadata
|
Decorators :
@Inject(STARK_APP_METADATA)
|
- The Metadata of the application the contains the supportedLanguages
|
Public dateAdapter |
Type : DateAdapter<any>
|
- Needed to change the locale of the DateAdapter
|
Public logger |
Type : StarkLoggingService
|
Decorators :
@Inject(STARK_LOGGING_SERVICE)
|
- The `StarkLoggingService` instance of the application.
|
Public sessionService |
Type : StarkSessionService
|
Decorators :
@Inject(STARK_SESSION_SERVICE)
|
- The `StarkSessionService` instance of the application.
|
Private sessionServiceSubscription |
Type : Subscription
|
A reference to the sessionService subscription, needed to unsubscribe upon destroy. |
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Inject,
Input,
OnDestroy,
OnInit,
Renderer2,
ViewEncapsulation
} from "@angular/core";
import { DateAdapter } from "@angular/material/core";
import { Subscription } from "rxjs";
import {
STARK_APP_METADATA,
STARK_LOGGING_SERVICE,
STARK_SESSION_SERVICE,
StarkApplicationMetadata,
StarkLanguage,
StarkLoggingService,
StarkSessionService
} from "@nationalbankbelgium/stark-core";
import { AbstractStarkUiComponent } from "@nationalbankbelgium/stark-ui/src/internal-common";
/**
* @ignore
*/
const componentName = "stark-language-selector";
export type StarkLanguageSelectorMode = "dropdown" | "toolbar";
/**
* Component to select the application's language from a list of available languages passed as parameter.
*/
@Component({
selector: "stark-language-selector",
templateUrl: "./language-selector.component.html",
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
// We need to use host instead of @HostBinding: https://github.com/NationalBankBelgium/stark/issues/664
host: {
class: componentName
}
})
export class StarkLanguageSelectorComponent extends AbstractStarkUiComponent implements OnInit, OnDestroy {
/**
* HTML id of the language selector component.
*/
@Input()
public languageSelectorId = "";
/**
* In which 'mode' the language selector is displayed: dropdown / toolbar
*/
@Input()
public mode: StarkLanguageSelectorMode = "dropdown";
/**
* The currently selected language
*/
@Input()
public selectedLanguage = "undefined";
/**
* A reference to the sessionService subscription, needed to unsubscribe upon destroy.
*/
private sessionServiceSubscription!: Subscription;
/**
* Class constructor
* @param appMetadata - The Metadata of the application the contains the supportedLanguages
* @param logger - The `StarkLoggingService` instance of the application.
* @param sessionService - The `StarkSessionService` instance of the application.
* @param dateAdapter - Needed to change the locale of the DateAdapter
* @param renderer - Angular `Renderer2` wrapper for DOM manipulations.
* @param elementRef - Reference to the DOM element where this component is attached to.
* @param cdRef - Reference to the change detector attached to this component.
*/
public constructor(
@Inject(STARK_APP_METADATA) public appMetadata: StarkApplicationMetadata,
@Inject(STARK_LOGGING_SERVICE) public logger: StarkLoggingService,
@Inject(STARK_SESSION_SERVICE) public sessionService: StarkSessionService,
public dateAdapter: DateAdapter<any>,
renderer: Renderer2,
elementRef: ElementRef,
protected cdRef: ChangeDetectorRef
) {
super(renderer, elementRef);
}
/**
* Component lifecycle hook
*/
public override ngOnInit(): void {
super.ngOnInit();
this.logger.debug(componentName + ": controller initialized");
this.sessionServiceSubscription = this.sessionService.getCurrentLanguage().subscribe(
(language: string) => {
this.selectedLanguage = language;
this.cdRef.detectChanges(); // necessary because of the ChangeDetectionStrategy.OnPush
},
() => this.logger.error(componentName + ": an error occurred getting the current language.")
);
}
/**
* Component lifecycle hook
*/
public ngOnDestroy(): void {
if (this.sessionServiceSubscription) {
this.sessionServiceSubscription.unsubscribe();
}
}
/**
* Change the current language based on the selection made by the user
* @param language - The language to change to
*/
public changeLanguage(language: string): void {
if (this.selectedLanguage !== language) {
this.logger.debug(componentName + ": setting session current language => " + language);
this.selectedLanguage = language;
this.sessionService.setCurrentLanguage(this.selectedLanguage);
const languageIndex: number = this.appMetadata.supportedLanguages.findIndex(
(starkLanguage: StarkLanguage) => starkLanguage.code === this.selectedLanguage
);
if (languageIndex >= 0) {
const locale: string = this.appMetadata.supportedLanguages[languageIndex].isoCode;
this.dateAdapter.setLocale(locale);
this.logger.debug(componentName + ": locale changed to => " + locale);
}
}
}
/**
* @ignore
*/
public trackLanguage(_index: number, language: StarkLanguage): string {
return language.isoCode;
}
}
<div *ngIf="mode === 'dropdown'">
<mat-form-field>
<stark-dropdown
[dropdownId]="languageSelectorId"
[options]="appMetadata.supportedLanguages"
[value]="selectedLanguage"
[color]="color"
placeholder=""
optionIdProperty="code"
optionLabelProperty="translationKey"
(selectionChanged)="changeLanguage($event)"
>
</stark-dropdown>
</mat-form-field>
</div>
<div *ngIf="mode === 'toolbar'">
<mat-button-toggle-group name="languageSelector" [value]="selectedLanguage">
<mat-button-toggle
*ngFor="let language of appMetadata.supportedLanguages; trackBy: trackLanguage"
[id]="languageSelectorId + '-' + language.isoCode"
[value]="language.code"
(click)="changeLanguage(language.code)"
>{{ language.code }}
</mat-button-toggle>
</mat-button-toggle-group>
</div>