Language Menu Drop Down Posted by gonze1 on Mon 21 of Apr, 2008 20:40 GMT I would like to know if you have samples or additional info on making changes to your language drop down. I would like to use pictures, when a user clicks on the image they are taken to the language. Is this even possible? Thank you sir.
Posted by Phil on Mon 21 of Apr, 2008 21:44 GMT Hi, I presume that you are referring to the language selection control for an ASP page. What do you really want to do? If you want to add images to the DropDownList, then so far as I can tell, this is difficult (see for example here). If you just want to have some image buttons, for example with flags on them, or an asp:Menu control, then this would be fairly easy. The source code of the SelectLanguage.ascx control is pretty simple, so you may be able to adapt it to your needs. Part of it loads the language list and part of it handles the selection. It was important for me to load the language list dynamically, but you probably won't want to do this. If you just have a set of buttons, then the code will be even simpler. If that is what you want, then I can try to make a simple sample. (Most likely, you can do this better than me anyway, since I am really not the most experienced ASP programmer!) By the way, the SelectLanguage control uses session variables to store the language selection. I am not sure that this is the best method (at least for a site with a lot of traffic). It might be better to pass in the language as part of the URL. If anyone can make a simple sample of how to do this, it would be useful for me. Phil 21st April 2008
Posted by Phil on Tue 22 of Apr, 2008 19:32 GMT I have just made a simple test with two imagebutton controls. This test is based on the code which the Add-In will to your pages when you use the command "Language Switching Support for Web Projects". This is the minimum code required in the event handlers for the buttons (in VB.NET): Protected Sub btGerman_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btGerman.Click Session.Add ( "MLCulture", "de-DE" ) 'MLHIDE Session.Add ( "MLUICulture", "de" ) 'MLHIDE Response.Redirect(Request.Url.AbsolutePath) End Sub Protected Sub btEnglish_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btEnglish.Click Session.Add ( "MLCulture", "en-GB" ) 'MLHIDE Session.Add ( "MLUICulture", "en" ) 'MLHIDE Response.Redirect(Request.Url.AbsolutePath) End Sub As I mentioned above, this code is based on the code generated by the Add-In, which has two parts: The function ml_InitCulture() Private Sub ml_InitCulture Dim MLCultureName as String = Session("MLCulture") 'MLHIDE Dim MLUICultureName as String = Session("MLUICulture") 'MLHIDE if MLCultureName IsNot Nothing AndAlso MLCultureName.Length > 0 Then Page.Culture = MLCultureName End If if MLUICultureName IsNot Nothing AndAlso MLUICultureName.Length > 0 Then Page.UICulture = MLUICultureName End If End Sub and a call to this function, which it adds to the Page.InitializeCulture() method. Protected Overrides Sub InitializeCulture() ml_InitCulture() End Sub Of course, if your project is in C#, these will be generated in C#. As you can see, this mechanism relies on session variables. It might be better to pass the selected language in as pert of the URL. I will try to make an example of how to do this in the near future and also extend the Add-In to generate appropriate code. Phil 22st April 2008
Posted by gonze1 on Wed 23 of Apr, 2008 00:28 GMT Thank you so much for your example, i search all night yesterday to find a way of putting the language into the URL. Example (deafault.aspx?culture=en-us) i did nto fimd anything that would work. If you have a solution to use a url that will be good for me. Again thank you > I have just made a simple test with two imagebutton controls. This test is based on the code which the Add-In will to your pages when you use the command "Language Switching Support for Web Projects". > > This is the minimum code required in the event handlers for the buttons (in VB.NET): > > > Protected Sub btGerman_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btGerman.Click > > Session.Add ( "MLCulture", "de-DE" ) 'MLHIDE > Session.Add ( "MLUICulture", "de" ) 'MLHIDE > Response.Redirect(Request.Url.AbsolutePath) > > End Sub > > Protected Sub btEnglish_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btEnglish.Click > > Session.Add ( "MLCulture", "en-GB" ) 'MLHIDE > Session.Add ( "MLUICulture", "en" ) 'MLHIDE > Response.Redirect(Request.Url.AbsolutePath) > > End Sub > > > As I mentioned above, this code is based on the code generated by the Add-In, which has two parts: > > The function ml_InitCulture() > > Private Sub ml_InitCulture > > Dim MLCultureName as String = Session("MLCulture") 'MLHIDE > Dim MLUICultureName as String = Session("MLUICulture") 'MLHIDE > > if MLCultureName IsNot Nothing AndAlso MLCultureName.Length > 0 Then > Page.Culture = MLCultureName > End If > > if MLUICultureName IsNot Nothing AndAlso MLUICultureName.Length > 0 Then > Page.UICulture = MLUICultureName > End If > > End Sub > > > and a call to this function, which it adds to the Page.InitializeCulture() method. > > Protected Overrides Sub InitializeCulture() > ml_InitCulture() > End Sub > > > Of course, if your project is in C#, these will be generated in C#. > > As you can see, this mechanism relies on session variables. It might be better to pass the selected language in as pert of the URL. I will try to make an example of how to do this in the near future and also extend the Add-In to generate appropriate code. > > Phil > 22st April 2008
Posted by gonze1 on Thu 24 of Apr, 2008 02:10 GMT I tried to work with what you gave me here and i am not able to get the language to change at all, no errors it even does a refresh but no language change. What can be wrong? Can you please tell me, are you able to make it work default.aspx?culture=en-US as i have seen in this site ( http://formin.finland.fi/public/default.aspx?culture=en-US&contentlan=2 ) Thank you. > I have just made a simple test with two imagebutton controls. This test is based on the code which the Add-In will to your pages when you use the command "Language Switching Support for Web Projects". > > This is the minimum code required in the event handlers for the buttons (in VB.NET): > > > Protected Sub btGerman_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btGerman.Click > > Session.Add ( "MLCulture", "de-DE" ) 'MLHIDE > Session.Add ( "MLUICulture", "de" ) 'MLHIDE > Response.Redirect(Request.Url.AbsolutePath) > > End Sub > > Protected Sub btEnglish_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btEnglish.Click > > Session.Add ( "MLCulture", "en-GB" ) 'MLHIDE > Session.Add ( "MLUICulture", "en" ) 'MLHIDE > Response.Redirect(Request.Url.AbsolutePath) > > End Sub > > > As I mentioned above, this code is based on the code generated by the Add-In, which has two parts: > > The function ml_InitCulture() > > Private Sub ml_InitCulture > > Dim MLCultureName as String = Session("MLCulture") 'MLHIDE > Dim MLUICultureName as String = Session("MLUICulture") 'MLHIDE > > if MLCultureName IsNot Nothing AndAlso MLCultureName.Length > 0 Then > Page.Culture = MLCultureName > End If > > if MLUICultureName IsNot Nothing AndAlso MLUICultureName.Length > 0 Then > Page.UICulture = MLUICultureName > End If > > End Sub > > > and a call to this function, which it adds to the Page.InitializeCulture() method. > > Protected Overrides Sub InitializeCulture() > ml_InitCulture() > End Sub > > > Of course, if your project is in C#, these will be generated in C#. > > As you can see, this mechanism relies on session variables. It might be better to pass the selected language in as pert of the URL. I will try to make an example of how to do this in the near future and also extend the Add-In to generate appropriate code. > > Phil > 22st April 2008
Posted by Phil on Thu 24 of Apr, 2008 19:34 GMT You need to have followed a few steps, which I hope are described in Quick Tour (ASP.NET 2.0). In particular, you need to export the texts to resource files and add support for language switching The first of these operations generates .resx files: in the App_LocalResoources folder, used by the meta:resourcekey mechanism in the App_GlobalResources folder, used by the ml_string() function in the code behind files. Check that these files are actually generated. The second of these operations generates the function ml_InitCultures() in your code behind files and adds a call to this function from the Page.InitializeCulture function. Check that this code has been generated. If you have done these steps, but switching language does not work, then I suggest that you try hard coding the language into the Page directive with UICulture="fr" to determine whether texts get loaded from resource files at all. I hope that this helps, but let me know if it doesn't. After all, if you are having a problem, then other people are probably having the same problem. I will look into the method of passing the language in the URL. This will probably take me a few days. Phil 24th April 2008
Posted by gonze1 on Thu 24 of Apr, 2008 23:28 GMT Phil, everything is working with the drop down do i know the resource files are working. When i try to add your first recomendation of buttons that when i am not able to get the page to change the language while clicking the button, but again if i use the drop down it will work. I will try your last recomendation for the url. Thank you > You need to have followed a few steps, which I hope are described in Quick Tour (ASP.NET 2.0). > > In particular, you need to export the texts to resource files > > and add support for language switching > > > The first of these operations generates .resx files: > *in the App_LocalResoources folder, used by the meta:resourcekey mechanism > *in the App_GlobalResources folder, used by the ml_string() function in the code behind files. > Check that these files are actually generated. > > The second of these operations generates the function ml_InitCultures() in your code behind files and adds a call to this function from the Page.InitializeCulture function. > Check that this code has been generated. > > If you have done these steps, but switching language does not work, then I suggest that you try hard coding the language into the Page directive with UICulture="fr" to determine whether texts get loaded from resource files at all. > > I hope that this helps, but let me know if it doesn't. After all, if you are having a problem, then other people are probably having the same problem. > > I will look into the method of passing the language in the URL. This will probably take me a few days. > > Phil > 24th April 2008
Posted by Phil on Thu 24 of Apr, 2008 21:54 GMT I had a quick look at how to handle URL parameters and came up with the following code (in VB): Protected Overrides Sub InitializeCulture() ml_InitCulture() End Sub Private Sub ml_InitCulture Dim MLUICultureName as String = Request("culture") 'MLHIDE if MLUICultureName IsNot Nothing AndAlso MLUICultureName.Length > 0 Then Page.Culture = MLUICultureName Page.UICulture = MLUICultureName End If End Sub Protected Sub btGerman_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btGerman.Click Response.Redirect ( Request.Url.AbsolutePath & "?culture=de-DE" ) End Sub Protected Sub btEnglish_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btEnglish.Click Response.Redirect ( Request.Url.AbsolutePath & "?culture=en-GB" ) End Sub The two button handlers at the end append the culture parameter to the URL and reload the page. The function ml_InitCulture, which is called from Page.InitializeCulture, retrieves this parameter and initialises both Page.Culture and Page.UICulture. Obviously, you would need a bit more error checking, but this mechanism works in principle. The biggest problem that I see, is that the culture parameter has to be added to every link within your web site, so that the language is preserved when you move from one page to the next. There is probably a standard technique for doing this, but I'm not a really experienced ASP programmer, and I havn't figured it out yet. Phil 24th April 2008
Posted by gonze1 on Fri 25 of Apr, 2008 00:37 GMT This is the error i get when i try your url script! ====== Compiler Error Message: BC30689: Statement cannot appear outside of a method body. Source Error: Line 8: Dim MLUICultureName As String = Session("MLUICulture") 'MLHIDE Line 9: Line 10: if MLCultureName IsNot Nothing AndAlso MLCultureName.Length > 0 Then Line 11: Page.Culture = MLCultureName Line 12: End If ========== > I had a quick look at how to handle URL parameters and came up with the following code (in VB): > > > Protected Overrides Sub InitializeCulture() > ml_InitCulture() > End Sub > > > Private Sub ml_InitCulture > > Dim MLUICultureName as String = Request("culture") 'MLHIDE > > if MLUICultureName IsNot Nothing AndAlso MLUICultureName.Length > 0 Then > Page.Culture = MLUICultureName > Page.UICulture = MLUICultureName > End If > > End Sub > > Protected Sub btGerman_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btGerman.Click > Response.Redirect ( Request.Url.AbsolutePath & "?culture=de-DE" ) > End Sub > > Protected Sub btEnglish_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btEnglish.Click > Response.Redirect ( Request.Url.AbsolutePath & "?culture=en-GB" ) > End Sub > > > The two button handlers at the end append the culture parameter to the URL and reload the page. The function ml_InitCulture, which is called from Page.InitializeCulture, retrieves this parameter and initialises both Page.Culture and Page.UICulture. > > Obviously, you would need a bit more error checking, but this mechanism works in principle. > > The biggest problem that I see, is that the culture parameter has to be added to every link within your web site, so that the language is preserved when you move from one page to the next. There is probably a standard technique for doing this, but I'm not a really experienced ASP programmer, and I havn't figured it out yet. > > Phil > 24th April 2008
Posted by Phil on Fri 25 of Apr, 2008 19:10 GMT I can't really interpret this error without seeing the rest of the file, but the error message seems pretty clear. Are the statements outside of a method body? > This is the error i get when i try your url script! > > ====== > Compiler Error Message: BC30689: Statement cannot appear outside of a method body. > > Source Error: > > > > Line 8: Dim MLUICultureName As String = Session("MLUICulture") 'MLHIDE > Line 9: > Line 10: if MLCultureName IsNot Nothing AndAlso MLCultureName.Length > 0 Then > Line 11: Page.Culture = MLCultureName > Line 12: End If > > ========== > Phil 25th April 2008