The method of implementing multi-language interface in ASP.net 2.0

Source: Internet
Author: User
asp.net 1. Do the same interface as before, just note that all the text needs to have a multilingual interface with the label to do





2. When you're done, select this file in Solution Explorer and choose Tools->generate Local Resource





3. You will find that a directory has been generated, app_localresources, and a ResX file in this directory. For example, if your ASPX file is default.aspx, it generates a file called Default.aspx.resx.





4. Open this file to see, the original in the label of those words are running here to





5. Open the original ASPX file to see source, will find the source changed:





<%@ Page language= "VB" masterpagefile= "~/masterpage.master" autoeventwireup= "false" codefile= "Default.aspx.vb" inherits= "Default2" title= "Untitled Page" culture= "Auto" meta:resourcekey= "PageResource1" uiculture= "Auto"%>





............





<asp:localize id= "Localize1" runat= "Server" meta:resourcekey= "Localize1resource1" > </asp:Localize>





*********************************************************************


is automatically generated if it is text on other controls. such as the button and the GridView:





<asp:button id= "Button1" runat= "Server" meta:resourcekey= "Button1resource1" text= "Button1"/><BR/>


<asp:gridview id= "GridView1" runat= "Server" meta:resourcekey= "Gridview1resource1" >


<Columns>


<asp:boundfield headertext= "Header1" meta:resourcekey= BoundFieldResource1 "/>"


<asp:boundfield headertext= "Header2" meta:resourcekey= BoundFieldResource2 "/>"


</Columns>


</asp:GridView>

The
generated resource is as follows:











   *********************************************************************





6. Note here: meta:resourcekey= "PageResource1" and meta:resourcekey= "Localize1resource1" This means that the text here is read from the resource.





7. OK now to do a resource file for another language. Very simple, copy Default.aspx.resx again paste, then rename for Default.aspx.fr-fr.resx notice FR-FR is the name of the language you want to do. If you look at DVDRip, you should think it's like a subtitle file, right?





8. Some people say how do I know what the name of that language is? Very simple, open ie,tools->internet options-> Languages-> Add, box in the middle of it.





9. Now start editing the text for another resource. Open Default.aspx.fr-fr.resx, contrast the original content, the contents of the inside into French.





10. So far, we have completed the first phase. Asp. NET selects the displayed language according to the user's language preference. To change the settings in IE, in the place we have just said changed into FR-FR, open this page to see? If it's French, congratulations. If not, go back and redo ...





11. Next, we want to allow the user to choose their own language, not automatically. We will use cookies to access the user's choice. At the application level, we read this cookie and set the appropriate culture and uiculture. Of course, the first time we come in, we still have to read the browser settings, which can be obtained from the request.userlanguages.





Private Supportedlanguages as String () = {"En-us", "FR-FR"}


public Const language_cookie_name as String = "Userlanguage"





Sub application_acquirerequeststate (ByVal sender as Object, ByVal e as EventArgs)


Dim Languagecookie as HttpCookie = Request.Cookies (language_cookie_name)


Dim language as String = String.Empty





If (Languagecookie is nothing) Then


Dim UserLanguages as String () = Request.UserLanguages


Dim Index as Integer


for i as Integer = 0 to Userlanguages.length-1


index = Array.indexof (supportedlanguages, UserLanguages (i))


If Index >= 0 Then


language = supportedlanguages (index)


Exit for


End If


Next





If Language = String.Empty Then _


language = supportedlanguages (0)


Response.Cookies.Add (New HttpCookie (Language_cookie_name, LANGUAGE))


Else


language = Languagecookie.value


End If





Dim culture as CultureInfo = New CultureInfo (language)


System.Threading.Thread.CurrentThread.CurrentUICulture = Culture


End Sub


12. Then we need to have a page for the user to choose the language, such as we use two button to do. After clicking on this button, we want to set this cookie to the appropriate language:





Protected Sub button2_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Button2.click


setlanguage ("Fr-fr")


End Sub





Protected Sub setlanguage (ByVal language as String)


Response.Cookies (Global.ASP.global_asax. Language_cookie_name). Value = Language


Dim culture as CultureInfo = New CultureInfo (language)


System.Threading.Thread.CurrentThread.CurrentUICulture = Culture


Server.Transfer (Request.path)


End Sub


Note the final Server.Transfer (Request.path), which is to re-read the page, so that the page will also display the newly selected language.





13. Finally we have to remove the ASP.net automatic selection. Remove from Default.aspx:





culture= "Auto" uiculture= "Auto"





14. Open it up and take a look!





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.