Thursday, January 28, 2016

Adding resource file for localization in Asp.net

Localization is the process of customization to make our application behave as per the current culture and locale. So in that case what we do is we keep key value pairs in a type of file called resource files (.resx files)

Lets see how to store key value pairs in a resource file and how to use them in a Razor view.(Assume our project name is "MyApplication" for examples)


  1. Create a directory in your project for resource files (Ex: directory named "Localization")
  2. To create a resource file in that directory,
    right click on that directory => click Add => click New Item => Select general tab under Visual C# => Select Resource File => click Add (Give a name to that file as you want ex:MyResource)
  3. Then your resource file will be displayed in a table format in Visual Studio Insert your Name , Value pairs. 
  4. Set "Custom Tool Namespace" value as path to your resource file (ex:MyApplication.Localization)
Now you have created a resource file successfully. Then you just have to use them in Razor views.
for that,
Add this code at the top of your views.

@using Resources = MyApplication.Localization

Here MyApplication is project name and Localization is directory of resource file.

Then when ever you want to use a value in Razor view use this,

@Resources.MyResource.Name

Here "MyResource" is name of your resource file and "Name" name of your key value pair. 

No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...