Globalization (languages and resources)

To prepare you application to meet the challenge support countries and cultures. First thing to do is : Startup.cs
//Add these usings, using System.Globalization; using Microsoft.AspNetCore.Localization; ' ' //In ConfigureServices method add following lines of code (example) services.Configure (options => { var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("da-DK") }; options.DefaultRequestCulture = new RequestCulture("da-DK"); options.SupportedCultures = supportedCultures; options.SupportedCultures = supportedCultures; }); . . services.AddControllersWithViews(); services.AddLocalization(option => option.ResourcesPath = "Resources"); //In Configure method add these lines after calls to app.UseHttpsRedirection() and app.UseStaticFiles() var localizeOptions = app.ApplicationServices.GetService <IOptions<RequestLocalizationOptions>>(); app.UseRequestLocalization(localizeOptions.Value);
Place a folder with the name Resources with subfolder named Controllers f.ex, with two resx files in it, here english and danish CultureInfo codes. In this example simply name it HomeControllerXXXX

Containing these strings:
// With the translation in both english and danish. // To get an accessor _localizer from the Controller and Localizer property from a View ,add these lines of code:
using Microsoft.Extensions.Localization; public class HomeController: Controller { private readonly ILogger<HomeController> _logger; private readonly IStringLocalizer<HomeController> _localizer; public IStringLocalizer<HomeController> Localizer { get { return _localizer; } } public HomeController(ILogger<HomeController> logger, IStringLocalizer<HomeController>localizer) { _logger = logger; _localizer = localizer; } public IActionResult Index() { ViewBag.Date = DateTime.Now; ViewBag.FirstBasicString = _localizer["FirstBasicString"].Value; ViewBag.ThatWasABadBeginning = _localizer["ThatWasABadBeginning"].Value; return View(); }
On the corresponding View-file add the following lines:
@using GlobalizationStuff.Controllers @using Microsoft.AspNetCore.Mvc.Localization @using Microsoft.Extensions.Localization; @inject IStringLocalizer<HomeController> Localizer <div class="text-center"> <h1 class="display-4">Welcome</h1> <span>@ViewBag.Date</span> <div>@ViewBag.FirstBasicString</div> <div>@ViewBag.ThatWasABadBeginning</div> <p>@Localizer["ReadyToRumble"]</p> </div>