Changeset 3108806
- Timestamp:
- 06/27/2024 02:55:38 PM (21 months ago)
- Location:
- multilingual-contact-form-7-with-polylang/trunk
- Files:
-
- 1 added
- 1 deleted
- 4 edited
-
admin/Editor_Panels.php (deleted)
-
admin/MessagesTranslationManager.php (added)
-
frontend/Messages_Translation.php (modified) (2 diffs)
-
inc/Helpers.php (modified) (1 diff)
-
plugin.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
multilingual-contact-form-7-with-polylang/trunk/frontend/Messages_Translation.php
r2859864 r3108806 15 15 * 16 16 */ 17 17 18 namespace mlcf7pll\frontend; 18 19 … … 20 21 21 22 new Messages_Translation(); 22 class Messages_Translation23 {24 23 25 public function __construct() 26 {24 class Messages_Translation { 25 public function __construct() { 27 26 28 if (is_admin()) 29 return; 27 if ( is_admin() ) { 28 return; 29 } 30 30 31 add_filter('load_textdomain_mofile', [$this, 'load_textdomain_mofile']);31 add_filter( 'load_textdomain_mofile', [ $this, 'load_textdomain_mofile' ] ); 32 32 33 add_filter('wpcf7_display_message', [$this, 'translate_cf7_messages'], 90, 2);33 add_filter( 'wpcf7_display_message', [ $this, 'translate_cf7_messages' ], 90, 2 ); 34 34 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 } 39 39 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 ) { 49 50 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']; 55 80 56 81 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 } 57 88 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 } 65 91 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 ) { 70 98 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(); 73 102 74 return str_replace( "{$user_locale}.mo", "{$pll_locale}.mo", $mofile ); 75 } 103 $translated = __( $message, 'contact-form-7' ); 76 104 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 } 94 107 } 95 108 -
multilingual-contact-form-7-with-polylang/trunk/inc/Helpers.php
r3076827 r3108806 115 115 116 116 $messages = wpcf7_messages(); 117 foreach($messages as $key => $val){ 118 $messages[$key] = $val['default']; 119 } 117 120 118 121 // remove the filter to make translating work again -
multilingual-contact-form-7-with-polylang/trunk/plugin.php
r3076827 r3108806 4 4 * Plugin URI: 5 5 * Description: Enables translation and use of the same forms in different languages of Contact Form 7 forms with Polylang 6 * Version: 1.0.1 06 * Version: 1.0.11 7 7 * Author: Andreas Münch 8 8 * Author URI: https://andreasmuench.de … … 93 93 require_once('admin/String_Registration.php'); 94 94 require_once('admin/Settings_Page.php'); 95 require_once( 'admin/MessagesTranslationManager.php' ); 95 96 require_once('frontend/Submission.php'); 96 97 require_once('frontend/String_Translation.php'); -
multilingual-contact-form-7-with-polylang/trunk/readme.txt
r3076827 r3108806 4 4 Tags: contact form 7, polylang, multilingual, translate, language 5 5 Requires at least: 5.7.0 6 Tested up to: 6. 4.36 Tested up to: 6.5.5 7 7 Requires PHP: 5.6 8 Stable tag: 1.0.1 08 Stable tag: 1.0.11 9 9 License: GPLv2 or later 10 10 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 69 69 == Changelog == 70 70 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 71 76 = 1.0.10 = 72 77 * 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.