All files / lib/services i18n-request-scope.service.ts

100% Statements 11/11
25% Branches 1/4
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 256x 6x 6x     6x       31x 31x   31x         10x       10x      
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { I18nService } from './i18n.service';
 
@Injectable({ scope: Scope.REQUEST })
export class I18nRequestScopeService {
  readonly detectedLanguage: string;
 
  constructor(
    @Inject(REQUEST) private readonly req,
    private readonly i18nService: I18nService,
  ) {
    this.detectedLanguage =
      req.i18nLang || (req.raw ? req.raw.i18nLang : undefined);
  }
 
  public translate(key, options?) {
    options = {
      lang: this.detectedLanguage,
      ...options,
    };
    return this.i18nService.translate(key, options);
  }
}