File

src/app/core/language/language-select/language-select.component.ts

Description

Shows a dropdown-menu of available languages

Metadata

Index

Properties
Methods
Inputs

Constructor

constructor(location: Location)
Parameters :
Name Type Optional
location Location No

Inputs

availableLocales
Type : ConfigurableEnumValue[]
Default value : []

Methods

changeLocale
changeLocale(lang: string)
Parameters :
Name Type Optional
lang string No
Returns : void

Properties

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%;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""