File

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

Description

Shows a dropdown-menu of available languages

Metadata

Index

Properties
Methods

Constructor

constructor(translationService: LanguageService, location: Location)
Parameters :
Name Type Optional
translationService LanguageService No
location Location No

Methods

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

Properties

localeEnumId
Default value : LOCALE_ENUM_ID
siteRegionCode
Type : string

The region code of the currently selected language/region

import { ChangeDetectionStrategy, Component, Inject } from "@angular/core";
import { LanguageService } from "../language.service";
import { LOCATION_TOKEN } from "../../../utils/di-tokens";
import { LANGUAGE_LOCAL_STORAGE_KEY } from "../language-statics";
import { MatButtonModule } from "@angular/material/button";
import { MatIconModule } from "@angular/material/icon";
import { MatMenuModule } from "@angular/material/menu";
import { LOCALE_ENUM_ID } from "../languages";
import { ConfigurableEnumDirective } from "../../basic-datatypes/configurable-enum/configurable-enum-directive/configurable-enum.directive";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { MatTooltipModule } from "@angular/material/tooltip";

/**
 * Shows a dropdown-menu of available languages
 */
@Component({
  selector: "app-language-select",
  templateUrl: "./language-select.component.html",
  styleUrls: ["./language-select.component.scss"],
  imports: [
    MatButtonModule,
    MatIconModule,
    MatMenuModule,
    ConfigurableEnumDirective,
    FontAwesomeModule,
    MatTooltipModule,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LanguageSelectComponent {
  localeEnumId = LOCALE_ENUM_ID;
  /**
   * The region code of the currently selected language/region
   */
  siteRegionCode: string;

  constructor(
    private translationService: LanguageService,
    @Inject(LOCATION_TOKEN) private location: Location,
  ) {
    this.siteRegionCode = this.translationService.currentRegionCode();
  }

  changeLocale(lang: string) {
    localStorage.setItem(LANGUAGE_LOCAL_STORAGE_KEY, lang);
    this.location.reload();
  }
}
<button
  mat-icon-button
  [matMenuTriggerFor]="menu"
  matTooltip="Change language"
  i18n-matTooltip
>
  <fa-icon icon="globe" class="toolbar-icon"></fa-icon>
</button>

<mat-menu #menu>
  <a
    *appConfigurableEnum="let lang of localeEnumId"
    (click)="changeLocale(lang.id)"
    mat-menu-item
  >
    <span>{{ lang.label }}</span>
  </a>
</mat-menu>

./language-select.component.scss

.toolbar-icon {
  color: white;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""