Wikia

Community Central

WatchlistRandom pageRecent changes

Template changing depending on viewer's language

63,506pages on
this wiki

Forum page

Forums: Index Help desk Template changing depending on viewer's language
Wikia's forums are a place for the community to help other members.
To contact staff directly or to report bugs, please use Special:Contact.
Note: This topic has been unedited for 1822 days. It is considered archived - the discussion is over. Do not add to unless it really needs a response.

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:

{{ #switch: ??? | en = Something | fr = Quelque chose | sv = Någonting }}

Now the problem is, what is the key value ("???" above) supposed to be? – Smiddle / talk 18:39, 21 May 2007 (UTC)

That would be {{CONTENTLANGUAGE}} ― Thailog 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 {{USERNAME}}, 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 {{#switch}} 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 {{#switch}} 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.—This unsigned comment is by Smiddle (wallcontribs) . Please sign your posts with ~~~~!

Ciencia Al Poder solution

For adding in MediaWiki:Common.js:


function loadLangSelector(){
  var se = document.getElementsByTagName('span');
  for (var i = 0; i < se.length; i++){
    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{
          le[j].style.display = 'none';
          if (le[j].lang == '') def = le[j];
        }
      }
      if (!matched && def)
        def.style.display = 'inline';
    }
  }
}

addOnloadHook(loadLangSelector);

code updated Ciencia Al Poder (talk) -@WikiDex 14:48, 25 May 2007 (UTC) Code updated --Ciencia Al Poder (talk) -@WikiDex 07:53, 26 May 2007 (UTC)

To use in a page:

<span class="langselect">
<span lang="en">Hello</span>
<span lang="es">Hola</span>
<span>Default if none is matched</span>
</span>

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)

Ok, Ciencia is a dumbass and make some mistakes in the code. But now it works. As alternative to specify style="display:none" in each inner span to prevent displaying it before the page loads and the script is executed, or if the script is not exeuted, you can add in MediaWiki:Common.css:

 .langselect span {
   display: none;
 }

--Ciencia Al Poder (talk) -@WikiDex 14:48, 25 May 2007 (UTC)

Hmm, note I used the element span, but it's a inline element, so if you plan to insert block elements in it (like tables or divs) you must change span into div, and change 'display:inlite' to 'display:block' in the code, but then it would be treated as a block element (the next element or text will go below and not next to the div). Other solution is to duplicate the code:
function loadLangSelector(){
  var se = document.getElementsByTagName('span');
  for (var i = 0; i < se.length; i++){
    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{
          le[j].style.display = 'none';
          if (le[j].lang == '') def = le[j];
        }
      }
      if (!matched && def)
        def.style.display = 'inline';
    }
  }
  dc = document.getElementById('content');
  if (!dc) return;
  se = dc.getElementsByTagName('div');
  for (var i = 0; i < se.length; i++){
    if ((' '+se[i].className+' ').indexOf(' langselect ') != -1){
      var le = se[i].getElementsByTagName('div');
      var matched = false;
      var def = false;
      for (var j = 0; j < le.length; j++){
        if (le[j].lang == wgUserLanguage){
          le[j].style.display = 'block';
          matched = true;
        }else{
          le[j].style.display = 'none';
          if (le[j].lang == '') def = le[j];
        }
      }
      if (!matched && def)
        def.style.display = 'block';
    }
  }
}

Code updated --Ciencia Al Poder (talk) -@WikiDex 07:53, 26 May 2007 (UTC) ... and use div or span (in all elements implicated, the container and the inner elements) without taking care of it. --Ciencia Al Poder (talk) -@WikiDex 15:00, 25 May 2007 (UTC)

It almost works.
<span class="langselect">
<span lang="ru">cough</span>
<span lang="en">blah</span>
<span lang="fr">meh</span>
<span>argh</span>
</span>
If the above code is applied, and I'd have my interface language set to Russian, Enlish or French, either cough, blah or meh would show up. If I wouldn't, nothing showed up, and not "argh". But still, it's great. – Smiddle / talk 20:32, 25 May 2007 (UTC)
Ok, I forgot to put def.style.display in the last sentence. Now the default will also work. --Ciencia Al Poder (talk) -@WikiDex 07:53, 26 May 2007 (UTC)
The default doesn't work for me. Neither in IE7 or MZFF.Smiddle / talk 09:12, 26 May 2007 (UTC)
It works with div tags, but not spans. – Smiddle / talk 09:21, 26 May 2007 (UTC)
The only thing left now is to fix the linebreakers. – Smiddle / talk 09:38, 26 May 2007 (UTC)
It works for me in Wikia:Sandbox. Please, update your code with the last code in this page and try again. You forgot to add a def.style.display. Delete the first loadLangSelector() function definition, and put the addOnloadHook(loadLangSelector); before the function definition. I have it installed too at the end of User:Ciencia_Al_Poder/monobook.js. And you can put all the spans in the same line. The line breaks is only to show the structure --Ciencia Al Poder (talk) -@WikiDex 09:53, 26 May 2007 (UTC)

Latest Photos

Add a Photo
7,469photos on this wiki
See more >

Recent Wiki Activity

See more >

Around Wikia's network

Random Wiki