Closed afarah1 closed 3 months ago
PS: The issue is only in the views.
Using the same sample project, if we empty index.hbd
and change to translate at the controller:
src/app.controller.ts
import {Controller, Get, Render} from '@nestjs/common';
import {I18nService} from 'nestjs-i18n';
@Controller()
export class AppController {
constructor(private readonly i18n: I18nService) { }
@Get()
@Render('index')
getIndex() {
console.log(this.i18n.t('index.hello'));
}
}
It works... Result:
$ npm run start
> test@0.0.1 start
> nest start
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [NestFactory] Starting Nest application...
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [InstanceLoader] I18nModule dependencies initialized +23ms
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [RoutesResolver] AppController {/}: +6ms
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [I18nService] Handlebars helper registered
[Nest] 21422 - 08/12/2024, 7:19:44 PM LOG [NestApplication] Nest application successfully started +1ms
hiya
^C
Turns out the issue is the path must be quoted in the .hbs template, i.e. {{ t 'index.hello' }}
not {{ t index.hello }}
. Maybe the error could be better handled as to not fail with TypeError
.
Describe the bug
A basic "hello world" following the docs fails with hbs as view engine. Steps to reproduce:
src/main.ts
src/app.controller.ts
src/app.module.ts
nest-cli.json
Finally:
Result:
Reproduction
Yes
System Info
Used Package Manager
npm
Validations