• This plugin (Table of Contents Plus) doesn’t work with TranslatePress. On a translated web site, I click the translated version of the “hide” link, and the TOC flashes out of sight for an instant before appearing again.

    I think that the problem is that TranslatePress translates the “show” and “hide” text in the HTML, but the TOCPlus javascript code is still looking for the original text.

    For example, in lines 141 and 142 of table-of-contents-plus/front.js, this code appears:

    switch( $(this).html() ) {
    case $(‘<div/>’).html(tocplus.visibility_hide).text():

    That’s testing “Is the text in the HTML equal to the text that we expect to appear for the visibility_hide option?”

    I suggest that, instead of relying on the html text to make decisions like this, it would be better to rely on the presence or absence of a class.

    • This topic was modified 3 years, 4 months ago by apbarrett.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter apbarrett

    (@apbarrett)

    I have a proof of concept that uses

    $(‘span.toc_toggle a’).addClass(‘toc_toggle_show’)

    or

    $(‘span.toc_toggle a’).addClass(‘toc_toggle_hide’)

    to set a class on the hide/show link, and uses

    if ($(this).hasClass(‘toc_toggle_hide’)) { … } else { … }

    to test the class. Now it works with the TranslatePress plugin.

    Hello @apbarrett,

    I concur (having the same problem of yours with my translated version via TranslatePress).

    I would be interested in having the complete code you’re using, even if it implies changing the core JS code of the plugin.

    Could you share it, before it gets included (hopefully) in next update ?

    Thread Starter apbarrett

    (@apbarrett)

    Here’s my code, and instructions for editing your installation.

    First, make a backup of your site.

    Then go to the admin interface of your WordPress site. In the side menu, select Plugins -> Plugin Editor. In the plugin editor, select Table of Contents Plus. In the list of files, select front.js.

    In the editor window, scroll down until about line 133, where you should see this

    
    $('#toc_container p.toc_title').append(' <span class="toc_toggle">[<a href="#">' + visibility_text + '</a>]</span>');
    

    Replace everything from that line until the end of file (around line 170) with the following code:

    
                       $('#toc_container p.toc_title').append(' <span class="toc_toggle">[<a href="#">' + visibility_text + '</a>]</span>');
    			if ( visibility_text == tocplus.visibility_show ) {
    				$('ul.toc_list').hide();
    				$('#toc_container').addClass('contracted').shrinkTOCWidth();
    				$('span.toc_toggle a').addClass('toc_toggle_show');
    			} else {
    				$('span.toc_toggle a').addClass('toc_toggle_hide');
    			}
    	
    			$('span.toc_toggle a').click(function(event) {
    				event.preventDefault();
    				if ($(this).hasClass('toc_toggle_hide')) {
    					    $(this).removeClass('toc_toggle_hide');
    						$(this).addClass('toc_toggle_show');
    						$(this).html(tocplus.visibility_show);
    					
    						if ( $.cookie ) {
    							if ( invert )
    								$.cookie('tocplus_hidetoc', null, { path: '/' });
    							else
    								$.cookie('tocplus_hidetoc', '1', { expires: 30, path: '/' });
    						}
    						$('ul.toc_list').hide('fast');
    						$('#toc_container').addClass('contracted').shrinkTOCWidth();
    
    				} else { // must have class 'toc_toggle_show'
    					    $(this).removeClass('toc_toggle_show');
    						$(this).addClass('toc_toggle_hide');
    						$(this).html(tocplus.visibility_hide);
    
    						if ( $.cookie ) {
    							if ( invert )
    								$.cookie('tocplus_hidetoc', '1', { expires: 30, path: '/' });
    							else 
    								$.cookie('tocplus_hidetoc', null, { path: '/' });
    						}
    						$('#toc_container').css('width', tocplus.width).removeClass('contracted');
    						$('ul.toc_list').show('fast');
    				}
    			});
    		}
    	}
    });
    

    Save the front.js file.

    Select all and copy the entire contents of front.js into front.min.js. front.min.js is supposed to be a minified version of front.js, but we’ll just make it an identical copy, not minified.

    Save the front.min.js file.

    Test it. Restore from backup if it doesn’t work.

    Thank you !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Doesn’t work with TranslatePress’ is closed to new replies.