Introduction to the Internationalization of MVC
The problem of encountering internationalized language in the project is often the case, before doing about the MVC internationalization language, initially intends to make full use of angularjs to achieve, but gradually found that the page title is difficult to control its language conversion, So for the page tiltle use the background resources file to achieve and foreground use ANGULARJS to achieve, so better concise and convenient, this section we talk about the internationalization of MVC problem.
Topic Introduction
For the sake of efficiency all the use of front-end scripting is a good choice, but sometimes a little bit more trouble, this article only discusses the use of MVC to achieve, let us first look at an example.
We create a new il8n folder in the project create a new resource file under here "Note: Modify the access modifier to public"
Next we create a new " Internationalizationcontroller Controller", where the value of the key for its resource file is obtained
Assembly MyAssem = assembly.getexecutingassembly (); ResourceManager rm = new ResourceManager ("ASP.NET_MVC_7.IL8N.RESOURCE.ZH-CN", MyAssem); Viewbag.title = rm. GetString ("Cnblogs"); Viewbag.blog = rm. GetString ("Blogname"); Viewbag.sign = rm. GetString ("Mysignature");
Let's start by debugging to see if the value has been obtained.
The error details are as follows:
Additional information: Could not find any resources that are appropriate for the specified culture or neutral culture. Make sure that "asp.net_mvc_7.il8n.resource.zh-cn.resources" is correctly embedded or linked to the assembly "ASP. Mvc_7" At compile time, or that all required satellite assemblies are loadable and fully signed.
When this error occurs, I do not know why, after looking at the information has said, see the project Obj/debug (see project operation mode Select corresponding mode) whether there is a corresponding extension . Resources end of the file, if any, copy the string except for the extension, first see if there is a corresponding file it , as follows:
See here to copy the content and the code is written in a consistent, or no solution. After thinking, take a look at this ResourceManager class parameters specifically refers to what the meaning of the first is basename, the hint is not too clear, or to see the MSDN note on whether there is attention to the place. Sure enough Look at the following:
Resource file name can not have any extension, look at our name before, I first demonstrated the creation of a resource file in the upper left corner has been explicitly marked, it created a resource name of Resource.zh-cn.resx, The intention of the above is that there can only be one. End of the resource name, we will now resource. Take a look again, the code is modified as follows.
ResourceManager rm = new ResourceManager ("ASP.NET_MVC_7.IL8N.ZH-CN", MyAssem);
Before, compare the following:
ResourceManager rm = new ResourceManager ("ASP.NET_MVC_7.IL8N.RESOURCE.ZH-CN", MyAssem);
We give the following code in the corresponding view:
Let's take a look at the final effect:
Note: When you create a resource file name, you must name it with an extension or an underscore.
Of course, for ease of administration, you can create a single project for a resource file, and you only need to load the corresponding project assembly, for example:
var Il8massem = Assembly.Load ("Il8nresource"); ResourceManager rm = new ResourceManager ("IL8NRESOURCE.ZH-CN", Il8nassem); var blog = rm. GetString ("Blogname");
MVC for InternationalizationThere are only two forms of implementing the Internationalization Choice language:
(1) Select your language by using the dropdown box.
(2) According to the user's PC or phone language to select the corresponding language for translation.
This article shows the following drop-down boxes. Before we need to understand the internationalization, internationalization is what, is not the language between different countries, well, yes, if you are not a computer, I will immediately identify with you, or a little despise you. Let's take a look next.
InternationalizationInternationalization is abbreviated as i18n,i18n and what kind of thing, it represents 18 letters from I to N. Internationalization is used to develop products or software, in which case they can easily be localized into language and culture, and it is divided into globalization and localization.
(1) Globalization: Globalization is abbreviated as g11n, representing 11 of letters from G to n, which can be processed in a way that enables them to support different cultures when developing products or software.
(2) Localization: Localization is abbreviated as L10N, representing 10 of letters from L to N, which can be customized for specific cultures when developing products or software.
Culture in the ASP. NET FrameworkThere are two cultures in asp: Culture and UICulture, with two lowercase letters to define the language, and two uppercase letters to define the area. For example, en stands for English, while GB and US represent Britain (UK) and American (United States), so in this case, in the United Kingdom, it is defined as EN-GB, which is defined as en-us in the United States.
Culture: Represents the function of the relevant cultural, such as date, number, currency, etc.
UICulture: Used to locate the correct resource file and present it on the Web page through the ResourceManager class.
Both of these cultural attributes are in the. NET is available in each thread, and is handled by the ASP when rendering is required.
Internationalization in MVCApproximate idea: Store the language in session and read the resource file change language through the drop-down box. Then we begin to realize.
(1) Create a new project Internationalizationresources and under which the Chinese and English resource files are created.
(2) Create a user registration class and use ResourceType in DataAnnotations to correspond to its default resource type (Chinese)
public class Userviewmodel {[Display (Name = "UserName", ResourceType = typeof (Internationalizationresources . Resource)] [Required (errormessageresourcename = "usernamerequired", Errormessageresourcetype = typeof (Internationa Lizationresources.resource)] public string UserName {get; set;} [Display (Name = "FullName", ResourceType = typeof (Internationalizationresources.resource))] [Required (Errormessageresourcename = "namerequired", Errormessageresourcetype = typeof ( Internationalizationresources.resource)] public string FullName {get; set;} [Display (Name = "Password", ResourceType = typeof (Internationalizationresources.resource))] [Required (Errormessageresourcename = "passwordrequired", Errormessageresourcetype = typeof ( Internationalizationresources.resource)] public string Password {get; set;} [Display (Name = "ConfirmPassword", ResourceType = typeof (Internationalizationresources.resource))] [RequiRed (errormessageresourcename = "confirmpasswordrequired", Errormessageresourcetype = typeof ( Internationalizationresources.resource)] [Compare ("Password", Errormessageresourcename = "Confirmpasswordcompare" , Errormessageresourcetype = typeof (Internationalizationresources.resource))] public string ConfirmPassword {get; Set } [Display (Name = "Address", ResourceType = typeof (Internationalizationresources.resource))] [Required (Error Messageresourcename = "Addressrequired", Errormessageresourcetype = typeof (Internationalizationresources.resource)) ] public string Address {get; set;} }
(3) Set the culture at initialization time.
protected override void Initialize (System.Web.Routing.RequestContext requestcontext) { base. Initialize (RequestContext); if (session["CurrentCulture"]! = null) { Thread.CurrentThread.CurrentCulture = new CultureInfo (session[" CurrentCulture "]. ToString ()); Thread.CurrentThread.CurrentUICulture = new CultureInfo (session["CurrentCulture"]. ToString ()); } }
(4) The language is stored via the drop-down box.
Public ActionResult changeculture (string ddlculture) { Thread.CurrentThread.CurrentCulture = new CultureInfo (ddlculture); Thread.CurrentThread.CurrentUICulture = new CultureInfo (ddlculture); session["CurrentCulture"] = ddlculture; Return View ("Index");
(5) User registration and verification.
[HttpPost] Public ActionResult Index (userviewmodel user) { if (modelstate.isvalid) { } return View (); }
(6) Use the strongly typed view to get the page title first.
@model asp.net_mvc_7.models.userviewmodel@{ viewbag.title = InternationalizationResources.Resource.Title;}
(7) Read the language in the resource file and set it to the drop-down box and select the language to change the language event.
<div class= "col-md-4" > @using (Html.BeginForm ("ChangeCulture", "internationalization")) { <p > @InternationalizationResources. resource.selectlanuage: @Html. DropDownList ("Ddlculture", New SelectList ( New[] {new{value= "ZH-CN", text= InternationalizationResources.Resource.Chinese}, new{value= "en-us", text= InternationalizationResources.Resource.English}}, "Value", "Text", session["CurrentCulture"]), new {onchange = " This.form.submit (); "}) </p> } </div>
(8) to register and verify.
<br/> @using (html.beginform ("Index", "internationalization")) {@Html. AntiForgeryToken () <div class= "Form-h Orizontal "> @Html. ValidationSummary (True," ", new {@class =" Text-danger "}) <div class=" Form-group "& Gt @Html. labelfor (model = model. UserName, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "Col-md-10" > @Html. editorfor (model = model. UserName, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model = Model. UserName, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group "> @Html. labelfor (model = model. FullName, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "Col-md-10" > @Html. editorfor (model = model. FullName, new {htmlattributes = new {@class = "Form-control"}}) @Html. VAlidationmessagefor (model = model). FullName, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group "> @Html. labelfor (model = model. Password, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "Col-md-10" > @Html. passwordfor (model = model. Password, new {@class = "Form-control"}) @Html. validationmessagefor (model = model. Password, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group "> @Html. labelfor (model = model. ConfirmPassword, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "Col-md-10" > @Html. passwordfor (model = model. ConfirmPassword, new {@class = "Form-control"}) @Html. validationmessagefor (model = model. ConfirmPassword, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Html. labelfor (model = model. Address, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "Col-md-10" > @Html. textareafor (model = model. Address, new {@class = "Form-control"}) @Html. validationmessagefor (model = model. Address, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > <div class= "col-md-offset-2 col-md-10" > <input type= "Submit" value= "@International IzationResources.Resource.Save "class=" btn btn-default "/> </div> </div> </div> }
Let's take a full look at the demo effect:
SummarizeIn this section we learned how to internationalize in MVC, note: Generally we can set a default language such as the above Resource.resx (Chinese), and the English must be Resource to the beginning followed by the language such as (RESOURCE.EN-US.RESX) line. When the page needs a few translations can be directly through the ResourceManager class to achieve, but the above also said the need to pay attention to the place.
The internationalization of MVC