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:
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)
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)
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