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.
I could see doing it in one object, like this:
Assuming there’s no overlap in option names between plugins, you could just start your plugins with:
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():If you really wanted to keep the plugin options separate, just create another level:
Then:
Finally, you could get fancy and make it easier to get different languages by storing them all in one multi-layered object:
And
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:
This would output a different set, depending on the page I’m on.