Implement localization and globalization in ASP. net mvc, asp. netmvc
When developing a multi-language website, we can create a resource file for a certain language and choose the resource file to be used during runtime based on the different language preferences set by the browser. The resource file is embedded into the Assembly when the Assembly is generated.
This article describes how to implement globalization and localization in ASP. net mvc. For example, when a browser chooses English, some page elements are displayed in English. When a browser chooses to browse in Chinese, the browser displays Chinese.
Use Visual Studio 2013 to create an authentication-free MVC project.
Create the following Model:
public class Student
{
public int Id { get; set; }
[Display (Name = "Name")]
[Required (ErrorMessage = "Required")]
public string Name { get; set; }
[Display (Name = "Age")]
[Required (ErrorMessage = "Required")]
public int Age { get; set; }
}
Generate a solution.
Add a strong-type view about Student in the Index method of HomeController, and select the default Create template. It is roughly as follows:
@model GlobalAndLocal.Models.Student
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<Input type = "submit" value = "CREATE" class = "btn-default"/>
</div>
</div>
Now, we hope that when the browser chooses English, the page elements are displayed in English.
Create a class library named MyResources in the solution.
Create a resource file for Chinese and set the access modifier to public:
Create an English resource file and set the access modifier to public:
Generate a class library.
Reference this class library in the MVC project.
Modify the Student class as follows:
public class Student
{
public int Id { get; set; }
[Display(Name=MyResources.Resource.Name)]
[Required(ErrorMessage=MyResources.Resource.NameRequiredError)]
public string Name { get; set; }
[Display(Name = MyResources.Resource.Age)]
[Required(ErrorMessage = MyResources.Resource.AgeRequiredError)]
public int Age { get; set; }
}
On the Index strong view page, modify the following:
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="@MyResources.Resource.Submit" class="btn btn-default" />
</div>
</div>
An error occurs when you run the MVC project.
Modify the Student class as follows:
public class Student
{
public int Id { get; set; }
[Display(Name="Name", ResourceType=typeof(MyResources.Resource))]
[Required(ErrorMessageResourceName = "NameRequiredError", ErrorMessageResourceType = typeof(MyResources.Resource))]
public string Name { get; set; }
[Display(Name = "Age", ResourceType = typeof(MyResources.Resource))]
[Required(ErrorMessageResourceName = "AgeRequiredError", ErrorMessageResourceType = typeof(MyResources.Resource))]
public int Age { get; set; }
}
Finally, you need to set the following in Web. config:
<system.web>
......
<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"></globalization>
</system.web>
Select English in chrome language settings.
After refreshing, the effect is as follows: