Click here to Skip to main content
15,887,453 members
Articles / Web Development / HTML
Tip/Trick

Working with Resources File in ASP.NET MVC5

Rate me:
Please Sign up or sign in to vote.
4.94/5 (7 votes)
20 Apr 2016CPOL2 min read 40.2K   845   12   6
How to add and work with Resource files in ASP.NET MVC5

Introduction

In this tip, I will discuss how to work with resource files in ASP.NET MVC5. This is a very basic way to work on it and there is not only this method to do it. So keep coding and be happy.

Working with the Code

  • Add a folder in your project and name it LanguageResources (or whatever you want).
  • Add a Resources file (.resx) in that folder and name it Global.resx for English and Global.bn.resx for Bangla (in my case).
  • Keep the Access Modifier public for this file (and other resource files also).
  • Then add some Name and their respective Value as key value pair. Such as Name(First_Name) and Value(First Name) and so on.
  • After that, add buttons to your pages from where you want to change the language.
  • Then, create a controller to change the language and then update your Global.asax.cs file.

Add some button or action link in your page from where you want to change the language. Remember, you have to give the language culture info in the query string like below (here en for English and bn for Bangla).

C#
@Html.ActionLink("English", "ChangeLanguage", 
"Language", new { SelectedLanguage = "en" }, null)
@Html.ActionLink("বাংলা", "ChangeLanguage", 
"Language", new { SelectedLanguage = "bn" }, null)

Create a Language controller and create an action like below (I named it ChangeLanguage) in that controller. Pass the language information you gave in query string as a parameter for this action. Then, save the chosen language in your browser cookies like below:

C#
public ActionResult ChangeLanguage(string SelectedLanguage)
        {
            if (SelectedLanguage != null)
            {
                Thread.CurrentThread.CurrentCulture = 
                	CultureInfo.CreateSpecificCulture(SelectedLanguage);
                Thread.CurrentThread.CurrentUICulture = 
                	new CultureInfo(SelectedLanguage);
                var cookie = new HttpCookie("Language");
                cookie.Value = SelectedLanguage;
                Response.Cookies.Add(cookie);
            }
            return RedirectToAction("Index", "Home");
        }

Now to check your selected language every time the pages loads, you need to write the code below in your Global.asax.cs file. It will check the cookie value and set the language you stored in your cookie. If cookie is empty, then it will select en (English) as default.

C#
protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
            if (cookie != null && cookie.Value != null)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = 
                	new System.Globalization.CultureInfo(cookie.Value);
                System.Threading.Thread.CurrentThread.CurrentUICulture = 
                	new System.Globalization.CultureInfo(cookie.Value);
            }
            else
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = 
                	new System.Globalization.CultureInfo("en");
                System.Threading.Thread.CurrentThread.CurrentUICulture = 
                	new System.Globalization.CultureInfo("en");
            }
        }

Now, in your project where you want the label to be shown, just put the line below with the Name of your Resource file.

C#
@YourApplicationName.YourResourceFolderNameSpace.Global.ResourceNameProperty

Example:

C#
@TestProject.LanguageResource.Global.First_Name

And this will show you as First Name since its Value was set as First Name.

That's all of it. Now run your project and see the magic.

Points of Interest

I had a lot of fun changing the language to Bangla because sometimes you would not find an appropriate word for an English word.

History

  • Source code (full project) is uploaded.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Bangladesh Bangladesh
Worked on ASP.NET MVC, AngularJS, jQuery, T-SQL, LINQ, Kendo UI, SQL Server, Bootstrap. Working on web based application development and fully concentrated on both client side and server side programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Arkitec25-Aug-21 13:22
professionalArkitec25-Aug-21 13:22 
SuggestionCan you upload the sourcecode? Pin
debashishPaul14-Apr-16 20:34
professionaldebashishPaul14-Apr-16 20:34 
AnswerRe: Can you upload the sourcecode? Pin
Md._Mehedi_Hasan15-Apr-16 0:40
professionalMd._Mehedi_Hasan15-Apr-16 0:40 
GeneralRe: Can you upload the sourcecode? Pin
debashishPaul15-Apr-16 1:21
professionaldebashishPaul15-Apr-16 1:21 
AnswerRe: Source code uploaded Pin
Md._Mehedi_Hasan20-Apr-16 0:20
professionalMd._Mehedi_Hasan20-Apr-16 0:20 
GeneralRe: Source code uploaded Pin
debashishPaul20-Apr-16 3:31
professionaldebashishPaul20-Apr-16 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.