Home > Uncategorized > Localise a web page via Javascript

Localise a web page via Javascript

Here is a way to simply detect the user’s language,and switch the content for the local version. As a caveat, this system will show English before changing to the local language, and it’s not good for SEO. You can get rid of the change from english by removing visible text in the HTML, but this also serves as a failover for users who don’t speak any of the supported languages.

<span id=”btnBuy”>Download Software</span>

<script language=”javascript”>

var translations = {
“en” : {
“btnBuy” : “Download Software”
},
“fr” : {
“btnBuy” : “Télécharger le logiciel”,
}};

function changelanguage(httpheaders)

{
var lang=httpheaders[“Accept-Language”];
lang = lang.split(“,”)[0].substring(0,2);
console.log(lang);
for(var i in translations[lang])
{
var translation = translations[lang][i];
console.log(i + “->” + translation);
document.getElementById(i).innerHTML = translation;
}
}
</script>
<script src=”http://ajaxhttpheaders2.appspot.com/?callback=changelanguage”></script&gt;

Kudos to Dan Singerman for the appengine script.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: