Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not translate and alway return default value #681

Open
5 tasks done
LeCongNam opened this issue Dec 23, 2024 · 0 comments
Open
5 tasks done

Do not translate and alway return default value #681

LeCongNam opened this issue Dec 23, 2024 · 0 comments

Comments

@LeCongNam
Copy link

LeCongNam commented Dec 23, 2024

Describe the bug

I using

  • @nestjs/cli": "^10.0.0" .
  • "nestjs-i18n": "^10.5.0" .
    I using I18n in HttpException filter all global. But I18n not translate and alway return default value. I using i18n.t() and i18n.translate(). When i using lang:'debug' key return correct. Please help me. Thank you so much
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  private _logger = new Logger(HttpExceptionFilter.name);
  private _translatePathPrefix = I18N_FOLDERS.MESSAGE; // Tiền tố của tệp (ví dụ "message")

  public async catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();
    const status = exception.getStatus();
    const {
      code,
      message,
      data = null,
      module,
    }: ResponseException = exception.getResponse() as ResponseException;

    this._logger.error('Unhandled exception', exception.getResponse());

    const i18n = I18nContext.current<I18nService>(host);
    const lang = request.headers['accept-language'] || i18n.lang; // Nếu không có, fallback sang 'en'

    let translatedMessage: string;
    console.log({
      lang,
      txt: `${this._translatePathPrefix}.${code || 'some_thing_went_wrong'}`,
    });

    try {
      const key = `${this._translatePathPrefix}.${code || 'some_thing_went_wrong'}`;

      translatedMessage = await i18n.translate(key as any, {
        args: data, 
        lang: 'vi', 
        defaultValue: message || 'Something went wrong!',
      });
    } catch (error: any) {
      this._logger.error('Error translating message', error);
      console.error(error);

      translatedMessage = message || 'Something went wrong!';
    }
    
    ```
    APP MODULE:  I18nModule.forRoot({
      fallbackLanguage: 'en',
      loaderOptions: {
        path: join(__dirname, '/i18n/'),
        watch: true,
      },
      resolvers: [
        { use: QueryResolver, options: ['lang', 'locale', 'l'] },
        AcceptLanguageResolver,
      ],
    }),
    
 NESTJS-CLI.JSON {
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "assets": [
      {
        "include": "i18n/**/*",
        "watchAssets": true,
        "outDir": "dist/src/i18n"
      }
    ]
  }
}
i setup folder like homepage
src/i18n/vi/message.json   
{
"some_thing_went_wrong" : "some content"
}

Reproduction

no

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (12) x64 AMD Ryzen 5 5600G with Radeon Graphics
    Memory: 4.77 GB / 13.54 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
  Browsers:
    Chrome: 131.0.6778.204

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant