To change languages, I implemented a language selector.
But now my customer say would would like the website to detect the language settings in the browser and automatically show the site's language version based on the browser settings.
But keep in mind that it should still be possible to select another language since there is no gaurantee that even though the visiting browser/computer is in a certain language the users does not neccesarily speak/understand that language.
Furthermore you can handle the showing of numbers and dates etc. in each culture by adding a culture parameter in the web.config file, which you can read more about here: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
This is not Umbraco specific, but you can do this in your Page_Load of your page or in the Application_BeginRequest event on youy global.asax file. I have personnaly already implemented such things and I prefer doing it in the global.asax file. Then you are sure it is done every time. Here are the links to two threads that might set you on the right track:
Thanks Michael, I'll try the global.asax method. I took a look a the code examples in your links, but I didn't see a code example for the global.asax file.
@Jan
Thanks Jan, I implemented a language selector on the site, so if the visitor doesn't want the language set by his browser, he can choose another language. I also use language dictionary items to dynamically localize labels, buttons, etc ...
The examples I linked to are not implemented in the global.asax file indeed, but I think you can more or less copy-paste the examples under the Application_BeginRequest method of your global.asax file.
if I don't forget, I'll check tonight at home how I did it exactly and post the code if it can help you.
I have implemented the browser language detection script in my master template, but - sadly enough - it doesn't work on my site. Am I overlooking something? Maybe I should also configure the web.config file?
Can you maybe post the code? It might help identifying the problem. Also, can you maybe specify what exactly does not work? Do you get an error? Do you see the pages displayed in a wrong language?
Have you set the hostnames correclty in you content tree?
I don't think there is anything to do in the web.config. The only thing you can do there is specify a default culture, but I don't think it will be of much help for your problem.
I pasted this script in the <head> section of my master template:
<script language="C#" runat="server"> protected void Page_Load(object sender, EventArgs e) { if(Request.UserLanguages != null && Request.ServerVariables["script_name"] == "/") { string language = Request.UserLanguages[0]; if(!string.IsNullOrEmpty(language)) { //Get the culture name - and make sure it's in the RFC 1766 standard <languagecode2>-<country/regioncode2> //i.e. en-GB, so it's easier to check for if (language.Length < 3) language += "-" + language.ToUpper(); //Redirect to the correct page based on language, default to english switch(language) { case "en-US": Response.Redirect("/home.aspx"); break; case "nl-BE": Response.Redirect("/startpagina.aspx"); break; case "fr-BE": Response.Redirect("/acceuil.aspx"); break; default: Response.Redirect("/home.aspx"); break; } } } } </script>
I don't get an error or something, but I guess the script doesn't get executed because if I change the language settings of my browser to nl-BE or fr-BE it's always the English homepage (en-US) that get's loaded.
This is strange indeed :-S Maybe you can try to write out/log some debug information like the value of Request.UserLanguages, Request.ServerVariables["script_name"] and also the value of the variable "language" used in the switch. This might help in identifying what is goign wrong. My first guess is that, for some reason, the language value does not match with the switch values. Could it be that you get fr-FR and nl-NL instead of fr-BE and nl-BE?
Just a side note: for French I think there is a spelling mistake in the redirect : "/acceuil.aspx" instead of "/accueil.aspx" as you mention in your hostnames list.
Maybe another check to make sure: when you add your language in your
browser settings, do you then move it up as first in the list? Because
the script only checks on the first available language :-)
Thanks for your advice. How can I write out this debug information. Can I do this via the Umbraco Backoffice or do I need to do this via 'attach to proces' in Visual Studio?
I misspelled indeed 'acceuil.aspx' it should be 'accueil.aspx'.
I think the quickest way is indeed to debug in Visual Studio by attaching to process and then put some breakpoints in your code.
Alternatively, if you want to test "live", you could write the output into some hidden fields you generate from your code, via "Response.Write" or something similar. That way you can check the source of the page and see the values without having anything displayed in the browser. You can use the same mechanism to display Javascript alerts or anything of that kind.
But debugging in Visual Studio will also allow you to browse directly through the whole objects states, so I think it is the best way of doing in this case.
I was able to debug the script in Visual Studio and immediately saw the problem:
//Get the culture name - and make sure it's in
the RFC 1766 standard <languagecode2>-<country/regioncode2> //i.e. en-GB, so it's easier to check for if (language.Length < 3) language += "-" + language.ToUpper();
the string value of the language variable held 5 characters (eg nl-be) so this condition was never met
was never met, as the value of Request.ServerVariables["script_name"] was "/default.aspx"
So I adjusted the script and now it works:
<script language="C#" runat="server"> protected void Page_Load(object sender, EventArgs e) { if(Request.UserLanguages != null && Request.ServerVariables["script_name"] == "/default.aspx") { string language = Request.UserLanguages[0]; if(!string.IsNullOrEmpty(language)) { //Get the culture name - and make sure it's in the
RFC 1766 standard <languagecode2>-<country/regioncode2> //i.e. en-GB, so it's easier to check for if (language.Length < 3) language += "-" + language.ToUpper(); //Redirect to the correct page based on language,
default to english switch(language) { case "en-us": Response.Redirect("/home.aspx"); break; case "nl-be": Response.Redirect("/startpagina.aspx"); break; case "fr-be": Response.Redirect("/accueil.aspx"); break; default: Response.Redirect("/home.aspx"); break; } } } } </script>
I'm glad to hear you sorted it out by doing the debugging :-)
Maybe one last thought for the road: is there a specific reason why you actually check on "fr-BE" and "nl-BE" and not just "fr" or "nl"? Because this means that someone with language settings "fr-FR" or "nl-NL" will be displayed the English version by default, which is maybe not your intention...
Good luck with the launch of your new website tonight!
multilanguage site and browser settings
Hi,
I created a multilanguage site with three languages (English, Dutch and French):
http://195.130.154.107/
To change languages, I implemented a language selector.
But now my customer say would would like the website to detect the language settings in the browser and automatically show the site's language version based on the browser settings.
Is this possible with Umbraco?
Thanks for your advice,
Anthony Candaele
Belgium
Hi Anthony
This should be possible to achieve but I don't know if there are any SEO-wise pitfalls to watch out for.
I think you should be able to make a check on the users language by doing as suggested here: http://stackoverflow.com/questions/256229/multi-lingual-web-application-how-do-i-detect-the-users-language-in-asp-net
But keep in mind that it should still be possible to select another language since there is no gaurantee that even though the visiting browser/computer is in a certain language the users does not neccesarily speak/understand that language.
Furthermore you can handle the showing of numbers and dates etc. in each culture by adding a culture parameter in the web.config file, which you can read more about here: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
Hope this helps.
/Jan
Hi Antony,
This is not Umbraco specific, but you can do this in your Page_Load of your page or in the Application_BeginRequest event on youy global.asax file. I have personnaly already implemented such things and I prefer doing it in the global.asax file. Then you are sure it is done every time. Here are the links to two threads that might set you on the right track:
http://our.umbraco.org/forum/using/ui-questions/12887-Language-
and
http://our.umbraco.org/forum/using/ui-questions/17457-ASPNet-Usercontrol-%28Resource-files%29-localization-with-umbraco
Hope this helps.
Cheers,
Michael.
@Michael,
Thanks Michael, I'll try the global.asax method. I took a look a the code examples in your links, but I didn't see a code example for the global.asax file.
@Jan
Thanks Jan, I implemented a language selector on the site, so if the visitor doesn't want the language set by his browser, he can choose another language. I also use language dictionary items to dynamically localize labels, buttons, etc ...
Thanks to both Michael and Jan, you rock,
Anthony
Hi Anthony,
The examples I linked to are not implemented in the global.asax file indeed, but I think you can more or less copy-paste the examples under the Application_BeginRequest method of your global.asax file.
if I don't forget, I'll check tonight at home how I did it exactly and post the code if it can help you.
Cheers,
Michael.
@Michael, Ok Michael, I'll also take a look at it tonight. Thanks for your help.
Hi Michael,
I have implemented the browser language detection script in my master template, but - sadly enough - it doesn't work on my site. Am I overlooking something? Maybe I should also configure the web.config file?
Thanks for your help,
Anthony
Hi Anthoy,
Can you maybe post the code? It might help identifying the problem. Also, can you maybe specify what exactly does not work? Do you get an error? Do you see the pages displayed in a wrong language?
Have you set the hostnames correclty in you content tree?
I don't think there is anything to do in the web.config. The only thing you can do there is specify a default culture, but I don't think it will be of much help for your problem.
Cheers,
Michael.
Hi Michael,
I pasted this script in the <head> section of my master template:
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e) {
if(Request.UserLanguages != null && Request.ServerVariables["script_name"] == "/") {
string language = Request.UserLanguages[0];
if(!string.IsNullOrEmpty(language)) {
//Get the culture name - and make sure it's in the RFC 1766 standard <languagecode2>-<country/regioncode2>
//i.e. en-GB, so it's easier to check for
if (language.Length < 3)
language += "-" + language.ToUpper();
//Redirect to the correct page based on language, default to english
switch(language) {
case "en-US":
Response.Redirect("/home.aspx");
break;
case "nl-BE":
Response.Redirect("/startpagina.aspx");
break;
case "fr-BE":
Response.Redirect("/acceuil.aspx");
break;
default:
Response.Redirect("/home.aspx");
break;
}
}
}
}
</script>
This are the hostnames for my websites:
195.130.154.107/home.aspx (en-US)
195.130.154.107/startpagina.aspx (nl-BE)
195.130.154.107/accueil.aspx (fr-BE)
I don't get an error or something, but I guess the script doesn't get executed because if I change the language settings of my browser to nl-BE or fr-BE it's always the English homepage (en-US) that get's loaded.
Thanks for your help,
Anthony
Have you tried this new package?
http://our.umbraco.org/projects/website-utilities/language-selector
yes, I just installed it this morning. A great package, works like a charm:
http://195.130.154.107
But my problem is that my customer also wants browser detection, so that the language is chosen based on the language settings in the browser.
greetings,
Anthony
Hi Anthony,
This is strange indeed :-S Maybe you can try to write out/log some debug information like the value of Request.UserLanguages, Request.ServerVariables["script_name"] and also the value of the variable "language" used in the switch. This might help in identifying what is goign wrong. My first guess is that, for some reason, the language value does not match with the switch values. Could it be that you get fr-FR and nl-NL instead of fr-BE and nl-BE?
Just a side note: for French I think there is a spelling mistake in the redirect : "/acceuil.aspx" instead of "/accueil.aspx" as you mention in your hostnames list.
Cheers,
Michael.
Maybe another check to make sure: when you add your language in your browser settings, do you then move it up as first in the list? Because the script only checks on the first available language :-)
Cheers,
Michael.
Hi Michael,
Thanks for your advice. How can I write out this debug information. Can I do this via the Umbraco Backoffice or do I need to do this via 'attach to proces' in Visual Studio?
I misspelled indeed 'acceuil.aspx' it should be 'accueil.aspx'.
Thanks,
Anthony
Hi Anthony,
I think the quickest way is indeed to debug in Visual Studio by attaching to process and then put some breakpoints in your code.
Alternatively, if you want to test "live", you could write the output into some hidden fields you generate from your code, via "Response.Write" or something similar. That way you can check the source of the page and see the values without having anything displayed in the browser. You can use the same mechanism to display Javascript alerts or anything of that kind.
But debugging in Visual Studio will also allow you to browse directly through the whole objects states, so I think it is the best way of doing in this case.
Cheers,
Michael.
Hi Michael,
I was able to debug the script in Visual Studio and immediately saw the problem:
//Get the culture name - and make sure it's in the RFC 1766 standard <languagecode2>-<country/regioncode2>
//i.e. en-GB, so it's easier to check for
if (language.Length < 3)
language += "-" + language.ToUpper();
the string value of the language variable held 5 characters (eg nl-be) so this condition was never met
Also this condition:
if(Request.UserLanguages != null && Request.ServerVariables["script_name"] == "/")
was never met, as the value of Request.ServerVariables["script_name"] was "/default.aspx"
So I adjusted the script and now it works:
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e) {
if(Request.UserLanguages != null && Request.ServerVariables["script_name"] == "/default.aspx") {
string language = Request.UserLanguages[0];
if(!string.IsNullOrEmpty(language)) {
//Get the culture name - and make sure it's in the RFC 1766 standard <languagecode2>-<country/regioncode2>
//i.e. en-GB, so it's easier to check for
if (language.Length < 3)
language += "-" + language.ToUpper();
//Redirect to the correct page based on language, default to english
switch(language) {
case "en-us":
Response.Redirect("/home.aspx");
break;
case "nl-be":
Response.Redirect("/startpagina.aspx");
break;
case "fr-be":
Response.Redirect("/accueil.aspx");
break;
default:
Response.Redirect("/home.aspx");
break;
}
}
}
}
</script>
You can check this out here:
http://195.130.154.107
Tonight I'll bring this site to the production server
greetings,
Anthony
Hi Anthony,
I'm glad to hear you sorted it out by doing the debugging :-)
Maybe one last thought for the road: is there a specific reason why you actually check on "fr-BE" and "nl-BE" and not just "fr" or "nl"? Because this means that someone with language settings "fr-FR" or "nl-NL" will be displayed the English version by default, which is maybe not your intention...
Good luck with the launch of your new website tonight!
Cheers,
Michael.
Hi Michael,
Thanks for the tip. I debugged the code again, this line:
string language = Request.UserLanguages[0];
result in a string value like this 'nl-be'
so I added this line of code:
language = language.Substring(0, 2);
so this gives me back the value 'nl'
now I can do a switch case statement like this:
switch(language) {
case "en":
Response.Redirect("/home.aspx");
break;
case "nl":
Response.Redirect("/startpagina.aspx");
break;
case "fr":
Response.Redirect("/accueil.aspx");
break;
default:
Response.Redirect("/home.aspx");
break;
and this works also, and as you mentioned, is more robust code.
Thanks again,
Anthony
You're welcome Anthony!
Glad I could help :-)
Cheers,
Michael.
is working on a reply...