toonvanstrijp / nestjs-i18n

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

this.i18n.translate translate is undefiend #72

Closed amrelshinawy1 closed 4 years ago

amrelshinawy1 commented 4 years ago

TypeError: Cannot read property 'test.HELLO' of undefined at I18nService.translate (E:\elephant-tech\projects\theelephanttech-backend-boilerplate\node_modules\nestjs-i18n\dist\services\i18n.service.js:48:49)

and when theapplication start i got this [Nest] 14632 - 11/13/2019, 9:00:12 PM [I18nService] Object: {}

toonvanstrijp commented 4 years ago

Can you show me your project setup and structure?

amrelshinawy1 commented 4 years ago
src -
      i18n-
             en-
                  test.json
     modules-
                  demo-
                          demo.controller.ts   

   app.module.ts
toonvanstrijp commented 4 years ago

can you also show me your app.module.ts?

amrelshinawy1 commented 4 years ago
here is the app.module.ts
import { Module } from "@nestjs/common";
import { CoreModule } from "./core";
import { DemoModule } from "./modules/demo/demo.module";
import { I18nModule, QueryResolver, HeaderResolver } from "nestjs-i18n";
import { join } from "path";

@Module({
    imports: [
        CoreModule,
        DemoModule,
        I18nModule.forRoot({
            path: join(__dirname, "./i18n/"),
            fallbackLanguage: "en",
            filePattern: "*.json",
            resolvers: [new QueryResolver(["lang", "locale", "l"]), new HeaderResolver()],
            saveMissing: true,
        }),
    ],
})
export class AppModule {}
toonvanstrijp commented 4 years ago

@amrelshinawy1 can you do a console.log(join(__dirname, "./i18n/")) before the @Module and send me the output?

toonvanstrijp commented 4 years ago

@amrelshinawy1 also send me the contents of test.json

amrelshinawy1 commented 4 years ago
\dist\i18n\
test.json
{
    "HELLO": "Hello",
    "PRODUCT": {
        "NEW": "New Product: {name}"
    },
    "ENGLISH": "English",
    "ARRAY": [
        "ONE",
        "TWO",
        "THREE"
    ],
    "cat": "Cat",
    "ONLY_EN_KEY": "this key only exists in en lang"
}
toonvanstrijp commented 4 years ago

@amrelshinawy1 did you copy you i18n folder to you dist folder?

Try this path in development:

path: join(__dirname, "../src/i18n/")

amrelshinawy1 commented 4 years ago

yes i added i18n folder to the dist

also this path: join(__dirname, "../src/i18n/") didnt work

toonvanstrijp commented 4 years ago

@amrelshinawy1 can you zip your project or send me the repo so I can test it myself? It's kind hard to debug this way :p

amrelshinawy1 commented 4 years ago

backend.zip

toonvanstrijp commented 4 years ago

@amrelshinawy1 I downloaded your zip and started the server.

I went to http://localhost:7000/api/demo/ and it shows me Hello as it should! So I can reproduce this error. If you can give me exact instructions how to reproduce this issue I can help you.

I'm going to close this for now.

amrelshinawy1 commented 4 years ago

yes it worked also with me maybe after i removed nodemodules thank you

loxy commented 4 years ago

We had the same problem. Turned out that the promise in parse.ts was rejected due to: https://github.com/ToonvanStrijp/nestjs-i18n/blob/241ec1003726be39a4577b6c5a0d472b241536a7/src/lib/utils/parse.ts#L17 isFile seems to be undefined in Node 8. And we are still using Node 8 in production. But its time to switch I think. Nevertheless but you should wrap a try catch around some Node file handling and reject with a meaningful error. That is the reason that it logs an empty object... @amrelshinawy1 Did you switch to Node > 8 when your error was disappearing?

I can make a pull request by the way...

toonvanstrijp commented 4 years ago

@loxy feel free to create a pull request! If you can make it backwards compatible with Node 8 would be great! (I can configure travis to test node 8 as well).

toonvanstrijp commented 4 years ago

@loxy I'm working on a fix right now so you don't need to create a pull request ;)

loxy commented 4 years ago

Ok, nice!

toonvanstrijp commented 4 years ago

@loxy fixed and released in V4.1.3! :tada: