![]() ![]() So in this application, for better demonstration of the implementation, we will work with both Views and API Controllers. I wil be using Visual Studio 2019 as my IDE. ![]() We will build an ASP.NET Core MVC Application and try to implement Localization to it. To be on the safer side, it’s good to build your applicaiton with multi-language support right from the beginning, yeah? Trust me, you do not want to be in that place. Down the line, there can be an instance where you have already completed the application, but suddenly it needs to be multilingual as well. According to me, it is highly important to make your application Multi-Lingual, if you are not able to anticipate the future of your application. One of them is, “Will our Application be Multi-Lingual?”. While starting to build an ASP.NET Core application, or any other application, There are few considerations to take care of. You can see the complete source code of this implementation on my Github. We will also do some advanced configuration where we store the Selected Language Information to the Cookie in the client browser. We will discuss in detail, Globalization and Localization in ASP.NET Core Application and go through various approaches on changing the Culture of the Application via Request. In this article, we will go through a less-talked about topic in the ASP.NET Core Community. The following code shows how to use one resource string for validation attributes with multiple classes: public void ConfigureServices(IServiceCollection services) How to use one resource string for multiple classes Public BookController(IHtmlLocalizer localizer) Private readonly IHtmlLocalizer _localizer In the following highlighted code, only the value of the name parameter is HTML-encoded. IHtmlLocalizer HTML-encodes arguments that are formatted in the resource string, but doesn't HTML-encode the resource string itself. Use the IHtmlLocalizer implementation for resources that contain HTML. Other developers prefer the traditional work flow as it can be easier to work with long string literals and easier to update localized strings. resx file and simply wrapping the string literals can reduce the overhead of localizing an app. For many developers, the new workflow of not having a default language. You develop an app with your default language and prepare it for the localization step without first creating a default resource file.Īlternatively, you can use the traditional approach and provide a key to retrieve the default language string. ![]() You can leave the default language literal strings in the app and wrap them in the localizer, so that you can focus on developing the app. If the localized value of "About Title" isn't found, then the indexer key is returned, that is, the string "About Title". In the preceding code, the IStringLocalizer implementation comes from Dependency Injection. Public AboutController(IStringLocalizer localizer) Private readonly IStringLocalizer _localizer Public class AboutController : Controller The following code example shows how to wrap the string "About Title" for localization. You can develop an app targeted for localization and not need to create resource files early in development. IStringLocalizer doesn't require storing the default language strings in a resource file. The interface has an indexer and an IEnumerable for returning localized strings. IStringLocalizer uses the ResourceManager and ResourceReader to provide culture-specific resources at run time. IStringLocalizer and IStringLocalizer were architected to improve productivity when developing localized apps. One task for localizing an app is to wrap localizable content with code that facilitates replacing that content for different cultures. By Rick Anderson, Damien Bowden, Bart Calixto, Nadeem Afana, and Hisham Bin Ateya
0 Comments
Leave a Reply. |