toonvanstrijp / nestjs-i18n

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

When using typesafety, type defintions not generated #448

Closed rubiin closed 1 year ago

rubiin commented 1 year ago

Trying to use type definitions

  1. created a folder called generated on the same level as app.module.ts
  2. Modified the code as below:

import { Module } from '@nestjs/common'; import * as path from 'path'; import { I18nModule } from 'nestjs-i18n';

@Module({ imports: [ I18nModule.forRoot({ fallbackLanguage: 'en', loaderOptions: { path: path.join(dirname, '/i18n/'), watch: true, }, typesOutputPath: path.join(dirname, '../src/generated/i18n.generated.ts'), }), ], controllers: [], }) export class AppModule {}


When running the app, `generated` folder is still empty with no file as `i18n.generated.ts` 
toonvanstrijp commented 1 year ago

@rubiin this generated folder should be created automatically. Just tried running this again at one of my projects that is using it and it seems to work just fine.

Here is my configuration:

 I18nModule.forRootAsync({
      useFactory: (config: ConfigService) => ({
        fallbackLanguage: config.get<string>('DEFAULT_LANGUAGE'),
        loaderOptions: {
          path: path.join(__dirname, '/i18n/'),
          watch: true,
        },
        typesOutputPath: path.join(
          __dirname,
          '../src/generated/i18n.generated.ts',
        ),
      }),
      inject: [ConfigService],
      resolvers: [
        { use: QueryResolver, options: ['lang'] },
        { use: HeaderResolver, options: ['x-lang'] },
        AcceptLanguageResolver,
      ],
    }),

Do you have any translation files in place? Did you also modify the nest-cli.json to look like this? Otherwise nest won't copy the translations files to the dist directory.

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [
      { "include": "i18n/**/*", "watchAssets": true }
    ]
  }
}
36ru commented 1 year ago
 /* DO NOT EDIT, file generated by nestjs-i18n */

import "nestjs-i18n" from ;
export
type <> = ;
export
type <> = ;

I have this generated

toonvanstrijp commented 1 year ago

@36ru can you create a small sample repo that demonstrates this issue?

For me it generates this:

/* DO NOT EDIT, file generated by nestjs-i18n */

import { Path } from "nestjs-i18n";
export type I18nTranslations = {
    "auth": {
        "modules": {
            "missing": string;
        };
    };
    "common": {
        "exceptions": {
            "unauthorized": string;
            "not-found": string;
        };
    };
    "user": {
        "exists": string;
    };
};
export type I18nPath = Path<I18nTranslations>;
36ru commented 1 year ago

nest-cli.json

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [
      { "include": "i18n/**/*", "watchAssets": true }
    ]
  }
}

app.module.ts

import { Module } from '@nestjs/common';
import * as path from 'path';
import { I18nModule } from 'nestjs-i18n';

@Module({
  imports: [
    I18nModule.forRoot({
      fallbackLanguage: 'ru',
      fallbacks: {
        'ru-*': 'ru',
        'en-*': 'en'
      },
      loaderOptions: {
        path: path.join(__dirname, '/i18n/'),
        watch: true,
      },
      typesOutputPath: path.join(
        __dirname,
        '../src/generated/i18n.generated.ts',
      ),
      resolvers: [
        { use: QueryResolver, options: ['lang'] },
        AcceptLanguageResolver,
      ],
    }),
  ],
  controllers: [],
})
export class AppModule {}
toonvanstrijp commented 1 year ago

@36ru and the translation files please? :)

36ru commented 1 year ago

/src/i18n/en/messages.json

{
  "HELLO": "Hello"
}
36ru commented 1 year ago

And after launching the application , I get errors accordingly

src/generated/i18n.generated.ts:7:7 - error TS1109: Expression expected.

7 type <> = ; ~

toonvanstrijp commented 1 year ago

@36ru just tried you configuration and for me it's working fine.

Can you try this zip: test.zip

What version of nodejs and typescript are you using?

36ru commented 1 year ago

nodejs v18.13.0 typescript in project 4.6.3

toonvanstrijp commented 1 year ago

@36ru did you try the test.zip? Just tried running it again with nodejs v18.13.0 and typescript 4.6.3. Which run command do you use to start your project?

36ru commented 1 year ago

npm run start:dev

36ru commented 1 year ago

Your code created a regular file for me..

But in my project I couldn't create it. I'll deal with it.

36ru commented 1 year ago

Thank you for answering =)

toonvanstrijp commented 1 year ago

@rubiin does this also solve your issues?

rubiin commented 1 year ago

still an issue path.join(__dirname+"/generated/i18n.generated.ts") doesnot work. Shows no changes on

rubiin commented 1 year ago

Noticed this generates the file on dist instead of src. This worked instead typesOutputPath: path.join(process.cwd()+"/src/generated/i18n.generated.ts"),

rubiin commented 1 year ago

Also can you provide me how can i do translation on service side without having to specify the language. The language is being sent as headers

toonvanstrijp commented 1 year ago

@rubiin what kind of setup are you using? A mono repo setup, or just one project? To do translations in your service you can do the following: I18nContext.current<I18nTranslations>('test.HELLO').

rubiin commented 1 year ago

I18nContext.current('test.HELLO')

Argument of type 'string' is not assignable to parameter of type 'ArgumentsHost'.Getting this. Can you update the test app from earliier

rubiin commented 1 year ago

Got it . Should be I18nContext.current<I18nTranslations>().translate('test.HELLO'). Also doing I18nContext.current() seems to be somewhat odd, I think there should be an alias for I18nContext.current() similar to t is for translate. That should make the syntax more appealing

toonvanstrijp commented 1 year ago

@rubiin oops sorry I typed it all wrong! Glad you found it :)

asenyarb commented 1 year ago

Hi, @36ru. I had the exact same problem with the malformed i18n.generated.ts file. Upgrading a typescript library version (I had 4.7.4) to the latest one (currently it is 4.9.4) solved the issue. Hope, it helps!