Auto: http://dev.yesky.com/msdn/182/2412682.shtml
1. Make the interface the same way as before, just note that use label to make all the text that requires a multilingual interface
2. After finishing, select this file in Solution Explorer and select 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 the 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 that meta: resourcekey = "pageresource1" and meta: resourcekey = "localize1resource1" indicate that the text is read from the resource.
7. OK is now used as the resource file for another language. Very simple, copy default. aspx. resx again paste a bit, then rename for 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? Open IE, tools-> Internet Options-> ages-> Add, and click in the middle of the box.
9. Edit the text of other resources now. 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 extends age_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 userages as 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 the language. For example, we use two buttons. After clicking this button, we need 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 subProtected sub setlanguage (byval language as 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) is used to re-read the page, so that the page displays 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!