Closed rubiin closed 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 }
]
}
}
/* DO NOT EDIT, file generated by nestjs-i18n */
import "nestjs-i18n" from ;
export
type <> = ;
export
type <> = ;
I have this generated
@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>;
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 {}
@36ru and the translation files please? :)
/src/i18n/en/messages.json
{
"HELLO": "Hello"
}
And after launching the application , I get errors accordingly
src/generated/i18n.generated.ts:7:7 - error TS1109: Expression expected.
7 type <> = ; ~
@36ru just tried you configuration and for me it's working fine.
Can you try this zip: test.zip
npm install
npm run start:dev
What version of nodejs and typescript are you using?
nodejs v18.13.0 typescript in project 4.6.3
@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?
npm run start:dev
Your code created a regular file for me..
But in my project I couldn't create it. I'll deal with it.
Thank you for answering =)
@rubiin does this also solve your issues?
still an issue
path.join(__dirname+"/generated/i18n.generated.ts")
doesnot work. Shows no changes on
Noticed this generates the file on dist instead of src.
This worked instead typesOutputPath: path.join(process.cwd()+"/src/generated/i18n.generated.ts"),
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
@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')
.
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
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
@rubiin oops sorry I typed it all wrong! Glad you found it :)
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!
Trying to use type definitions
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 {}