Skip to main content

jQuery Rotate Through Other Languages

By Sam Deering

JavaScript

Share:

😳 Ever felt a little embarassed sharing your JavaScript? Let's fix it.

text-changes-automatically

jQuery code snippet to show other languages link (say in the footer) of your web page. It randomly shows other languages available with a link to change the language of the website. It is set to change every 5 seconds but this can be optimised as well as the array of languages displayed.

jQuery Code

(function (b) {
	var c = ['<span lang="ar">العربية</span>', '<span lang="el">Ελληνικά</span>', '<span lang="pl">Polski</span>', '<span lang="bs">Bosanksi</span>', '<span lang="id">Bahasa Indonesia</span>', '<span lang="ru">Русский</span>', '<span lang="zh">中文</span>', '<span lang="it">Italiano</span>', '<span lang="sr">српски</span>', '<span lang="hr">Hrvatski</span>', '<span lang="ja">日本語</span>', '<span lang="es">Español</span>', '<span lang="fr">Français</span>', '<span lang="ko">한국어</span>', '<span lang="tl">Tagalog</span>', '<span lang="de">Deutsch</span>', '<span lang="fa">فارسی</span>', '<span lang="vi">Tiếng Việt</span>'];
	var a = document.doctype ? document.doctype.publicId : document.firstChild.data;
	var d = a.indexOf("XHTML") > 0;
	updateLanguage = function () {
		b("#languages a").empty().append(c[Math.floor(Math.random() * c.length)] + " (Other languages)");
		if (d) {
			b("#languages span").attr("xml:lang", b("#languages span").attr("lang"))
		}
	};
	updateLanguage();
	window.setInterval(updateLanguage, 5000)
})(jQuery)

HTML Code

<li id="languages"><a href="[yourlink]">Other languages</a></li>

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

💁‍♀️ Fun Fact: Python was ranked #2 on a recent highest paid coders list. *


🤓 Ok. When did a code editor from Microsoft become kinda cool!?