-
-
Notifications
You must be signed in to change notification settings - Fork 137
Open
Description
Hello,
I implemented this library in my project's nestJs Mailer Service using HandleBarsAdapter, but it seems I misconfigured it, because translations works but does not take in account the lang passed to the mail service.
// app.module.ts
...
I18nModule.forRoot({
fallbackLanguage: 'fr',
loaderOptions: { path: join(__dirname, '/i18n/'), watch: true },
throwOnMissingKey: true,
logging: true,
resolvers: [{ use: AcceptLanguageResolver, options: ['lang'] }]
}),
...// mail.module.ts
import { Module } from '@nestjs/common';
import { MailService } from './mail.service';
import { MailController } from './mail.controller';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { ConfigModule } from '@nestjs/config';
import { User } from '@src/users/entities/user.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { join } from 'path';
import { I18nService } from 'nestjs-i18n';
const path = join(__dirname, 'templates');
@Module({
imports: [
ConfigModule.forRoot(),
MailerModule.forRootAsync({
inject: [I18nService],
useFactory: (i18n: I18nService) => ({
transport: {
host: process.env.EMAIL_HOST,
secure: false,
auth: {
user: process.env.EMAIL_SENDER,
pass: process.env.EMAIL_PWD
}
},
defaults: {
from: `"${process.env.EMAIL_PREFIX} No Reply" <${process.env.EMAIL_SENDER}>`
},
template: {
dir: join(path, 'mails'),
adapter: new HandlebarsAdapter({
t: i18n.hbsHelper
}),
options: { strict: true }
},
options: {
partials: {
dir: join(path, 'layouts'),
options: {
strict: true
}
}
}
})
}),
TypeOrmModule.forFeature([User])
],
providers: [MailService],
controllers: [MailController],
exports: [MailService]
})
export class MailModule {}// mail.service.ts
async sendUserInvitationToJoinOrganization(
user: User,
organization: Organization,
token: string,
lang?: string
) {
const subject: string = this.i18nService.translate(
'default.invitation_organization',
{
lang,
args: { organizationName: organization.name }
}
);
await this.mailerService.sendMail({
to: user.email,
subject: `${process.env.EMAIL_PREFIX} ${subject}`,
template: 'invitation',
context: {
name: user.firstName,
url: this.constructUrl('invitation', { token }),
organization,
lang
}
});
}If I switch the fallback language option, internationalization works well !
AI suggests to replace
adapter: new HandlebarsAdapter({
t: i18n.hbsHelper
}),by
t: (key: string, options?: Record<string, any>) => {
const lang = options?.hash?.lang || 'en';
return i18n.translate(key, { lang });
}and it indeed works but it does feel weird, plus I need to add arguments into this, or none of my arguments are interpreted.
If that helps, I can tell you that the option logging: true doesn't seems to work either...
Thanks by advance for your help
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels