first commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { I18nService } from '../services/i18n.service';
|
||||
|
||||
@Pipe({
|
||||
name: 't',
|
||||
standalone: true,
|
||||
pure: false,
|
||||
})
|
||||
export class TranslatePipe implements PipeTransform {
|
||||
constructor(private i18n: I18nService) {}
|
||||
transform(key: string, paramsOrFallback?: any): string {
|
||||
// If a string is passed as the second arg, treat it as a fallback text.
|
||||
// Otherwise, assume it's a params object for interpolation.
|
||||
const params = (paramsOrFallback && typeof paramsOrFallback === 'object') ? paramsOrFallback : undefined;
|
||||
const fallback = (typeof paramsOrFallback === 'string') ? paramsOrFallback : undefined;
|
||||
const val = this.i18n.t(key, params);
|
||||
// When i18n returns the key unchanged, use the provided fallback if any
|
||||
if (fallback && val === key) return fallback;
|
||||
return val;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user