Jquery - Creating a general language file to declare defaults for any plugins

internationalization, javascript, jquery

Solution

I could see doing it in one object, like this:

var en = {
    defaultLoadingText: "Loading...",
    defaultSavingText: "Saving...",
    defaultDoneText: "Done",
    defaultRedirectText: "Redirecting...",
    defaultErrorText: "Oops! Our bad, something wrong.";
    moreText: "Read more", 
    lessText: "hide"
};

Assuming there's no overlap in option names between plugins, you could just start your plugins with:

$('').ajaxStatus(en);
$('').shrinker(en);

Most plugins take in an options object and ignore the extra properties. If you have some other options you want to pass to a particular plugin, you just use `$.extend()`:

$('').ajaxStatus($.extend({}, { success: function () {} }, en));

If you really wanted to keep the plugin options separate, just create another level:

var en = {
    ajaxStatus: {
        defaultLoadingText: "Loading...",
        defaultSavingText: "Saving...",
        defaultDoneText: "Done",
        defaultRedirectText: "Redirecting...",
        defaultErrorText: "Oops! Our bad, something wrong.";
    },
    shrinker: {
        moreText: "Read more", 
        lessText: "hide"
    }
};

Then:

$('').ajaxStatus(en.ajaxStatus);
$('').shrinker(en.shrinker);

Finally, you could get fancy and make it easier to get different languages by storing them all in one multi-layered object:

var localizedOptions = {
    'en-US': {
        ajaxStatus: {
            defaultLoadingText: "Loading...",
            defaultSavingText: "Saving...",
            defaultDoneText: "Done",
            defaultRedirectText: "Redirecting...",
            defaultErrorText: "Oops! Our bad, something wrong.";
        },
        shrinker: {
            moreText: "Read more", 
            lessText: "hide"
        }
    },
    'es-ES': {
        ajaxStatus: {
            defaultLoadingText: "Cargar...",
            defaultSavingText: "Guardar...",
            defaultDoneText: "Terminado",
            defaultRedirectText: "Redirigir...",
            defaultErrorText: "¡Ay! Nuestra algo malo, malo.";
        },
        shrinker: {
            moreText: "Leer más", 
            lessText: "esconder"
        }
    } // ... etc.
};

And

$('').ajaxStatus(localizedOptions[navigator.language || navigator.userLanguage].ajaxStatus);

I must admit I haven't used something like this myself, because I usually get language files in the form of a resource file. In that case, I'll just output a single collection of key/value pairs via server-side logic and manually set the appropriate plugin properties.

In ASP.NET MVC:

<script>
var Resources = {};
@foreach (var kvp in ViewBag.Resources) {
Resources['@kvp.Key'] = "@kvp.Value";
}
</script>

This would output a different set, depending on the page I'm on.

Problem

Like most people, I use a bunch of jquery plugins on my website. From Twitter bootstrap to jquery ui datepicker and so on. But I need my website to be multilingual, so I created a general language file called english.js , and in that file I want to declare some of the defaults from plugins as well as other language variables. The problem is that all these plugins have varying structures to declare defaults. I saw a couple of questions already ( jquery - setting plugin defaults?, Globally defining jQuery plugin parameters , jQuery plugin defaults , ...). But they are not clear. STRUCTURE 1 ``` $.fn.ajaxStatus = function (params) { var settings = $.extend( { defaultLoadingText :"Loading...", defaultSavingText :"Saving...", defaultDoneText :"Done", defaultRedirectText:"Redirecting...", defaultErrorText :"Oops! Our bad, something wrong."; },$.fn.ajaxStatus.defaults, params), }); ``` STRUCTURE 2 ``` jQuery.fn.extend({ shrinker:function (options) { var opts = $.extend({ "moreText":"Read more", "lessText":"hide", }, $.fn.shrinker.defaults, options); }); ``` THE QUESTION Without modifying the plugin, is it possible to assign some defaults for the function that will be used every time I use the plugin? How can I define the language defaults in an external file? Right now, I have this in my language file, but it feels wrong, is this how you do it? ``` if ($.fn.ajaxStatus !== undefined) { $.fn.ajaxStatus.defaults = { defaultLoadingText :"Loading2...", defaultSavingText :"Saving2...", defaultDoneText :"Done2", defaultRedirectText:"Redirecting2...", defaultErrorText :"Oops! Our bad, something wrong" }; } ``` Thanks in advance for you help.

Original source