|
-
Feb 1st, 2012, 10:07 AM
#1
Thread Starter
Member
[RESOLVED] Changing Languages
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
Last edited by jdogg; Feb 1st, 2012 at 10:07 AM.
Reason: Typo
-
Feb 1st, 2012, 02:39 PM
#2
Re: Changing Languages
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
-
Feb 2nd, 2012, 10:48 AM
#3
Thread Starter
Member
Re: Changing Languages
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:
Code:
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 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 code
Code:
<asp:Localize ID="lblOnlineHelp" runat="server"
Text="<%$ Resources: lblOnlineHelpResource1.Text %>"></asp:Localize>
<asp:Button ID="btnSwitchLanguage" runat="server"
Text="<%$ Resources: btnSwitchLanguageResource1.Text %>" />
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.
Any ideas?
-
Feb 2nd, 2012, 02:53 PM
#4
Re: Changing Languages
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
-
Feb 3rd, 2012, 07:06 AM
#5
Thread Starter
Member
Re: Changing Languages
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
-
Feb 3rd, 2012, 07:34 AM
#6
Re: Changing Languages
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
-
Feb 3rd, 2012, 08:01 AM
#7
Thread Starter
Member
Re: Changing Languages
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
-
Feb 3rd, 2012, 03:53 PM
#8
Re: Changing Languages
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
-
Feb 6th, 2012, 07:34 AM
#9
Thread Starter
Member
Re: Changing Languages
Sorry about the late reply gep, this is the code i have so far
Code:
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
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 difference
-
Feb 6th, 2012, 10:35 AM
#10
Thread Starter
Member
Re: Changing Languages
Gep I solved this problem, well I went in a different direction more or less so I will be setting this thread to resolved
-
Feb 7th, 2012, 02:19 AM
#11
Re: [RESOLVED] Changing Languages
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
-
Feb 8th, 2012, 08:29 AM
#12
Thread Starter
Member
Re: [RESOLVED] Changing Languages
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.
Code:
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
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 forum
-
Feb 9th, 2012, 02:33 AM
#13
Re: [RESOLVED] Changing Languages
 Originally Posted by jdogg
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 forum
Sounds like a plan.
And yes, I would have to agree, not the best approach, as that way of doing it isn't very scalable at all, and would be a maintenance nightmare 
Gary
-
Feb 9th, 2012, 07:41 AM
#14
Thread Starter
Member
Re: [RESOLVED] Changing Languages
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...
-
Feb 9th, 2012, 03:44 PM
#15
Re: [RESOLVED] Changing Languages
Looks like you are onto a winner for now then 
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|