Search code examples
asp.net-coresignalr

Is it possible to get the UI culture of a SignalR client to send localized content?


It's easy enough for me to send messages via SignalR to specific users:

await _hubContext.Clients.User(userIDString).SendAsync("remoteMethod", data);

The challenge is that I can't figure out how to send that message string accounting for the user's culture (en, fr, nl, es, etc). With a straight up hit to a regular endpoint, you just use Resources.PropertyName and it magically works based on the user's browser culture. But if I want to tailor the data above to the user's culture, I don't know how. In fact, if this code fires as a result of a different user, the current UI culture is of that user.


Solution

  • As it turns out, it makes sense that what I'm asking isn't easily possible. Each registered client has little more than an ID to work with. While you could do some magic with an implementation of IUserIdProvider to map userID's to culture, you would then have to maintain extra state to do so, and I didn't want to be in that business.

    My solution was to simply serialize a bunch of resource strings and have the client call them from a controller action. I have the client cache it with [ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)], and the resulting Javascript object is a global variable available to all the things on the client.