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

"parsing translation error - { stack: [ {} ] }" in application logs on start #620

Open
5 tasks done
nonamecat19 opened this issue Mar 11, 2024 · 0 comments
Open
5 tasks done

Comments

@nonamecat19
Copy link

Describe the bug

Hi, I saw some strange behavior of the library.
Error 2024-03-11 14:08:43 [I18nService] parsing translation error - { stack: [ {} ] } +1ms
After this error log application working properly. Maybe you have idea how to fix it or suppress the log?

app.module.ts

import { Module } from '@nestjs/common';
import { AcceptLanguageResolver, HeaderResolver } from 'nestjs-i18n';
import { I18nTranslationLoader } from '@core/loaders';

@Module({
  imports: [
    CoreI18nModule.forRoot({
      fallbackLanguage: 'en',
      loader: I18nTranslationLoader,
      loaderOptions: {},
      typesOutputPath: `${appConfig.i18n.types}/i18n.generated.ts`,
      resolvers: [
        new HeaderResolver(['x-lang']),
        AcceptLanguageResolver,
      ],
      translations: {
        en: {
          core: coreEnLocale,
        },
        ua: {
          core: coreUaLocale,
        },
      },
    }),
  ],
})
export class AppModule {}

i18n.module.ts

import { I18nStorage } from '@core/storages';
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { assign, omit } from 'lodash';
import { I18nModule } from 'nestjs-i18n';
import { I18nOptions } from 'nestjs-i18n/dist/interfaces/i18n-options.interface';
import { I18nTranslation } from 'nestjs-i18n/dist/interfaces/i18n-translation.interface';

@Module({})
export class CoreI18nModule {
  private static i18nStorage = new I18nStorage();

  public static forRoot(options: I18nOptions & { translations?: I18nTranslation }): DynamicModule {
    const module = this.getModule(options.translations);
    const i18nStorageProvider = this.getI18nStorageProvider();

    return assign(module, {
      imports: [
        I18nModule.forRoot(omit(options, ['translations'])),
      ],
      providers: [
        i18nStorageProvider,
      ],
      exports: [
        i18nStorageProvider,
      ],
    } as DynamicModule);
  }

  public static forFeature(config?: I18nTranslation): DynamicModule {
    return this.getModule(config);
  }

  protected static getModule(config?: I18nTranslation): DynamicModule {
    if (config) {
      this.i18nStorage.add(config);
    }

    return {
      global: true,
      module: CoreI18nModule,
      imports: [],
      exports: [],
      providers: [],
    };
  }

  private static getI18nStorageProvider(): Provider {
    return {
      provide: I18nStorage,
      useFactory: () => this.i18nStorage,
    };
  }
}

i18n-translation.loader.ts

import { appConfig } from '@app/app.config';
import { ModuleHelper } from '@core/helpers';
import { I18nStorage } from '@core/storages';
import { I18nLoader, I18nTranslation } from 'nestjs-i18n';
import { Observable } from 'rxjs';

export class I18nTranslationLoader extends I18nLoader {
  public async load(): Promise<I18nTranslation | Observable<I18nTranslation>> {
    return (ModuleHelper.get(I18nStorage) as I18nStorage).get();
  }

  public async languages(): Promise<string[] | Observable<string[]>> {
    return appConfig.i18n.languages;
  }
}

Reproduction

Version nestjs-i18n: 10.2.6, same issue on 10.4.5. OS: MacOS, Linux

System Info

System:
    OS: macOS 14.4
    CPU: (8) arm64 Apple M1 Pro
    Memory: 48.27 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v18.17.1/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 8.15.3 - ~/.nvm/versions/node/v18.17.1/bin/pnpm
    bun: 1.0.25 - ~/.bun/bin/bun
    Watchman: 2024.01.22.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 122.0.6261.112
    Edge: 122.0.2365.80
    Safari: 17.4

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