Skip to content

Instantly share code, notes, and snippets.

@jlgrall
Created August 14, 2012 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlgrall/3352659 to your computer and use it in GitHub Desktop.
Save jlgrall/3352659 to your computer and use it in GitHub Desktop.
i18next: Adds support for data-i18n and data-i18n-options to jQuery UI Buttons
// Adds support for data-i18n and data-i18n-options to jQuery UI Buttons:
if( $.fn.button ) { // Checks the presence of the button component of jQuery UI
// Keep a reference to the original _create function:
var button_create_orig = $.ui.button.prototype._create;
$.ui.button.prototype._create = function( ) {
button_create_orig.apply( this, arguments );
if( this.type === "button" ) { // Not needed for input, radio or checkbox
var data_i18n = this.buttonElement.attr( "data-i18n" ),
i18n_options = this.buttonElement.data( "i18n-options" );
// If found, apply the attribute and datas to the button label:
if( data_i18n || i18n_options ) {
var elementButtonText = this.buttonElement.find( ".ui-button-text" );
this.buttonElement.removeAttr( "data-i18n" ).removeData( "i18n-options" );
if( data_i18n ) elementButtonText.attr( "data-i18n", data_i18n );
if( i18n_options ) elementButtonText.data( "i18n-options", i18n_options );
}
}
};
// Keep a reference to the original destroy function:
var button_destroy_orig = $.ui.button.prototype.destroy;
$.ui.button.prototype.destroy = function( ) {
if( this.type === "button" ) {
var elementButtonText = this.buttonElement.find( ".ui-button-text" ),
data_i18n = elementButtonText.attr( "data-i18n" ),
i18n_options = elementButtonText.data( "i18n-options" );
// If found, apply the attribute and datas to the button element:
if( data_i18n ) this.buttonElement.attr( "data-i18n", data_i18n );
if( i18n_options ) this.buttonElement.data( "i18n-options", i18n_options );
}
button_destroy_orig.apply( this, arguments );
};
}
@jlgrall
Copy link
Author

jlgrall commented Aug 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment