ASP. NET MVC Multi-language (Html+js common set of resource files)

Source: Internet
Author: User

This article is mainly a experience sharing of recent work in long languages. The text of the content for the reference to a number of great God's program, self-kneading out of a new scheme, for the HTML and JavaScript part of the multi-language switch, a common set of resource files. The code is primarily implemented using the IHttpModule + cookie method. The specific principle does not say much (ask the degree Niang or the Google), aims to share the code. If have not like also please detour, do not spray!

This program is relatively simple to understand, nor low, or closely follow the international fan, if you feel good, may as well use to look!

First step: Create a resource file

Step Two: Add the IHttpModule interface implementation class

namespacepims_webconsole{ Public classCultureawarehttpmodule:ihttpmodule { Public voidDispose () {} Public voidInit (HttpApplication context) {context. BeginRequest+=setcurrentculture; }        Private voidSetcurrentculture (Objectsender, EventArgs args) {            stringCulturename =string.            Empty; HttpCookie Culturecookie=System.web.httpcontext.current.request.cookies[const.cookiename_language]; if(Culturecookie! =NULL) {culturename=Culturecookie.value; Thread.CurrentThread.CurrentCulture=NewCultureInfo (culturename); Thread.CurrentThread.CurrentUICulture=Thread.CurrentThread.CurrentCulture; }        }    }}

Step three: Registering instance classes in Web. config

  <system.webServer>    <modules>      <add name="cultureawarehttpmodule" type="pims_webconsole.cultureawarehttpmodule"/>    </modules>  </system.webServer>  

To this, the background of the multilingual switch part of the Code is aligned. The code behind the script

Fourth step: The core code of the JavaScript section

var__page = __page | |{};__page.langsuffix= "";//Language Cookie Name__page.langcookiename = "_pims_lang";//Dictionary of language type and suffix control__page.langdictionary = {"ZH-CN": "", "VI-VN": "_VN" };//Select language__page.lang =$.cookie (__page.langcookiename);if(!__page.lang) {__page.lang= "ZH-CN"; _setlanguagecookie (__page.lang);};//Language Information Collection__page.cultures =NULL;//Initialize the page language__page.localization =function() {kendo.culture (__page.lang); __page.langsuffix=__page.langdictionary[__page.lang];};__page.localization (); __page.setlanguagecookie=_setlanguagecookie;function_setlanguagecookie (lang) {$.cookie (__page.langcookiename, Lang, {expires):9999 }); Window.location.reload ();}

Fifth step: The use of language in View+js

The way to @resource directly in view is OK ( don't tell me how I can't?) I will not tell you that you need to go to the config in the view using this namespace ), JS, mainly in the view to define a JSON object, and then JS in the use of this JSON object. To some extent to solve some obsessive compulsive disorder players, for their own code in the various names of the custom quirks.

Example in view:

<script>var$PageLanguage ={OK:"@Html. Raw (@Resource. OK)", Cancel:"@Html. Raw (@Resource. Cancel)", Create:"@Html. Raw (@Resource. Create)", Query:"@Html. Raw (@Resource. Query)", MachineName:"@Html. Raw (@Resource. MachineName)", Shift:"@Html. Raw (@Resource. Shift)"    };</script>

@section Scripts {
@Scripts. Render ("~/customjs/test.js?v=1.0")

}

<label class= "Toolbar-label" for= "Shift" > @Resource .shift</label>

JS in the example:

$ (function  () {    alert ($PageLanguage. OK);});

By this, you have finished all the content, you can start your performance!

Thank you!

ASP. NET MVC Multi-language (Html+js common set of resource files)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.