Currently, I am working on creating a pipe that will replace specific keywords with the correct strings. To keep this pipe well-structured, I have decided to store my keywords and strings in another file. Below is the code snippet for reference:
import { Injectable } from '@angular/core';
import { AdvEnDictionary } from './advertiser-dict/adv-en-dictionary';
import { AdvFaDictionary } from './advertiser-dict/adv-fa-dictionary';
@Injectable()
export class AdvertiserDictionary {
constructor(
private advEnDictionary: AdvEnDictionary['words'],
private advFaDictionary: AdvFaDictionary['words']
) {}
getString(keyword:string, lang:string) {
switch(lang) {
case 'fa':
return this.advFaDictionary[keyword];
break;
case 'en':
return this.advEnDictionary[keyword];
break;
default:
return this.advFaDictionary[keyword];
}
}
}
Here is the implementation of the pipe:
import { Pipe, PipeTransform } from '@angular/core';
import { CookieService } from 'ngx-cookie';
import { AdvertiserDictionary } from '../classes/advertiser-dictionary';
import { PublisherDictionary } from '../classes/publisher-dictionary';
@Pipe({
name: 'translation'
})
export class TranslationPipe implements PipeTransform {
constructor(private cookieService: CookieService,
private advertiserDictionary: AdvertiserDictionary,
private publisherDictionary: PublisherDictionary,
) {}
transform(value: string, args?: any): any {
var [prefix, keyword] = value.split("-");
var lang = this.cookieService.get("lang");
switch(prefix.toLowerCase()) {
case 'pub':
return this.publisherDictionary.getString(
keyword.toUpperCase(),
lang
);
break;
case 'adv':
return this.advertiserDictionary.getString(
keyword.toUpperCase(),
lang
);
break;
}
}
}
However, I am facing some challenges in different scenarios:
- When I simply add the class in the pipe, I encounter a 'No provider' error for classes.
- When I include the class in module providers, I receive an 'invalid arguments' error for the class. Additionally, I prefer not to have this class available throughout the entire app but only for use in my pipe.