Determine the user/country language spoken and siplay accordingly

Greetings,

Is it possible to display a text in different languages (fr, ru, nl) depending on the browser data (may be).

This is what is my requirement,

I will have a fixed page (url) which will display a text. Now if a user check the same page from France the same text should be displayed in french. The page will not change (or url will not change) but only the text will change to country specific language (fetched from a xml may be)

Your kind inputs will be much appriciated. Thank you for your time.

Javascript + HTML +CSS will be better as I do not have knowledge on .php or .aspx . I’m a beginner in JavaScript and have fair bit of knowledge in XHTML and CSS.

Regards

You should do this server-side (e.g., via PHP or similar).

Examine the Accept-Language HTTP header (taking Q values into consideration) and send a 300 (Multiple Choices) header back with a URI (and a Vary header).

Thank you for the quick response. Is it not at all doable through JavaScript?

Regards

It might be, using an ugly kludge, but I would advise against it. It would mean locking out anyone (like me) with JavaScript disabled, for one thing.

The problem is that you can’t access arbitrary HTTP headers using client-side JavaScript. You could use ajax to request the same page and then use the .getAllResponseHeaders() method to find out, and then change location.href based on that. But that would be a horrible kludge.

Server-side is definitely better in this case.

Thanks. I understand, however server side i’m not much sure and i’ve been given a requirement on JavaScript :frowning:

Okay lets say somehow i will have to show the end result, as i have to show up the text in different language I know depending of language code, i guess re-directing can be done?

now i got a script (attached)where i’m able to redirect to different Html pages based on the language code. from an array of language code, it first check with browser then return the value corresponding to the code (some .html file).
i. can i have div placed within the body with ids and can be show or hide depending on the language of browser?

or,
ii. Instead of .html, is it possible to have a xml file which will contains data for different language and ultimately will show up in a div in the body?

Kindly have a check and thanks for your help.

Regards

You can redirect using JavaScript, that’s not the problem. The problem is that you cannot (easily) find out the user’s language preference with JavaScript. (As far as I know.)

Also remember that not everyone has JavaScript enabled, and you can never rely on it being supported. This doesn’t seem to be essential functionality, so it might not matter, as long as there are links where a user can manually go to a version with the desired language.

Your attachment isn’t approved yet, and I probably won’t have time to look at it today, but perhaps someone else will.

currently i have been given requirement on JavaScript. However, i will communicate the disadvantages as you said. Is it possible to post an useful link?

Here is the code,

<html>
<head>
<title> </title>

<script>

//Enter ISO 639-2 letter Language codes to detect (see: http://www.w3.org/WAI/ER/IG/ert/iso639.htm):
var langcodes=new Array(“fr”, “de”, “it”, “pt”, “es”, “default”)

//Enter corresponding redirect URLs (last one is for default URL):
var langredirects=new Array(“fr.html”, “de.html”, “it.html”, “pt.html”, “es.html”, “abc.html”)

var languageinfo=navigator.language? navigator.language : navigator.userLanguage
var gotodefault=1

function redirectpage(dest){
if (window.location.replace)
window.location.replace(dest)
else
window.location=dest
}

for (i=0;i<langcodes.length-1;i++){
if (languageinfo.substr(0,2)==langcodes[i]){
redirectpage(langredirects[i])
gotodefault=0
break
}
}

if (gotodefault)
redirectpage(langredirects[langcodes.length-1])

</script>

</head>

<body>
</body>
</html>

Request to moderator to ignore and not to approve the attached. The code is mentioned in the next post.

Thanks

Please disregard this as i found and deleted the file. Thanks

So if the requirement had been to suspend gravity, would you have made an attempt? :slight_smile:

Okay, the script you have might work, if all browsers provide the language setting in the navigator object. It could be simplified, though. Something like this?


var supportedLanguages = {
    fr: "fr.html",
    de: "de.html",
    it: "it.html",
    pt: "pt.html",
    es: "es.html"
};
var browserLanguage = navigator.language ? navigator.language : navigator.userLanguage;

if (browserLanguage && supportedLanguages[browserLanguage]) {
    if (location.replace) {
        location.replace(supportedLanguages[browserLanguage]);
    } else {
        location.href = supportedLanguages[browserLanguage];
    }
}

You could have this in the default page, so you wouldn’t have to do a redirect at all for unsupported languages.

(N.B., my JavaScript sample is entirely untested, so there might be errors in it. I’m a bit stressed for time.)

Well, I would have tried best to get it done and had searched/consult on net for best solution.

Well, coming to the modified code is not working :frowning:

And Please let me know if this can be done and how.

i. depending on the browser language show / hide div (instead redirecting to another html page)
or
ii. depending on the browser language fetch particular data from xml

(i dont want to have another lot of html for different language)

Thanks for looking into this.

Regards

Now if a user check the same page from France the same text should be displayed in french.

Oh, great. So when I’m in Pisa to attend the conference, and I want to check your site out, I have to figure out where the Engrish button is when everything’s in Italian?
Or, someone’s in Spain but they want to view the page in Catalan. Are you going to do regions?
If they are in Belgium, will you display the page in French or in Dutch? The Dutch speakers generally don’t speak French and the French speakers don’t seem to know Dutch. What about Quebec, Canada? Miami, Florida?

Google uses geo-something to determine which version of google I get. If I type in Google.com, I get Google.nl, and unfortunately it will give me google.nl results, which suck balls compared to google.com (.us) results as far as HTML/CSS etc go. If I were looking for local stuff, it would be helpful (and so sometimes it is what I want). Luckily, I can trick the evil googles by specifically requesting google.us.

If the language were to appear in the url, then users could at least change it there. But if it’s purely done in Javascript, you’ll have to script in a button people can easily find to change languages, and then remember that language between pages (so, maybe a cookie or something).

It’s hardly “tricking the evil googles” :wink: … on google.nl they give you a link to the international version (google.com/ncr does the trick) - because they recognise that while they’re trying to be helpful, sometimes it isn’t!

Greetings,

Thanks to all of the members for your great support and feedback.

The job is finally done.