• Resolved Carlos

    (@lbakyl)


    Hi,

    I read the articles on how to exclude strings from being translated automatically, but it is not clear to me how to use it.

    On my site, I have quizzes under https://www.englishteachers.cz/quizzes/* (such as this quiz) and I would like all sub-pages under */quizzes/* to be excluded from automatic translation (manual translation is possible or could be disabled altogether). Is there a way of achieving it?

    I managed to do that with Weglot but was considering buying Translate Press instead if the functionality is there.

    Thank you for your help.

    Regards,

    Jan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Carlos

    (@lbakyl)

    Apparently, this is possible by creating a mini-plugin (a function that will only run the plugin on the page if it is not in the listo f excluded URL/slugs:

    – You can achieve this with custom code:

    1) Create an empty plugin like this: https://gist.github.com/sareiodata/76f701e01db6685829db

    2) Add (and edit the arrays with your desired pages slug that you want to be excluded) the following code to the end of it:

    add_action( ‘plugins_loaded’, ‘trpc_exclude_tp_from_pages’, -1 );
    function trpc_exclude_tp_from_pages(){
    $do_not_translate_path = array(‘contact’);
    $do_not_translate_path = array(‘home’);
    $do_not_translate_path = array(‘test-page-page’);

    foreach ( $do_not_translate_path as $path ){
    if ( !empty($path) && strpos($_SERVER[‘REQUEST_URI’], untrailingslashit($path)) !== false ){
    add_filter( ‘trp_allow_tp_to_run’, function(){return false; } );
    }
    }
    }

    3) Install this plugin via FTP (copy it inside wp-content/plugins) or create a zip archive with it and install it via the WordPress plugin upload functionality.

    Thank you for your kind answer, Alexandru from TranslatePress!
    Marking as resolved.

    Thread Starter Carlos

    (@lbakyl)

    Adding the entire code with the mini-plugin

    
    <?php
    /**
     * Plugin Name: Name Of The Plugin
     * Plugin URI: https://URI_Of_Page_Describing_Plugin_and_Updates
     * Description: A brief description of the Plugin.
     * Version: The Plugin's Version Number, e.g.: 1.0
     * Author: Name Of The Plugin Author
     * Author URI: https://URI_Of_The_Plugin_Author
     * License: A "Slug" license name e.g. GPL2
     */
     
    /*  Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL)
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License, version 2, as 
        published by the Free Software Foundation.
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    // Start writing code after this line!
    
    add_action( 'plugins_loaded', 'trpc_exclude_tp_from_pages', -1 );
    function trpc_exclude_tp_from_pages(){
        $do_not_translate_path = array('PART-OF-URL');
    	
        foreach ( $do_not_translate_path as $path ){
            if ( !empty($path) && strpos($_SERVER['REQUEST_URI'], untrailingslashit($path)) !== false ){
                add_filter( 'trp_allow_tp_to_run', function(){return false; } );
            }
        }
    }
    
    Thread Starter Carlos

    (@lbakyl)

    Just for completion, that piece of code was not actually working for me, so I modified it with a hard-coded rule that if the pattern in the URL is detected with the given language code, then strip the URL of the language code (e.g. yourdomain.com/ro/my-page is stripped to yourdomain.com/my-page).

    However, this means that the user is shifted back to the original language and when they move onto another page that does provide translation, they need to use the language switcher again to change the language to the secondary language.

    In this code to make it work, make sure to modify row no.36 with the secondary language. If you have more than one secondary languages, then it would be more complicated it won’t work.

    <?php
    /**
     * Plugin Name: TranslatePress - Exclude URLs From Translation
     * Plugin URI: none
     * Description: A mini plugin for TranslatePress to exclude URL paths from translation
     * Version: 1.0
     * Author: Carlos De Nirro
     * Author URI: none
     * License: GPL3
     */
     
    /*  Copyright Janos Bakalaros (email : [email protected])
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License, version 2, as 
        published by the Free Software Foundation.
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    // Start writing code after this line!
    
    add_action( 'plugins_loaded', 'trpc_exclude_tp_from_pages', -1 );
    
    function trpc_exclude_tp_from_pages(){
        $do_not_translate_path = array('quizzes');
    	
        foreach ( $do_not_translate_path as $path ){
            if ( !empty($path) && strpos($_SERVER['REQUEST_URI'], untrailingslashit($path)) !== false ){
                add_filter( 'trp_allow_tp_to_run', function(){return false; } );
    
    			$link = str_replace("cs/", "", $_SERVER['REQUEST_URI']);
    			
    			$_SERVER['REQUEST_URI'] = $link;
    			 
    		}
    	}
    }
    
    
    • This reply was modified 4 years, 6 months ago by Carlos.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude all URLs in a path from a translation’ is closed to new replies.