Hey everyone, does anyone know a simple and easy way to change the language of the web page? I want to give users a choice of english or french, by clicking a button but not sure what the best way is
Printable View
Hey everyone, does anyone know a simple and easy way to change the language of the web page? I want to give users a choice of english or french, by clicking a button but not sure what the best way is
Hello,
I am not sure that I would go as far as to call it "easy", but certainly the accepted best practice way of doing it is to use localisation and globalisation.
You can find out information about this here:
http://msdn.microsoft.com/en-us/libr.../ms228208.aspx
Hope that helps!
Gary
Thanks Gep, I found that after I posted here, however i do have one problem, I want to be able to allow the user to choose either french or ehglish with a button click, whenever i change the Culture though IE the .resx files kick in, but when I program it does not work. Now I a not sure if that is because this is not my code as I am just help out with it and perhaps they have done something else. i know when I enter the code, the culture changes, but the resource files dont kick in.
To change the culture I am using:
i have set up a msgbox to inform me what the the culture is set to and it works and tells me which one. So far for the aspx file i have this for codeCode:Protected Sub btnSwitchLanguage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSwitchLanguage.Click
'Change the language
Dim strParameters As String = ""
If GetLanguage() = "1" Then 'Currently in English - switching to French
strParameters = "?l=f"
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-CA")
Else 'Currently in French - switching to English
strParameters = "?l=e"
End If
i only have a few items to change, I may just do it manually, but right now I just wanna firgue this out. i have seen some ways to change the the culture, but nothing so far that allows the user to be the one to decide.Code:<asp:Localize ID="lblOnlineHelp" runat="server"
Text="<%$ Resources: lblOnlineHelpResource1.Text %>"></asp:Localize>
<asp:Button ID="btnSwitchLanguage" runat="server"
Text="<%$ Resources: btnSwitchLanguageResource1.Text %>" />
Any ideas?
Hmmm, what you are doing, looks like it should be working.
The thing that I might suggest is that in addition to changing the CurrentUICulture, you might want to change the CurrentCulture as well.
Gary
So I changed the CurrentCulture and still nothing. Do you think that I should should set english as the default in the web.config file? I have not done this and from the places I have read they have all sugggested that it be done
Hmmm, not sure in all honesty.
Is it possible you can upload a small sample application that shows what you are trying to do, and why it isn't working, and I will take a look?
Gary
I have it working now, but for some reason i need to click twice to make it happen. In the application I am loading a treeview, when i click the button it changes the treeview language but the culture does not change till I click the treeview or the button again. if I click the button it cause the languages not to match up, the treeview will be in english and the rest in french, if I click a node in the treeview it matches. Not sure how to make it so that it only takes one click to change
Hello,
This would suggest that you are not setting the change early enough in the ASP.Net Page Life Cycle in order for it to take effect.
Where are you setting this?
Gary
Sorry about the late reply gep, this is the code i have so far
This take places around line 335 of the page, so I think a lot is happening before I do this, for example, the tree view that I have is populate first cause they have set the page as english default when it loads. i am thinking of maybe putting InitializeCulture() in page load, but not sure if that would make much of a differenceCode:Protected Sub btnSwitchLanguage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSwitchLanguage.Click
'Change the language
Dim strParameters As String = ""
If GetLanguage() = "1" Then 'Currently in English - switching to French
strParameters = "?l=f"
Else 'Currently in French - switching to English
strParameters = "?l=e"
End If
If Session("SelectedCategory") IsNot Nothing Then
strParameters &= "&cat=" & Session("SelectedCategory").ToString
End If
'Dim ParameterCollection As NameValueCollection = Request.QueryString
'If ParameterCollection.HasKeys Then
' For i As Int32 = 0 To ParameterCollection.Keys.Count - 1
' strParameters &= "&" & ParameterCollection.GetKey(i) & "=" & ParameterCollection.Get(i)
' Next
'End If
Response.Redirect("OnlineHelp.aspx" & strParameters)
End Sub
Protected Overrides Sub InitializeCulture()
If GetLanguage() = "2" Then
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-CA")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-CA")
Session("Culture") = "fr-Ca"
ElseIf GetLanguage() = "1" Then
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-CA")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-ca")
End If
End Sub
Gep I solved this problem, well I went in a different direction more or less so I will be setting this thread to resolved
Hello,
For the benefit of the rest of the forum, and anyone else in a similar position, you might want to describe how you fixed your problem.
Thanks!
Gary
I kinda just hardcoded it in. I only needed it for 1 page, and for only like 4 or 5 items, so I left the buttons and labels I needed with no text and when the page loads based on the langauge it ask for it just kicks in my code.
Not the best and ideal way, but working on a tight deadline, for preliminary copy, but still have time to finish up the final version. If I ever firgue it out, I'll post the solution to the forumCode:Private Sub SetLanguageSwitchButton()
If GetLanguage() = "1" Then
btnSwitchLanguage.Text = "Français"
lblOnlineHelp.Text = "ADMS Online Help"
Page.Title = "New Help"
copyID.Text = "Copyright 2011"
poweredById.Text = "Powered by Honesty, Integrity, Teamwork, Leadership and Respect!"
top.Text = "Top"
btnPrint.InnerText = "Print"
Else
btnSwitchLanguage.Text = "English"
lblOnlineHelp.Text = "SGPA Aide en Ligne"
Page.Title = "Aide Nouveau"
copyID.Text = "droit d'auteur 2011"
poweredById.Text = "Propulsé par Honnêteté, intégrité, travail d'équipe, le leadership et le respect!"
top.Text = "Haut de la page"
btnPrint.InnerText = "Imprimer"
End If
End Sub
yeah maintenance would be a nightmare in this kind of set-up, I am just lucky that this is the only page that is needed like this. If I had to do multiple pages or there were alot things on this page to translate it would have been a time consuming soul eating event...
Looks like you are onto a winner for now then :)
Gary