toonvanstrijp / nestjs-i18n

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

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

Open kjxbyz opened 1 year ago

kjxbyz commented 1 year 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/StarterHub/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 1 year ago

I will look into it this weekend when I am free

rubiin commented 1 year 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 1 year ago

Need to support ws and socket-io.

ddenizakpinar commented 7 months ago

Is there an update or workaround about this one?

ItsManikantaGopi commented 4 months ago

https://medium.com/@gopimanikanta50/enabling-internationalization-i18n-in-socket-io-gateways-with-nestjs-framework-a60098de7318

I spend some time figuring out, how to integrate it with gateways, above article might help, have a great day.

brizha commented 1 month ago

A solution to support socket.io out of the box lib would be great