Plugin Directory

Changeset 3108806


Ignore:
Timestamp:
06/27/2024 02:55:38 PM (21 months ago)
Author:
andreasmuench
Message:

v1.0.11

Location:
multilingual-contact-form-7-with-polylang/trunk
Files:
1 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • multilingual-contact-form-7-with-polylang/trunk/frontend/Messages_Translation.php

    r2859864 r3108806  
    1515 *
    1616 */
     17
    1718namespace mlcf7pll\frontend;
    1819
     
    2021
    2122new Messages_Translation();
    22 class Messages_Translation
    23 {
    2423
    25     public function __construct()
    26     {
     24class Messages_Translation {
     25    public function __construct() {
    2726
    28         if (is_admin())
    29             return;
     27        if ( is_admin() ) {
     28            return;
     29        }
    3030
    31         add_filter('load_textdomain_mofile', [$this, 'load_textdomain_mofile']);
     31        add_filter( 'load_textdomain_mofile', [ $this, 'load_textdomain_mofile' ] );
    3232
    33         add_filter('wpcf7_display_message', [$this, 'translate_cf7_messages'], 90, 2);
     33        add_filter( 'wpcf7_display_message', [ $this, 'translate_cf7_messages' ], 90, 2 );
    3434
    35         if (get_option('mlcf7pll_fix_ajax_form_messages', 'on')){
    36             add_filter('locale', [$this, 'maybe_save_locale_to_cookie'], 9999);
    37         }
    38     }
     35        if ( get_option( 'mlcf7pll_fix_ajax_form_messages', 'on' ) ) {
     36            add_filter( 'locale', [ $this, 'maybe_save_locale_to_cookie' ], 9999 );
     37        }
     38    }
    3939
    40     /**
    41      * As we need the pll_language cookie we must make sure it is set sooner than PLL would do it.
    42      * This fixes a bug that on first visit of a translated page (not primary language)
    43      * the cookie is not set and the messages will not be translated correctly
    44      *
    45      * @param $locale
    46      * @return mixed
    47      */
    48     function maybe_save_locale_to_cookie($locale){
     40    /**
     41     * As we need the pll_language cookie we must make sure it is set sooner than PLL would do it.
     42     * This fixes a bug that on first visit of a translated page (not primary language)
     43     * the cookie is not set and the messages will not be translated correctly
     44     *
     45     * @param $locale
     46     *
     47     * @return mixed
     48     */
     49    function maybe_save_locale_to_cookie( $locale ) {
    4950
    50         if(empty($_COOKIE['pll_language'])){
    51             $_COOKIE['pll_language'] = $locale;
    52         }
    53         return $locale;
    54     }
     51        if ( empty( $_COOKIE['pll_language'] ) ) {
     52            $_COOKIE['pll_language'] = $locale;
     53        }
     54
     55        return $locale;
     56    }
     57
     58    /**
     59     * Fix translation of cf7 messages
     60     * Force loading the translation language version that is defined in the post data or the polylang cookie
     61     * This is necessary as cf7 is not supporting translation of messages in different languages for multilanguage
     62     * websites, but only translates them once and saves the translations statically
     63     */
     64    function load_textdomain_mofile( $mofile ) {
     65
     66        if (
     67            $_SERVER['REQUEST_METHOD'] !== 'POST'
     68            || ( empty( $_POST['_wpcf7_locale'] ) && empty( $_COOKIE['pll_language'] ) )
     69            || empty( $mofile )
     70            || strpos( $mofile, 'contact-form-7' ) === false
     71            || ! Helpers::is_rest()
     72        ) {
     73            return $mofile;
     74        }
     75
     76        $user_locale = get_user_locale();
     77
     78        if ( ! empty( $_POST['_wpcf7_locale'] ) ) {
     79            $locale = $_POST['_wpcf7_locale'];
    5580
    5681
     82        } else
     83            // not sure if the Cookie part is still necessary,
     84            // it seems that the _wpcf7_locale is always sent anyway
     85            if ( ! empty( $_COOKIE['pll_language'] ) ) {
     86            $locale = Helpers::pll_get_locale_by_slug( $_COOKIE['pll_language'] );
     87        }
    5788
    58     /**
    59      * Fix translation of cf7 messages
    60      * Force loading the translation language version that is defined in polylang cookie
    61      * This is necessary as cf7 is not supporting translation of messages in different languages for multilanguage websites,
    62      * but only translates them once and saves the translations statically
    63      */
    64     function load_textdomain_mofile( $mofile ) {
     89        return str_replace( "{$user_locale}.mo", "{$locale}.mo", $mofile );
     90    }
    6591
    66         // if the cookie is not set, we cannot retrieve the current language, also do this only for cf7 mofiles and only in the REST requests (AJAX)
    67         if(empty($_COOKIE['pll_language']) || empty($mofile) || strpos($mofile, 'contact-form-7') === false || !Helpers::is_rest()){
    68             return $mofile;
    69         }
     92    /**
     93     * translate the cf7 messages
     94     * only works with the standard messages that are also defined in the translation files
     95     * if these are changed in the form settings, the translation does not work
     96     */
     97    function translate_cf7_messages( $message, $status ) {
    7098
    71         $user_locale = get_user_locale();
    72         $pll_locale = Helpers::pll_get_locale_by_slug($_COOKIE['pll_language']);
     99        // make sure the cf7 textdomain is loaded
     100        // this may not be the case if the base language of the site is US EN
     101        Helpers::maybe_load_cf7_textdomain();
    73102
    74         return str_replace( "{$user_locale}.mo", "{$pll_locale}.mo", $mofile );
    75     }
     103        $translated = __( $message, 'contact-form-7' );
    76104
    77 
    78     /**
    79      * translate the cf7 messages
    80      * only works with the standard messages that are also defined in the translation files
    81      * if these are changed in the form settings, the translation does not work
    82      */
    83     function translate_cf7_messages($message, $status){
    84 
    85         // make sure the cf7 textdomain is loaded
    86         // this may not be the case if the base language of the site is US EN
    87         Helpers::maybe_load_cf7_textdomain();
    88 
    89         $translated = __($message, 'contact-form-7');
    90         return $translated;
    91     }
    92 
    93 
     105        return $translated;
     106    }
    94107}
    95108
  • multilingual-contact-form-7-with-polylang/trunk/inc/Helpers.php

    r3076827 r3108806  
    115115
    116116        $messages = wpcf7_messages();
     117        foreach($messages as $key => $val){
     118            $messages[$key] = $val['default'];
     119        }
    117120
    118121        // remove the filter to make translating work again
  • multilingual-contact-form-7-with-polylang/trunk/plugin.php

    r3076827 r3108806  
    44 * Plugin URI:
    55 * Description: Enables translation and use of the same forms in different languages of Contact Form 7 forms with Polylang
    6  * Version: 1.0.10
     6 * Version: 1.0.11
    77 * Author: Andreas Münch
    88 * Author URI: https://andreasmuench.de
     
    9393        require_once('admin/String_Registration.php');
    9494        require_once('admin/Settings_Page.php');
     95        require_once( 'admin/MessagesTranslationManager.php' );
    9596        require_once('frontend/Submission.php');
    9697        require_once('frontend/String_Translation.php');
  • multilingual-contact-form-7-with-polylang/trunk/readme.txt

    r3076827 r3108806  
    44Tags: contact form 7, polylang, multilingual, translate, language
    55Requires at least: 5.7.0
    6 Tested up to: 6.4.3
     6Tested up to: 6.5.5
    77Requires PHP: 5.6
    8 Stable tag: 1.0.10
     8Stable tag: 1.0.11
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6969== Changelog ==
    7070
     71= 1.0.11 =
     72* implement detection of untranslatable custom message strings
     73* implement feature to reset message strings
     74* improve detection of current language in AJAX form submit
     75
    7176= 1.0.10 =
    7277* fix issue with mb_stripos(), see https://wordpress.org/support/topic/critical-error-with-last-update/#post-17711176
Note: See TracChangeset for help on using the changeset viewer.