toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
642 stars 106 forks source link

The resolvers don't work #491

Closed NeoKms closed 1 year ago

NeoKms commented 1 year ago

There are two languages. English (en) and French (fr). The default is French. I wrote my own resolver (just for clarity, I tried everything - none of them work). The resolver always returns "en". Everything is exactly translated into "fr".

Resolver:

import { ExecutionContext, Injectable } from '@nestjs/common';
import { I18nResolver } from 'nestjs-i18n';

@Injectable()
export class MyResolver implements I18nResolver {

  resolve(context: ExecutionContext) {
    console.log('in resolver');
    return 'en';
  }
}

Module:

I18nModule.forRoot({
      fallbackLanguage: 'fr',
      loaderOptions: {
        path: pathModule.join(__dirname, '/i18n/'),
        watch: true,
      },
      resolvers: [
        new MyResolver()
      ],
      typesOutputPath: pathModule.join(
        __dirname,
        '../src/i18n/i18n.generated.ts',
      ),
    }),

I get a console output that the resolver worked. nestjs version 9.4.0 Repo with simple proj: https://github.com/NeoKms/i18ntest (npm i && npm run start:dev) Route: [post]/auth/sendCode with payload {"email": "test@gmail.com"}

ilker0 commented 1 year ago

I have same problem. Did you resolve this problem ? @NeoKms

NeoKms commented 1 year ago

I have same problem. Did you resolve this problem ? @NeoKms

No, I didn't

rubiin commented 1 year ago

Its because you are using i18nservice for transaltion. Its not request scoped so , it has no way of accessing the language the resolver has resolved to. Update the code to throw new BadRequestException(this.i18n.t('auth.errors.wrong_login_or_password',{ lang: I18nContext.current().lang}))

toonvanstrijp commented 1 year ago

@rubiin correct me ff I'm wrong, but maybe we can improve the I18nService to grab the I18nContext.current().lang if not language is provided? This way you don't have to do it manually?

rubiin commented 1 year ago

@toonvanstrijp yeah we can do it. I have updated the docs section too . I will make a pr for it. in next release.