src/app/core/language/language-select/language-select.component.ts
Shows a dropdown-menu of available languages
changeDetection | ChangeDetectionStrategy.OnPush |
selector | app-language-select |
imports |
MatSelectModule
|
styleUrls | ./language-select.component.scss |
templateUrl | ./language-select.component.html |
Properties |
Methods |
Inputs |
constructor(location: Location)
|
||||||
Parameters :
|
availableLocales | |
Type : ConfigurableEnumValue[]
|
|
Default value : []
|
|
changeLocale | ||||||
changeLocale(lang: string)
|
||||||
Parameters :
Returns :
void
|
currentLocale |
Type : string
|
import {
ChangeDetectionStrategy,
Component,
Inject,
Input,
} from "@angular/core";
import { LOCATION_TOKEN } from "../../../utils/di-tokens";
import {
DEFAULT_LANGUAGE,
LANGUAGE_LOCAL_STORAGE_KEY,
} from "../language-statics";
import { MatSelectModule } from "@angular/material/select";
import { ConfigurableEnumValue } from "app/core/basic-datatypes/configurable-enum/configurable-enum.types";
/**
* Shows a dropdown-menu of available languages
*/
@Component({
selector: "app-language-select",
templateUrl: "./language-select.component.html",
styleUrls: ["./language-select.component.scss"],
imports: [MatSelectModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LanguageSelectComponent {
@Input() availableLocales: ConfigurableEnumValue[] = [];
currentLocale: string;
constructor(@Inject(LOCATION_TOKEN) private location: Location) {
this.currentLocale =
localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY) || DEFAULT_LANGUAGE;
}
changeLocale(lang: string) {
if (lang === this.currentLocale) return;
localStorage.setItem(LANGUAGE_LOCAL_STORAGE_KEY, lang);
this.location.reload();
}
}
<mat-form-field appearance="fill" class="language-select-dropdown">
<mat-label i18n>Choose your language</mat-label>
<mat-select
[value]="currentLocale"
(selectionChange)="changeLocale($event.value)"
>
@for (locale of availableLocales; track locale.id) {
<mat-option [value]="locale.id">
{{ locale.label }}
</mat-option>
}
</mat-select>
</mat-form-field>
./language-select.component.scss
.language-select-dropdown {
max-width: 320px;
width: 100%;
}