Skip to content

Instantly share code, notes, and snippets.

@jlgrall
Last active January 28, 2016 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jlgrall/3353697 to your computer and use it in GitHub Desktop.
Save jlgrall/3353697 to your computer and use it in GitHub Desktop.
i18next: Adds support for data-i18n and data-i18n-options to jQuery UI Dialog's title
// Adds support for data-i18n and data-i18n-options to jQuery UI Dialog's title:
if( $.fn.dialog ) { // Checks the presence of the dialog component of jQuery UI
var rTitleKey = /\[title\]([^;]*);?/,
// Keep a reference to the original _create function:
dialog_create_orig = $.ui.dialog.prototype._create;
$.ui.dialog.prototype._create = function( ) {
var data_i18n,
i18n_options;
if( !this.options.title ) { // Because as defined in Dialog, the options.title should override the title attribute
var old_data_i18n = this.element.attr( "data-i18n" );
if( old_data_i18n ) {
old_data_i18n = old_data_i18n.replace( rTitleKey, function( match, p1 ) {
data_i18n = p1;
return "";
} ).trim();
if( data_i18n ) {
if( old_data_i18n.length ) this.element.attr( "data-i18n", old_data_i18n );
else this.element.removeAttr( "data-i18n" );
}
i18n_options = this.element.data( "i18n-options" );
}
}
dialog_create_orig.apply( this, arguments );
if( data_i18n || i18n_options ) {
var elementTitle = $( ".ui-dialog-title", this.uiDialogTitlebar );
if( data_i18n ) elementTitle.attr( "data-i18n", data_i18n );
if( i18n_options ) elementTitle.data( "i18n-options", i18n_options );
}
};
// Keep a reference to the original destroy function:
var dialog_destroy_orig = $.ui.dialog.prototype.destroy;
$.ui.dialog.prototype.destroy = function( ) {
var elementTitle = $( ".ui-dialog-title", this.uiDialogTitlebar ),
data_i18n = elementTitle.attr( "data-i18n" ),
i18n_options = elementTitle.data( "i18n-options" );
if( data_i18n ) {
var old_data_i18n = this.element.attr( "data-i18n" );
data_i18n = ( old_data_i18n ? old_data_i18n + ";" : "" ) + "[title]" + data_i18n;
this.element.attr( "data-i18n", data_i18n );
}
if( i18n_options ) {
var old_i18n_options = this.element.data( "i18n-options" );
if( old_i18n_options ) i18n_options = $.extend( old_i18n_options, i18n_options);
this.element.data( "i18n-options", i18n_options );
}
dialog_destroy_orig.apply( this, arguments );
};
}
@jlgrall
Copy link
Author

jlgrall commented Aug 15, 2012

@jlgrall
Copy link
Author

jlgrall commented Jan 28, 2016

Updated to fix a bug that could lead to the content of the dialog being removed with i18next v1.7.0+

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