I am trying to implement i18n
module in my application. For this i am making a utility that translates the string. It looks like:
import { I18nService } from "nestjs-i18n";
export class UtilsService {
constructor(private readonly i18n: I18nService){}
translateKey(msgKey){
let message = this.i18n.translate(msgKey, {
lang: 'en'
})
return message
}
}
But now when am trying to use this function somewhere else i get an error. What am i doing wrong??
I am trying to call it in a ResponseError like this:
export class ResponseError implements ResponseFormatter {
constructor(infoMessage: string, data?: any, errCode?: number) {
this.success = false;
this.message = infoMessage;
this.data = data;
this.errCode = errCode;
console.log(UtilsService.translateKey(infoMessage))
Logger.warn(
`${new Date().toString()
} - [Response]: ${
infoMessage
}${data ? ` - ${ JSON.stringify(data)}` : ''}`,
);
}
message: string;
data: any[];
errorMessage: any;
error: any;
success: boolean;
errCode: number;
}
You can create a singleton
out of UtilsService
and export that object instead:
import { I18nService } from "nestjs-i18n";
class I18NTranslator {
private i18n!: I18nService;
constructor(){
i18n = new I18nService();
}
translateKey(msgKey){
let message = this.i18n.translate(msgKey, {
lang: 'en'
})
return message
}
}
export const UtilsService = new I18NTranslator();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.