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>
Kudos to Dan Singerman for the appengine script.