Skip to content

Instantly share code, notes, and snippets.

@guozheng
Created July 22, 2011 07:28
Show Gist options
  • Save guozheng/1099029 to your computer and use it in GitHub Desktop.
Save guozheng/1099029 to your computer and use it in GitHub Desktop.
multi-lang detection js script sample (from http://jsfiddle.net/ambiguous/hDM3T/2/)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Multi-lang Testing Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<!-- Date: 2011-07-21 -->
</head>
<body>
<div lang="en" class="wrapper">
<h1>English</h1>
<p>Here is some English content.</p>
</div>
<div lang="fr" class="wrapper">
<h1>Française</h1>
<p>Voici le contenu en français.</p>
</div>
<div lang="pt" class="wrapper">
<h1>Português</h1>
<p>Aqui você encontra o conteúdo Português.</p>
</div>
<script type="text/javascript">
var known = {
en: true,
fr: true,
pt: true
};
// Figure out the language, default to English because that's
// what I speak.
var lang = (navigator.language || navigator.userLanguage || 'en').substr(0, 2);
if(!known[lang])
lang = 'en';
// Find all <div>s with a class of "wrapper" and lang attribute equal
// to `lang` and make them visibile.
$('div.wrapper[lang=' + lang + ']').show();
// Find all <div>s with a class of "wrapper" and lang attribute not
// equal to `lang` and make them invisibile.
$('div.wrapper[lang!=' + lang + ']').hide();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment