Globalization and Localization in ASP.NET Core MVC 5

On the Visual Studio, select Create a new project from Get Started

Select ASP.NET Core Web Application

Input Project Name and select Location for new project

Select ASP.NET Core 5.0 Version and select ASP.NET Core Empty Template. Click Create button to finish

Open Startup.cs file and add new configurations as below:

using System.Globalization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace LearnASPNETCoreMVC5
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");

            services.AddControllersWithViews()
                    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                    .AddDataAnnotationsLocalization();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("fr-FR"),
                new CultureInfo("de-DE"),
                new CultureInfo("ja-JP")
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en-US"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            });

            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Demo}/{action=Index}/{id?}");
            });
        }
    }
}

Create resource files containing the content used in Globalization and Localization as below:

Create new folder named Resources

Select Resources, right click and select Add/New Item

Select Resources File and input file name is SharedResource.resx

Input content for SharedResource.resx as below:

From SharedResource.resx file copy to new file named SharedResource_de_DE.resx and input content as below:

From SharedResource.resx file copy to new file named SharedResource_en_US.resx and input content as below:

From SharedResource.resx file copy to new file named SharedResource_fr_FR.resx and input content as below:

From SharedResource.resx file copy to new file named SharedResource_ja_JP.resx and input content as below:

Select current project, create a new class with the same name as the resource file named SharedResource.cs as below

namespace LearnASPNETCoreMVC5
{
    public class SharedResource
    {
    }
}

Create new folder named Controllers. In this folder, create new controller named Demo.cs as below:

using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;

namespace LearnASPNETCoreMVC5.Controllers
{
    [Route("demo")]
    public class DemoController : Controller
    {
        private readonly IStringLocalizer<SharedResource> sharedResource;

        public DemoController(IStringLocalizer<SharedResource> _sharedResource)
        {
            sharedResource = _sharedResource;
        }

        [Route("~/")]
        [Route("index")]
        [Route("")]
        public IActionResult Index()
        {
            var msg = sharedResource["Msg"];
            Debug.WriteLine("Msg: " + msg);

            var price = 1234567;
            Debug.WriteLine("Price: " + price.ToString("c"));

            var number = 1234567;
            Debug.WriteLine("Number: " + number.ToString("n"));

            var percent = 1234;
            Debug.WriteLine("Percent: " + percent.ToString("p"));

            var now = DateTime.Now;
            Debug.WriteLine("Date: " + now.ToString("F"));

            ViewBag.price = price;
            ViewBag.number = number;
            ViewBag.percent = percent;
            ViewBag.now = now;

            return View();
        }
    }
}

Create new folder named Views. In this folder, create new folder named Demo and add new views as below:

In Demo folder, create new view named Index.cshtml as below:

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    <a asp-controller="demo" asp-action="index" asp-route-culture="en-US">English</a>
    <a asp-controller="demo" asp-action="index" asp-route-culture="ja-JP">Japanese</a>
    <a asp-controller="demo" asp-action="index" asp-route-culture="fr-FR">French</a>
    <a asp-controller="demo" asp-action="index" asp-route-culture="de-DE">German</a>
    <br /><br />
    @SharedLocalizer["Msg"]
    <br />
    Price: @ViewBag.price.ToString("c")
    <br />
    Number: @ViewBag.number.ToString("n")
    <br />
    Percent: @ViewBag.percent.ToString("p")
    <br />
    Date: @ViewBag.now.ToString("F")

</body>
</html>

Select Views folder and right click to select Add\New Item Menu

Select Web\ASP.NET in left side. Select Razor View Imports item and click Add button to Finish

In _ViewImports.cshtml file and TagHelpers library as below:

@using Microsoft.Extensions.Localization
@using LearnASPNETCoreMVC5

@inject IStringLocalizer<SharedResource> SharedLocalizer

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Access Index action in Account controller with following url: http://localhost:9596/Account/Index

Output

Click Save button submit form to Save action in Account controller with invalid data as below:

Click Save button submit form to Save action in Account controller with valid data

Output