How to implement multilingual interfaces in ASP. Net (Version 2.0 or later)

Source: Internet
Author: User

 

1.
The interface is the same as before, but note that all the text that requires a multilingual interface is used as a label.

2. After completion
Select this file in Explorer and choose Tools> Generate Local Resource.

3.
You will find that a directory named App_LocalResources; contains an additional resx file. For example, if your aspx file is default. aspx, it will generate a file named default. aspx. resx.

4.
Open this file and check that all the text in the label has come here.

5. Open the original aspx file and check the source code. The source code is 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>

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

Text on other controls is automatically generated. For example:

<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: meta: resourcekey = "PageResource1" and meta: resourcekey = "Localize1Resource1"
This indicates that the text here is read from the resource.

7. OK is now used as the resource file for another language. Simple, copy
Default. aspx. resx and paste again, then rename is Default. aspx. fr-fr.resx note fr-fr is the name of the language you want to do. If you read dvdrip, do you think it is similar to a subtitle file?

8.
How do I know the name of a language? It's easy to open ie, tools-> internet options-> ages->
Add.

9.
Now, edit the text of other resources. Open Default. aspx. fr-fr.resx, compare the original content, change the content inside to French.

10.
So far, we have completed the first phase. ASP. Net selects the Display language based on your language preferences. Change the settings in IE and change the settings to fr-fr as we just mentioned. Open this page to see? If it's French, congratulations. If not, redo it later ......

11. In the next step, we should allow users to select their own language instead of automatically. We will use cookies to access users' choices. At the Application level, we read this cookie and set the appropriate culture and uiculture. Of course, when we first came in, we still had to read the browser settings, which can be obtained from Request. userages.

Private supportedlanguages as string () = {"En-us", "fr-fr "}
Public const
Export age_cookie_name as string = "userlanguage"

Sub
Application_AcquireRequestState (ByVal sender As Object, ByVal e
EventArgs)
Dim languageCookie As HttpCookie =
Request. Cookies (receivage_cookie_name)
Dim language As String =
String. Empty

If (languageCookie Is Nothing) Then
Dim userages
String () = Request. userages
Dim index As Integer
For I As Integer =
0 to userages. Length-1
Index = array. indexof (supportedlanguages,
Userages (I ))
If index> = 0 then
Language =
Supportedlanguages (INDEX)
Exit
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 select a language. For example, we use two buttons for this. After clicking this button, we need to set this cookie to the appropriate language:

Protected Sub Button2_Click (ByVal sender As Object, ByVal e
System. EventArgs) Handles Button2.Click
SetLanguage ("fr-fr ")
End
Sub

Protected sub setlanguage (byval language
String)
Response. Cookies (Global. ASP. global_asax.w.age_cookie_name). Value
= Language
Dim culture As CultureInfo = New
CultureInfo (language)
System. Threading. Thread. CurrentThread. CurrentUICulture
= Culture
Server. Transfer (Request. Path)
End
Sub

Note that the last Server. Transfer (Request. Path)
This is to re-read this page, so that this page will display the new language.

13.
Finally, we need to remove the automatic selection of ASP. Net. Remove from Default. aspx:

Culture = "auto" UICulture = "auto"

14. Open it and have 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.