Fandom

Wikia Developers Wiki

i18n functions

  • Cqm

    I've made a couple of simple functions to work with an object of translations that seem to be fairly standard here. I'd appreciate it if people more familiar with translation fallbacks had any pointers on how to improve that behaviour. I'm also interested in whether people think it should fallback to wgContentLanguage if wgUserLanguage fails on first try.

    Otherwise, feel free to use them in your code :)

    function get_lang_fallback(lang) {
    	if (lang.indexOf('-') > -1) {
    		// split to get parent language
    		return lang.split('-')[0];
    	}
     
    	return 'en';
    }
     
    function msg(i18n, key, lang) {
    	lang = lang || mw.config.get('wgUserLanguage');
     
    	if (i18n[lang] && i18n[lang][key]) {
    		return i18n[lang][key];
    	}
     
    	lang = get_lang_fallback(lang);
            return msg(lang);
    }

    Sample usage:

    var i18n = {
        en: {
            x: 'EN'
        },
        pt: {
            x: 'PT'
        }
        'zh-hans': {
            x: 'ZH-HANS'
        }
    };
     
    // assuming a user language of zh-hans
    msg(i18n, x); // ZH-HANS
    // assuming a user language of pt-br
    msg(i18n, x); // PT
    // assuming a user language of de
    msg(i18n, x); // EN

    Update: These have become part of a much more flexible script with much bigger goals in mind. Check it out at I18n-js!

      Loading editor
    • It looks pretty good to me! :) If I think of some suggestions, I would be sure to let you know :).

        Loading editor
    • Although you've already did it, a suggestion I was thinking of was to condense the code. You've done that (looking at your history) so you look good. :)

        Loading editor
    • Likely just an oversight, but it seems if get_lang_fallback() is used, the fallback language code is returned rather than the message from the fallback language.

      Regarding a wgContentLanguage fallback: the use case for it is fairly specific (a logged-in Russian (or anything but English) user visiting a wiki that isn't Russian or English), so I suppose it would depend whether the wiki's language or English is the more desirable fallback. What language does MediaWiki use in such a situation?

        Loading editor
    • Cqm

      @OneTwoThreeFall Thanks, it was indeed an oversight.

      Truth be told, I have no idea what the behaviour is in MediaWiki itself. I know they use smarter fallback logic in general, e.g. there isn't any parent zh language code to fall back to so use something else afaik, but a lot of that is hardcoded iirc.

        Loading editor
    • OneTwoThreeFall wrote:
      Likely just an oversight, but it seems if get_lang_fallback() is used, the fallback language code is returned rather than the message from the fallback language.

      Regarding a wgContentLanguage fallback: the use case for it is fairly specific (a logged-in Russian (or anything but English) user visiting a wiki that isn't Russian or English), so I suppose it would depend whether the wiki's language or English is the more desirable fallback. What language does MediaWiki use in such a situation?

      Hmm... I didn't know this and I've been doing i18n functions for a while. Good to know! :)

        Loading editor
    • MediaWiki uses a fallback chain dependent on language similarity and a bit of politics. For example, Spanish is linguistically similar to Portuguese, and so both Spanish and Portuguese may be designed to fallback to each other. Ukrainian used to fall back to  Russian , but due to politics now falls back to English. In some cases having the fallback to similar languages may be confusing because the written language may look similar but have completely different meaning. There are of course exceptions and other historical reasons for some of the fallback chains.

      I'm not sure why you're reinventing the wheel though, you can just check the fallback straight from mediawiki itself unless you want to avoid an extra api request. That can be avoided by saving the user's fallback to local storage.

      Anyway, see "fallback" in https://www.mediawiki.org/wiki/API:Siteinfo:

      https://fr.wikipedia.org/wiki/Sp%C3%A9cial:ApiSandbox#action=query&format=json&prop=&meta=siteinfo&siprop=general

      https://www.mediawiki.org/wiki/File:MediaWiki_fallback_chains.svg

        Loading editor
    • Cqm

      @Dessamator I realised after a little digging that my implementation was basic and in some cases broken. I'm now working on mirroring what is currently used on Wikia in terms of fallbacks, see User:Cqm/LangMap. That fallback image is very helpful for doing that, so thanks for pointing it out.


      Edit: Turns out it was a million time easier and quicker to grep through the message definitions for anything that defined a fallback.

        Loading editor
    • I don't think it will work very well unless someone commits to updating these forever. Fallbacks are not only linked to language similarity, they also depend on languages spoken in a certain area, e.g. some Belgium citizens  know French, English,  Arabic, and so forth. So it would fallback to one of those if the language in question is a minor language spoken mostly in Belgium.

      It also gets updated from time to time, so considering that it is likely that Wikia doesn't update its mapping they will always be behind. 

      Anyway, for the simple fallback case this might be good enough as it is likely that wikia doesn't use  as many languages as Wikimedia.

        Loading editor
    • Cqm

      @Dessamator If the fallbacks ever do get updated, then I'm perfectly happy to maintain them. However, as Wikia seldom touches the related files in their repository and alters the fallbacks even less (the last change was from years ago and I'm not entirely sure it was made by them), I think it's safe to say they won't be changing for the foreseeable future.

      Any changes that do occur, will likely come from MediaWiki core who almost religiously document such things, so it won't be too hard to figure out what changed.

        Loading editor
    • A Fandom user
        Loading editor
Give Kudos to this message
You've given this message Kudos!
See who gave Kudos to this message

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.