![]() Check if the property name matches the current line While we still got more lines in the JSON file Using (var reader = new JsonTextReader(sReader)) Using (var sReader = new StreamReader(str)) Using (var str = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) Let's read some text from the JSON file If the properte and filepath is null, return null private string? GetJsonValue(string propertyName, string filePath) You can install that one in the Nuget Console with the following command: Install-Package Newtonsoft.Json. Before we can work with JSON we have to include a reference to the Newtonsoft package. This method should take two parameters, one for the property name and one for the path of the file. They accept a key ( in a moment) and are responsible for finding the right value from our localization file (the JSON files).įirst, we should add functionality to get the values from our JSON files. Then we have the entry methods that we will be using from our controllers.First, we use DI to inject IDistributedCache.Public IEnumerable GetAllStrings(bool includeParentCultures) Public LocalizedString this arguments] => throw new NotImplementedException() Public LocalizedString this => throw new NotImplementedException() Public JsonStringLocalizer(IDistributedCache distributedCache) Private readonly IDistributedCache _distributedCache Public class JsonStringLocalizer : IStringLocalizer Inherit IStringLocalizer, implement the interface, and add a constructor for the class to inject IDistributedCache. Let’s name it JsonStringLocalizer.cs as it is JSON strings we will be working with. The first thing we gotta do is create a new folder named Localization and add a new class that inherits IStringLocalizer. Create an extension class for IStringLocalizer To make everything more efficient we will be adding IDistributedCache. The middleware is responsible for determining the language key passed in the request header and IStringLocalizer will be used to make support the JSON files that contain the translation strings. This solution will be made up of two pieces, some middleware, and the implementation of IStringLocalizer. Everything will be in the same project all the way. Create a new Web API in Visual Studioįor demo purposes, I will not be creating an onion architecture or using any patterns to handle business logic. NET 6.0 installed on your development computer to select that framework version. ![]() If you prefer another IDE that’s totally fine. Create a new ASP.NET Core Web API in Visual StudioĪlright, the first thing you have to do is create a new Web API based on the template in Visual Studio. ![]() I can tell you that it only takes three classes and a couple of registrations in our program.cs file to get this service up and running. I will do my best to make it simple for you, by providing step-by-step explanations and explaining my code as we are diving deeper into the logic in the application. To accomplish that we will be implementing cache functionality based on IDistributedCache and IStringLoxalizer to avoid reading the JSON file each time and relying on the cache. Net 6 that will return messages from the API in different languages based on a key supplied in the header. The final resultīy the end of this tutorial, we will have a fully functioning Web API built with. If you are ready to implement localization to your API, then let’s get started. To accomplish this we will be adding new middleware to our application for it to switch language based on a key in the request header. ![]() The localization will be grabbing the strings from a JSON file, that will store and hold the strings for each language. Also, we are going to take a look at how we can cache it to make the response time lower and the consumers happier. In this tutorial, I will be teaching you how to add localization in ASP.NET Core Web APIs. ![]()
0 Comments
Leave a Reply. |