In a recent project, which was the first time I’ve used React and Redux in anger, we had a requirement to support 2 different languages, in the .Net backend for emails and PDFs, and in the React frontend.
This approach has the advantage that we can use the redux state to translate the site automatically when a new language is selected, without having to reload any pages. For an application that depends on up-to-date data, and had to operate on por data connections, avoiding reloads is essential.
The redux examples shown here use TypeScript, which certainly helped in our development process, but there’s a lot of gotchas getting the dotnet new react-redux template and many 3rd party react and redux libraries working nicely with TypeScript. You can make it work, but it’s definitely not an out-of-the-box solution in most cases.
The solution consists of 3 parts:
- The Asp.Net Core 2.0 controller to translate the resx data into JSON,
- The Redux configuration to retrieve the data and populate it into the store; and
- A React function that reads the translation from the store and presents it to the user.
The solution below attempts to get the localisation from the user headers, so you’ll need to enable localisation in your Startup.cs , but depending on your use case, you may want save user language settings in the user’s account data, or in a cookie or other storage in their browser.
For the code samples in this post, please see the original on WordPress