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).
@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:
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.
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.
@YourApplicationName.YourResourceFolderNameSpace.Global.ResourceNameProperty
Example:
@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.