File

src/app/core/language/language.service.ts

Description

Service that provides the currently active locale and applies a newly selected one.

Index

Methods

Constructor

constructor(baseLocale: string, window: Window, siteSettings: SiteSettingsService)
Parameters :
Name Type Optional
baseLocale string No
window Window No
siteSettings SiteSettingsService No

Methods

currentRegionCode
currentRegionCode()

returns the region code of the locale currently used Extracts the region code (i.e. 'de', 'us', 'in') in lowercase letters from a locale (i.e. 'en-US', 'hi-IN')

Returns : string
initDefaultLanguage
initDefaultLanguage()
Returns : void
import { Inject, Injectable, LOCALE_ID } from "@angular/core";
import { LANGUAGE_LOCAL_STORAGE_KEY } from "./language-statics";
import { WINDOW_TOKEN } from "../../utils/di-tokens";
import { SiteSettingsService } from "../site-settings/site-settings.service";

/**
 * Service that provides the currently active locale and applies a newly selected one.
 */
@Injectable({
  providedIn: "root",
})
export class LanguageService {
  constructor(
    @Inject(LOCALE_ID) private baseLocale: string,
    @Inject(WINDOW_TOKEN) private window: Window,
    private siteSettings: SiteSettingsService,
  ) {}

  initDefaultLanguage(): void {
    const languageSelected = this.window.localStorage.getItem(
      LANGUAGE_LOCAL_STORAGE_KEY,
    );

    if (!languageSelected) {
      this.siteSettings.defaultLanguage.subscribe(({ id }) => {
        if (id !== this.baseLocale) {
          // Reload app with default language from config
          this.window.localStorage.setItem(LANGUAGE_LOCAL_STORAGE_KEY, id);
          this.window.location.reload();
        }
      });
    }
  }

  /**
   * returns the region code of the locale currently used
   * Extracts the region code (i.e. 'de', 'us', 'in') in lowercase letters
   * from a locale (i.e. 'en-US', 'hi-IN')
   */
  currentRegionCode(): string {
    const components = this.baseLocale.split("-");
    if (components.length >= 2) {
      return components[1].toLowerCase();
    } else {
      return components[0].toLowerCase();
    }
  }
}

results matching ""

    No results matching ""