toonvanstrijp / nestjs-i18n

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

Error after using forRootAsync #526

Closed ihengshuai closed 1 year ago

ihengshuai commented 1 year ago

Describe the bug

Hi, guys, I want to use configService to load the application configuration, so I use the forRootAsync class to inject configService, but I did not expect an error

// Common.module.ts
import { resolve } from 'path';
import {
  MiddlewareConsumer,
  Module,
  NestModule,
  ValidationPipe,
} from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Configuration } from 'src/config';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
import { I18nModule, I18nService } from 'nestjs-i18n';
import { CustomI18nResolver } from 'src/common/i18n/custom-i18n-resolver';

@Module({
  imports: [
    // 配置
    ConfigModule.forRoot({
      // envFilePath: resolve(__dirname, '../../.env'),
      // ignoreEnvFile: true,
      load: [Configuration],
      validationSchema: Joi.object({
        PORT: Joi.number(),
        NODE_ENV: Joi.string()
          .valid('development', 'production', 'testing')
          .required(),
      }),
    }),
    // i18n
    I18nModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) =>
        Promise.resolve({
          fallbackLanguage: configService.get('DEFAULT_LANGUAGE'),
          loaderOptions: {
            path: resolve(__dirname, '../../i18n/'),
            watch: false,
          },
          fallbacks: {
            'zh-cn': 'zh',
            'zh-*': 'zh_hk',
            zh: 'zh_hk',
            'en-*': 'en',
            'ko-*': 'ko',
            'ja-*': 'ja',
            'es-*': 'es',
          },
          resolvers: [CustomI18nResolver],
          viewEngine: 'ejs',
        }),
    }),
  ],
  exports: [ConfigModule, I18nModule],
  ],
})
export class CommonModule {}

// App.module.ts
import { Module } from '@nestjs/common';
import { CommonModule } from './modules/common/common.module';

@Module({
  imports: [CommonModule], // reference common module
})
export class AppModule {}

When I run the terminal I get the following error:

ERROR [I18nService] No resolvers provided! nestjs-i18n won't workt properly, 
please follow the quick-start guide: https://nestjs-i18n.com/quick-start

Please take a look at this error, thanks

Reproduction

https://github.com/ihengshuai/nestjs-practive/tree/i18n_error

System Info

System:
    OS: macOS 13.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.08 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.19.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.19.3 - /usr/local/bin/npm
    pnpm: 7.17.1 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 114.0.5735.198
    Edge: 110.0.1587.63
    Safari: 16.5

Used Package Manager

yarn

Validations

ihengshuai commented 1 year ago

I will also report an error without injecting configService, and I cannot use useFactory

Susccy commented 1 year ago

same issue? https://github.com/toonvanstrijp/nestjs-i18n/issues/472

put the resolvers prop outside of useFactory

rubiin commented 1 year ago

The resolvers position is wrong. You should put it outside as @Susccy said

import { Module } from '@nestjs/common';
import * as path from 'path';
import {
  AcceptLanguageResolver,
  QueryResolver,
  HeaderResolver,
  CookieResolver,
  I18nJsonLoader,
  I18nModule,
} from 'nestjs-i18n';

@Module({
  imports: [
    I18nModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        fallbackLanguage: "en",
        loaderOptions: {
          path: join(__dirname, "/i18n/"),
          watch: true,
        },
      }),
      resolvers: [
        new QueryResolver(["lang", "l"]),
        new HeaderResolver(["x-custom-lang"]),
        new CookieResolver(),
        AcceptLanguageResolver,
      ],
      inject: [ConfigService],
    }),
  ],
  controllers: [],
})
export class AppModule {}
ihengshuai commented 6 months ago

Thanks, I already have other solutions 🤗