1. Define the user language settings Information Class and save the language selected by the user. You can use session. Here cookies are used.
Using System;
Using System. Data;
Using System. configuration;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;
Using Commonutility;
/**/ /// <Summary>
///The ASPnet method provides language setting objects for the context language environment.
/// </Summary>
Public Class Aspnetlocalizer: ilocalizer
{
Httpcontext;
/**/ /// <Summary>
///Create object
/// </Summary>
/// <Param name = "httpresponse">HTTP objects</Param>
Public Aspnetlocalizer ()
{
This. Httpcontext=Httpcontext. Current;
}
Ilocalizer Member # Region Ilocalizer Member
Public String Localizername
{
Get
{
// Because cookies are divided into response. Request, you need to save the language information in the httpcontext context.
If (Httpcontext. items [cookiescenter. Language] = Null )
{
VaR localizername = Cookies. readcookies (httpcontext. Request, cookiescenter. Language );
If (String. isnullorempty (localizername ))
{
Localizername= "Zh-CN";
Setname (localizername );
}
Return Localizername;
}
Else
{
ReturnHttpcontext. items [cookiescenter. Language]. tostring ();
}
}
Set
{
Setname (value );
}
}
/**/ /// <Summary>
///Set Name
/// </Summary>
/// <Param name = "localizername">Language name</Param>
Void Setname ( String Localizername)
{
Cookies. writecookies (httpcontext. Response, cookiescenter. Language, localizername );
Httpcontext. items [cookiescenter. Language] = Localizername;
System. Globalization. cultureinfo Ci = New System. Globalization. cultureinfo (localizername );
System. Threading. thread. currentthread. currentculture = CI;
System. Threading. thread. currentthread. currentuiculture = CI;
}
# Endregion
}
The access to cookies is limited. When the request object is read and written to the response object, we need to save the value of the language selected by the user in other objects and save it in the session, save it in httpcontext. in items.
2. Define the httpmodule. During each access, the language is changed based on the user's settings.
Code
Using System;
Using System. Data;
Using System. configuration;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;
Using Commonutility;
//
// provides custom module initialization and event handling for implementation classes.
//
Public class myhttpmodule: ihttpmodule
{< br> httpapplication context;
Public phenixhttpmodule ()
{< br>
}
# region ihttpmodule member
Public void dispose ()
{< br> unitycontainer. objectprovider = null ;
context. postacquirerequeststate -= New eventhandler (context_postacquirerequeststate );
}
Public void Init (httpapplication context)
{< br> This . context = context;
context. postacquirerequeststate += New eventhandler (context_postacquirerequeststate );
}
void context_postacquirerequeststate ( Object sender, eventargs e)
{< br> var app = sender as httpapplication;
If (unitycontainer. objectprovider = null )
{< br> var provider = New aspnetobjectprovider ();
unitycontainer. objectprovider = provider;
}
system. globalization. cultureinfo CI = New system. globalization. cultureinfo (unitycontainer. getObject ilocalizer > (). localizername);
system. threading. thread. currentthread. currentculture = Ci;
system. threading. thread. currentthread. currentuiculture = Ci;
}
# Endregion
}
3. Add a record in Web. config
< Httpmodules >
< Add Name = "Mymodule" Type = "Myhttpmodule" />
</ Httpmodules >
4. Define the resource file access object ResourceManager
Code
Public class ResourceManager
{
Xelement ResourceManager;
Public localizer ()
{
// Read resource files as needed
VaR culture = system. Threading. thread. currentthread. currentculture;
VaR filepath = httpcontext. Current. server. mappath ("~ /App_code/"+ culture. Name +"/lang. xml ");
Xmllocalizer = xelement. Load (filepath );
}
Public localizer ()
: This ())
{
}
Public String getstring (string ID)
{
Return return xmllocalizer. descendants ("resource "). where (P => P. attribute ("name "). value = ID ). select (p1 => p1.value ). firstordefault ();
}
}
5. instantiate and assign values where values need to be assigned.
Note: ASP. NET is a beginner. This solution is very immature and can be used by yourself.