Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Globalization and localization in ASP.NET Core With Resource Files

Globalization and localization in ASP.NET Core With Resource Files

Date- Mar 04,2021 Updated Jan 2026 10946 Free Download Pay & Download

Globalisation and Localisation in .net core 3.1

In ASP.Net webforms you all might have used Resource(.resx)  files for implementing multiple language across your websites. What that mean is your webpage will pick text according to the selected culture from the resource file's key value string. However, with the evolution of .net technology and .Net core 3.1 the ways of applying localisation and Resource Files has changed.

So, you will find many solutions asking you to use  IStringLocalizer<Resource> for implementing localisation but those seems to be too complicated if you are a beginner or new to .net core. So, for those who  are new and want a simpler solution to use Resource files then follow the steps.


Step-1 Add Resource Files on Application Root

So the first step we need to follow is adding two Resource Files in your .Net core Application at root level. Remember you have to first keep the name Resource.resx and Resource1.resx while adding the files and you can change the filename later according to the culture likes Resource.en-US.resx and Resource.Ar.resx  but if you try to use those names from beginning those files won't build properly.

Globalization and localization in ASPNET Core With Resource Files                                                                         

After adding the files click on the files one by one and set Access modifier to Public 

Globalization and localization in ASPNET Core With Resource Files 2

Now as our Resource Files are ready to use we will now move to startup.cs file for further steps.

Step 2 - Changes in Startup.cs

So you can see we need few basic things here including Session and AddHttpContextAccessor . We will be using them in later steps.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;


namespace WebApplication1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSession();
            services.AddHttpContextAccessor();
            services.AddControllersWithViews();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddMvc().AddViewLocalization().AddSessionStateTempDataProvider();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

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

After configuring startup.cs now we have to use our resource files in our views. So the way to use them in the view is

Resource.ResourceManager.GetString("Hello", new CultureInfo("Ar"))

You can directly use your Resource string by this line . This basically need the culture name for which you have to get your string and the string you want. It will look for the string as per the specified culture and then giving a localised string.

However , we can create a separate class with some common method to get this localised string in order to make it more simpler. We will be using session to store our active culture. You can choose from where you will get culture into session.

We will be creating a new class


 public static class CustomResourceHelper
    {
        public static string GetResourceString(IHttpContextAccessor httpContext, string data)
        {
            if(string.IsNullOrEmpty(httpContext.HttpContext.Session.GetString("culture")))
                httpContext.HttpContext.Session.SetString("culture", "en-US");
            return Resource.ResourceManager.GetString(data, new CultureInfo(httpContext.HttpContext.Session.GetString("culture")));
        }
    }

This class will basically provide us a method to which we have to pass the httpContextAccessor Object and also the Resource string which you would like to have. You can make changes according to your need. And On View you can use it like Globalization and localization in ASPNET Core With Resource Files 3

So you can see we have used  this to get our localised string for "Hello".The reason why we need HttpContextAccessor is because of the culture value which we have saved in that.

@CustomResourceHelper.GetResourceString(HttpContextAccessor, "Hello")

You can see the change culture method which is saving the culture in the httpContextAccessor Session. Which we are using inside the class used on the view.

 public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HomeController(ILogger<HomeController> logger, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _httpContextAccessor = httpContextAccessor;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public IActionResult ChangeCulture(string culture)
        {
            _httpContextAccessor.HttpContext.Session.SetString("culture", culture);
            return RedirectToAction("Index");
        }
    }

You can change your view like this 

@using Microsoft.AspNetCore.Http
@{
    ViewData["Title"] = "Home Page";
    var culture = string.IsNullOrWhiteSpace(HttpContextAccessor.HttpContext.Session.GetString("culture")) ? "en-US" : HttpContextAccessor.HttpContext.Session.GetString("culture");
}
<select onchange="window.location.href='/Home/ChangeCulture?culture='+this.value">
    @if (culture == "en-US")
    {
        <option value="Ar">Arabic</option>
        <option selected value="en-US">English</option>
    }
    else
    {
        <option selected value="Ar">Arabic</option>
        <option  value="en-US">English</option>
    }
</select>
<div class="text-center">
    <h1 class="display-4">Welcome @CustomResourceHelper.GetResourceString(HttpContextAccessor, "Hello")</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

So, make the changes according to your need and run you project . You can download sample files also which we will provide along with this article .So the final output will be
Globalization and localization in ASPNET Core With Resource Files 4Globalization and localization in ASPNET Core With Resource Files 5

So,you can see whenever we will change  the dropdown language the text will change according to resource string . Download files or comment on article if you have any issues.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Encrypt and Decrypt Password in Asp.Net
May 15, 2022
Exception Handling Asp.Net Core
Aug 05, 2020
HTTP Error 500.31 Failed to load ASP NET Core runtime
Aug 23, 2022
How to implement Paypal in Asp.Net Core
Oct 30, 2022
Previous in ASP.NET Core
HTTP Error 502.5 - ANCM Out Of Process Startup Failure
Next in ASP.NET Core
Visual studio not refreshing changes in browser on reload
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • Task Scheduler in Asp.Net core 17548 views
  • Implement Stripe Payment Gateway In ASP.NET Core 16788 views
  • Send Email With HTML Template And PDF Using ASP.Net C# 16562 views
  • How to implement Paypal in Asp.Net Core 8.0 12921 views
  • Import data from Excel in Asp.Net 12788 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor