Forum:Template changing depending on viewer's language

Hello. I'd like to have a template which will switch to a certain thing depending on the skin language of the viewer. I know the basics: Now the problem is, what is the key value ("???" above) supposed to be? – Smiddle / talk 18:39, 21 May 2007 (UTC)
 * That would be &#123;&#123;CONTENTLANGUAGE&#125;&#125; ―  T hailog  19:07, 21 May 2007 (UTC)
 * I thought that was the wiki's language? ~Dantman(talk) tricks May 21, 2007 @ 19:16 (UTC)
 * Yeah, I've seen some other one trying with CONTENTLANGUAGE on a French wiki. No matter what language the skin was set to; it always showed French. – Smiddle / talk 20:03, 21 May 2007 (UTC)
 * There is no such magic word and probably never will be (example of it being removed by brion). There are cache issues apparently. You'll have to rely on javascript. There are two variables that return user language and content language (below). --Splarka (talk) 06:42, 22 May 2007 (UTC)

var wgUserLanguage = "en"; var wgContentLanguage = "en";


 * At Uncyclopedia, I once tried doing something with, which is made completely out of JavaScript. It didn't work. But I'll try that. How do I activate the tag? – Smiddle / talk 14:55, 22 May 2007 (UTC)
 * Well, that isn't as simple. You can't use javascript to make a new magic word (as parserfunctions like are server-side and javascript is client-side). --Splarka (talk) 06:14, 23 May 2007 (UTC)
 * So... what should I do to make this work? – Smiddle / talk 16:11, 23 May 2007 (UTC)
 * Well, give me a specific example of what you need to switch. Remember, this has to be processed client-side so all the info must be available (unlike which can use transclusions and #ifexist's). --Splarka (talk) 06:23, 24 May 2007 (UTC)
 * Making something change to "dansk" in Norwegian or Danish, "danskt" in Faroese, and "danska" in Icelandic or Swedish on c:Scanime perhaps. That would be useful. – Smiddle / talk 15:00, 24 May 2007 (UTC)

Would defining something in the MediaWiki namespace and just transcluding that work? -Afker 16:51, 24 May 2007 (UTC)
 * No, since that is related to the wiki's language and not the viewer's.

Ciencia Al Poder solution
For adding in MediaWiki:Common.js:

function loadLangSelector{ var se = document.getElementById('content').getElementsByTagName('span'); for (var i = 0; i < se.length; se++){ if ((' '+se[i].className+' ').indexOf(' langselect ') != -1){ var le = se[i].getElementsByTagName('span'); var matched = false; var def = false; for (var j = 0; j < le.length; j++){ if (le[j].lang == wgUserLanguage){ le[j].style.display = 'inline'; matched = true; }else if (le[j].lang == ''){ def = le[j]; }else{ le[j].style.display = 'none'; }     }      if (!matched && def) def.display = 'inline'; } } }

addOnloadHook(loadLangSelector);

To use in a page:

Hello Hola Default if none is matched

Whenever a span element with class langselect is found in the page, it would walk for all inner spans, and will change the visibility property to "hidden", so it won't be shown. If a span which lang attribute matches the wgUserLanguage variable (defined in each page and with the lang code set by the user) it will be shown to the reader. If none was matched, then the last span element with no lang attribute will be shown to the user (if there's one). Note that all the languages will be shown if the javaScript is not executed (disabled or in print version). You could add style="display:none;" to all inner spans so none will be shown if JavaScript isn't enabled. This code was not tested. It should work, but I didn't tested it, so you must test it adding the code in Special:Mypage/monobook.js --Ciencia Al Poder (talk) -@WikiDex 18:31, 24 May 2007 (UTC)
 * Thanks a bunch. Testing now. – Smiddle / talk 19:56, 24 May 2007 (UTC)
 * It didn't work for me. All the options showed up in IE7. I'll do it in FireFox. – Smiddle / talk 20:04, 24 May 2007 (UTC)
 * It didn't work either. I've refreshed, too. – Smiddle / talk 20:06, 24 May 2007 (UTC)