File

projects/core/src/i18n/translate.pipe.ts

Metadata

Name cxTranslate

Methods

Private markForCheck
markForCheck(value: string)
Parameters :
Name Type Optional
value string No
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
transform
transform(input: Translatable | string, options: TranslatableParams)
Parameters :
Name Type Optional Default value
input Translatable | string No
options TranslatableParams No {}
Returns : string
Private translate
translate(key: any, options: object)
Parameters :
Name Type Optional
key any No
options object No
Returns : void

Properties

Private lastKey
Type : string
Private lastOptions
Type : object
Private sub
Type : Subscription
Private translatedValue
Type : string
import {
  ChangeDetectorRef,
  isDevMode,
  OnDestroy,
  Pipe,
  PipeTransform,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { shallowEqualObjects } from '../util/compare-equal-objects';
import { Translatable, TranslatableParams } from './translatable';
import { TranslationService } from './translation.service';

@Pipe({ name: 'cxTranslate', pure: false })
export class TranslatePipe implements PipeTransform, OnDestroy {
  private lastKey: string;
  private lastOptions: object;
  private translatedValue: string;
  private sub: Subscription;

  constructor(
    protected service: TranslationService,
    protected cd: ChangeDetectorRef
  ) {}

  transform(
    input: Translatable | string,
    options: TranslatableParams = {}
  ): string {
    if (!input) {
      if (isDevMode()) {
        console.error(
          `The given input for the cxTranslate pipe (${input}) is invalid and cannot be translated`
        );
      }
      return;
    }

    if ((input as Translatable).raw) {
      return (input as Translatable).raw;
    }

    const key = typeof input === 'string' ? input : input.key;
    if (typeof input !== 'string') {
      options = { ...options, ...input.params };
    }

    this.translate(key, options);
    return this.translatedValue;
  }

  private translate(key: any, options: object) {
    if (
      key !== this.lastKey ||
      !shallowEqualObjects(options, this.lastOptions)
    ) {
      this.lastKey = key;
      this.lastOptions = options;

      if (this.sub) {
        this.sub.unsubscribe();
      }
      this.sub = this.service
        .translate(key, options, true)
        .subscribe((val) => this.markForCheck(val));
    }
  }

  private markForCheck(value: string) {
    this.translatedValue = value;
    this.cd.markForCheck();
  }

  ngOnDestroy(): void {
    if (this.sub) {
      this.sub.unsubscribe();
    }
  }
}

result-matching ""

    No results matching ""