Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
04 Mar
2021

Globalization and localization in ASP.NET Core With Resource Files

by Shubham Batra

2421

Download Attachments

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.

                                                                         

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

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 

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

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.

  • |
  • Globalization and localization in ASPNET Core , Asp Net Core with Resource file resx , How to get the resx file strings in asp net core , Culture in Net core , Localisation in AspNet Core

Comments

Tags

How to set Date and time format in IIS Manager
IIS Manager
IIS
Internet Information Services (IIS) Manager
Internet Information Services (IIS)
Internet Information Services
Error Handling In AspNet Core
Exception Handling Asp Net Core
Exception Handling
Exception Handling Asp Net
Aspnet
Creating Log Files in MVC
Error Handling in MVC
Exception Handling in AspNet
Handling Exceptions and Creating Error Logs in Asp net Mvc using base controller
net
Code2Tonight
Stopping Browser Reload On Save
Repository Pattern with ADONet in MVC
Repository Pattern With ASPNET MVC And AdoNet
MVC Crud Operation
Jquery Full Calender Integrated With ASPNET
Full Calendar
Jquery Calendar
Slick Slider
Slick Slider Example
responsive carousels
Entity Framework
MVC
Intergrate SummerNote Text Editor into AspNet MVC
Web Config
Auto Redirection
Redirection from Http to https
AspNet
Url Rewriting
Postgre
PgAdmin4
PostgreSql
A Non Fatal Error Occured During Cluster Initialisation In Postgre SQL
Microsoft Outlook
Outlook Appointments
Microsoft Exchange Service
Send Email With HTML Template And PDF Using ASPNet C#
Send Email
Email with html template
email with pdf attachment
email with html and pdf
Microsoft Outlook Contacts
Outlook
Microsoft Exhchange Service
JSON
Convert string with dot notation to JSON
HTTP Error 5025 ANCM Out Of Process Startup Failure
Internet Information Service
Net core
Payumoney Integration With AspNet MVC
Prism js
Highlighting Syntax
Syntax Highlighting
code stylings
c#
Jquery AJax
Ajax
Jquery
Implement Stripe Payment Gateway In ASPNET
Using Checkout in an ASPNET Web Forms application
Stripe Payment
Stripe Payment Integration
Stripe Integeration
How to upload Image file using AJAX andjQuery
upload Image file using AJAX and jquery
Ajax call
file upload using ajax
file uploading using ajax and jquery
ConfigurationBuilder does not contain a definition for SetBasePath
Reading app json file in dot net core
Appsetting jso
Dot Net Core
Globalization and localization in ASPNET Core
Asp Net Core with Resource file resx
How to get the resx file strings in asp net core
Culture in Net core
Localisation in AspNet Core
Url Encryption in AspNet MVC
Url Encryption in C#
Url Encryption
Custom Helpers
Slick Slider with single slide
Slick
Vue js
Child Components
How to reload vue js child components
Net Core
Visual Studio
C#
Net core 31
Razor
Zoom sdk
Zoom c# wrapper Inegration
zoom Integration in c#
Zoom Integration
Zoom window sdk
vue js toggle button
vue js
toggle buttons
vuejs
vue js toggle switch
SignalR
VueJs
SignalR in Net Core
Chat App in Vue js
Chat App using SignalR
AspNet Chat app
JPlayer
Html5 Audio Video Player
Music Player
QR Code Generator
QR Code
Jquery QR Code
AspNet MVC
Google Maps
Google map api
Places API
Google map Places API in AspNet
Jquery Autocomplete
Autocomplete
Jquery UI Autocomplete
ExcelDataReader
Import data from excel in AspNet
Card Number Formatting
Amex Card Format
Card Format
FCM
Cloud Messaging
Android Notifications
FCM Notifications for IOS
IOS Notifications
Angular js
apply css on child components in Angular js
Angular Mentions
Google Sign In
Google Login
Google Oauth Api
Social Login
Aspnet Mvc
Google + Api
Create and publish a package using Visual Studio (NET Framework
Windows)
Create and publish a nuget package
create your own nuget package
Image compress
Image optimization
compress Image
optimize Image
WebForm
AspNet Web Pages
Batch Script
Database backup
Powershell
ASpNet
Sql Server Backup
LinkedIn Authentication
Login using LinkedIN
Social Login in AspNet
LinkedIn Authentication in AspNet MVC
LinkedIn Login in aspnet MVC
Shuffle List in c#
C#Net
Google Login in AspNet MVC
GoogleAuthentication Nuget package
Password Encryption
RFC Encryption
Encryption and Decryption
Encryption in AspNet
Base 64 Encryption
Base 64 Decryption

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Kurukshetra
  • [email protected]

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2022 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Json Beautifier