toonvanstrijp / nestjs-i18n

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

In `@WebSocketGateway`, `I18nContext.current()` returns undefined. #568

Open kjxbyz opened 10 months ago

kjxbyz commented 10 months ago

Describe the bug

In @WebSocketGateway, I18nContext.current() returns undefined.

import {
  MessageBody,
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
  WsResponse,
} from '@nestjs/websockets'
import { UseGuards } from '@nestjs/common'
import { I18nContext, I18nService } from 'nestjs-i18n'
import { Server } from 'ws'
import { from, Observable } from 'rxjs'
import { map } from 'rxjs/operators'

@WebSocketGateway({
  path: '/ws',
  cors: {
    origin: '*',
  },
  transports: ['websocket'],
})
export class WsGateway {
  @WebSocketServer()
  server: Server

  constructor(private readonly i18n: I18nService) {}

  @SubscribeMessage('hello')
  hello(client: any, @MessageBody() data: any): string {
    console.log('this.i18n', this.i18n.t, I18nContext.current())
    return this.i18n.t('common.HELLO', { lang: I18nContext.current().lang })
  }

  @SubscribeMessage('hello2')
  hello2(client: any, @MessageBody() data: any): string {
    return this.i18n.t('common.NEW', {
      args: { name: 'Kimmy' },
      lang: I18nContext.current().lang,
    })
  }

  @SubscribeMessage('events')
  findAll(client: any, @MessageBody() data: any): Observable<WsResponse<number>> {
    return from([1, 2, 3]).pipe(
      map((item) => ({ event: 'events', data: item })),
    )
  }

  @SubscribeMessage('identity')
  async identity(@MessageBody() data: number): Promise<number> {
    return data
  }
}

Reproduction

https://github.com/kjxbyz/nest-starter

System Info

System:
    OS: macOS 13.5.2
    CPU: (6) x64 Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
    Memory: 11.50 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.6.7 - /usr/local/bin/npm
    Watchman: 2023.08.14.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 117.0.5938.88
    Safari: 16.6


### Used Package Manager

yarn

### Validations

- [X] Follow our [Code of Conduct](https://github.com/toonvanstrijp/nestjs-i18n/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guide](https://github.com/toonvanstrijp/nestjs-i18n/blob/main/CONTRIBUTING.md).
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
- [X] The provided reproduction is a [minimal reproducible](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
rubiin commented 10 months ago

I will look into it this weekend when I am free

rubiin commented 10 months ago

This is not a bug but rather the websocket implementation has not been done. Currently only websocket with graphql seems to be implemented .

kjxbyz commented 10 months ago

Need to support ws and socket-io.

ddenizakpinar commented 3 months ago

Is there an update or workaround about this one?