Translate WordPress with GTranslate - Version 2.8.41

Version Description

  • Added support for WP REST API JSON translation for posts and categories
  • Deeper integration with WooCommerce
  • Integration with WooCommerce Multi-Step Checkout plugin
  • Integration with Agile Store Locator plugin
  • Auto switch language based on browser language settigns for paid version added
Download this release

Release Info

Developer edo888
Plugin Icon 128x128 Translate WordPress with GTranslate
Version 2.8.41
Comparing to
See all releases

Code changes from version 2.8.40 to 2.8.41

Files changed (2) hide show
  1. gtranslate.php +221 -17
  2. readme.txt +8 -1
gtranslate.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: GTranslate
4
  Plugin URI: https://gtranslate.io/?xyz=998
5
  Description: Makes your website <strong>multilingual</strong> and available to the world using Google Translate. For support visit <a href="https://wordpress.org/support/plugin/gtranslate">GTranslate Support</a>.
6
- Version: 2.8.40
7
  Author: Translate AI Multilingual Solutions
8
  Author URI: https://gtranslate.io
9
  Text Domain: gtranslate
@@ -321,12 +321,12 @@ function RefreshDoWidgetCode() {
321
  jQuery('#new_window_option').show();
322
  jQuery('#url_translation_option').show();
323
  jQuery('#hreflang_tags_option').show();
324
- jQuery('#auto_switch_option').hide();
325
  } else {
326
  jQuery('#new_window_option').hide();
327
  jQuery('#url_translation_option').hide();
328
  jQuery('#hreflang_tags_option').hide();
329
- jQuery('#auto_switch_option').show();
330
  }
331
 
332
  if(widget_look == 'dropdown' || widget_look == 'flags_dropdown' || widget_look == 'globe' || widget_look == 'lang_names' || widget_look == 'lang_codes') {
@@ -797,7 +797,7 @@ if(jQuery('#pro_version:checked').length || jQuery('#enterprise_version:checked'
797
  jQuery('#new_window_option').show();
798
  jQuery('#url_translation_option').show();
799
  jQuery('#hreflang_tags_option').show();
800
- jQuery('#auto_switch_option').hide();
801
  }
802
 
803
  if('$widget_look' == 'dropdown' || '$widget_look' == 'flags_dropdown' || '$widget_look' == 'globe' || '$widget_look' == 'lang_names' || '$widget_look' == 'lang_codes') {
@@ -1290,21 +1290,8 @@ EOT;
1290
  </style>
1291
 
1292
  <script>window.intercomSettings = {app_id: "r70azrgx", 'platform': 'wordpress', 'translate_from': '<?php echo $default_language; ?>', 'is_sub_directory': <?php echo (empty($pro_version) ? '0' : '1'); ?>, 'is_sub_domain': <?php echo (empty($enterprise_version) ? '0' : '1'); ?>};(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/r70azrgx';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>
1293
- <script>
1294
- var intercom_timeout = setInterval(function() {
1295
- var intercom_box = jQuery('#intercom-container .intercom-launcher-frame');
1296
- jQuery({bottom: 500}).animate({bottom: 20}, {duration: 1000, easing: 'easeOutBounce', step: function() {intercom_box.css('cssText', 'bottom: ' + this.bottom + 'px !important;') }});
1297
- }, 15000);
1298
- Intercom('onShow', function() {clearInterval(intercom_timeout);});
1299
- </script>
1300
 
1301
  <?php
1302
- /*
1303
- <!-- Live Chat for GTranslate -->
1304
- <script>var _glc =_glc || [];_glc.push('all_ag9zfmNsaWNrZGVza2NoYXRyDgsSBXVzZXJzGM3hxwQM');var glcpath = (('https:' == document.location.protocol) ? 'https://my.clickdesk.com/clickdesk-ui/browser/' : 'http://my.clickdesk.com/clickdesk-ui/browser/');var glcp = (('https:' == document.location.protocol) ? 'https://' : 'http://');var glcspt = document.createElement('script'); glcspt.type = 'text/javascript'; glcspt.async = true; glcspt.src = glcpath + 'livechat-new.js';var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(glcspt, s);</script>
1305
- */
1306
- ?>
1307
- <?php
1308
  }
1309
 
1310
  public static function control_options() {
@@ -1978,3 +1965,220 @@ if($data['add_hreflang_tags'] and ($data['pro_version'] or $data['enterprise_ver
1978
  }
1979
  }
1980
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  Plugin Name: GTranslate
4
  Plugin URI: https://gtranslate.io/?xyz=998
5
  Description: Makes your website <strong>multilingual</strong> and available to the world using Google Translate. For support visit <a href="https://wordpress.org/support/plugin/gtranslate">GTranslate Support</a>.
6
+ Version: 2.8.41
7
  Author: Translate AI Multilingual Solutions
8
  Author URI: https://gtranslate.io
9
  Text Domain: gtranslate
321
  jQuery('#new_window_option').show();
322
  jQuery('#url_translation_option').show();
323
  jQuery('#hreflang_tags_option').show();
324
+ //jQuery('#auto_switch_option').hide();
325
  } else {
326
  jQuery('#new_window_option').hide();
327
  jQuery('#url_translation_option').hide();
328
  jQuery('#hreflang_tags_option').hide();
329
+ //jQuery('#auto_switch_option').show();
330
  }
331
 
332
  if(widget_look == 'dropdown' || widget_look == 'flags_dropdown' || widget_look == 'globe' || widget_look == 'lang_names' || widget_look == 'lang_codes') {
797
  jQuery('#new_window_option').show();
798
  jQuery('#url_translation_option').show();
799
  jQuery('#hreflang_tags_option').show();
800
+ //jQuery('#auto_switch_option').hide();
801
  }
802
 
803
  if('$widget_look' == 'dropdown' || '$widget_look' == 'flags_dropdown' || '$widget_look' == 'globe' || '$widget_look' == 'lang_names' || '$widget_look' == 'lang_codes') {
1290
  </style>
1291
 
1292
  <script>window.intercomSettings = {app_id: "r70azrgx", 'platform': 'wordpress', 'translate_from': '<?php echo $default_language; ?>', 'is_sub_directory': <?php echo (empty($pro_version) ? '0' : '1'); ?>, 'is_sub_domain': <?php echo (empty($enterprise_version) ? '0' : '1'); ?>};(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/r70azrgx';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>
 
 
 
 
 
 
 
1293
 
1294
  <?php
 
 
 
 
 
 
1295
  }
1296
 
1297
  public static function control_options() {
1965
  }
1966
  }
1967
  }
1968
+
1969
+ // translate WP REST API posts and categories data in JSON response
1970
+ if($data['pro_version'] or $data['enterprise_version']) {
1971
+ function gtranslate_rest_post($response, $post, $request) {
1972
+ if(isset($response->data['content']) and is_array($response->data['content']))
1973
+ $response->data['content']['gt_translate_keys'] = array(array('key' => 'rendered', 'format' => 'html'));
1974
+
1975
+ if(isset($response->data['excerpt']) and is_array($response->data['excerpt']))
1976
+ $response->data['excerpt']['gt_translate_keys'] = array(array('key' => 'rendered', 'format' => 'html'));
1977
+
1978
+ if(isset($response->data['title']) and is_array($response->data['title']))
1979
+ $response->data['title']['gt_translate_keys'] = array(array('key' => 'rendered', 'format' => 'text'));
1980
+
1981
+ if(isset($response->data['link']))
1982
+ $response->data['gt_translate_keys'] = array(array('key' => 'link', 'format' => 'url'));
1983
+
1984
+ // more fields can be added here
1985
+
1986
+ return $response;
1987
+ }
1988
+
1989
+ function gtranslate_rest_category($response, $category, $request) {
1990
+ if(isset($response->data['description']))
1991
+ $response->data['gt_translate_keys'][] = array('key' => 'description', 'format' => 'html');
1992
+
1993
+ if(isset($response->data['name']))
1994
+ $response->data['gt_translate_keys'][] = array('key' => 'name', 'format' => 'text');
1995
+
1996
+ if(isset($response->data['link']))
1997
+ $response->data['gt_translate_keys'][] = array('key' => 'link', 'format' => 'url');
1998
+
1999
+ // more fields can be added here
2000
+
2001
+ return $response;
2002
+ }
2003
+
2004
+ add_filter('rest_prepare_post', 'gtranslate_rest_post', 10, 3);
2005
+ add_filter('rest_prepare_category', 'gtranslate_rest_category', 10, 3);
2006
+ }
2007
+
2008
+ // auto redirect to browser language
2009
+ if(($data['pro_version'] or $data['enterprise_version']) and $data['detect_browser_language'] and parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == parse_url(site_url(), PHP_URL_PATH) . '/' and isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) and isset($_SERVER['HTTP_USER_AGENT']) and !isset($_SERVER['HTTP_X_GT_LANG']) and preg_match('/bot|spider|slurp|facebook/i', $_SERVER['HTTP_USER_AGENT']) == 0) {
2010
+ if($data['widget_look'] == 'flags' or $data['widget_look'] == 'dropdown_with_flags' or $data['widget_look'] == 'flags_name' or $data['widget_look'] == 'flags_code' or $data['widget_look'] == 'popup')
2011
+ $allowed_languages = $data['fincl_langs'];
2012
+ elseif($data['widget_look'] == 'flags_dropdown')
2013
+ $allowed_languages = array_values(array_unique(array_merge($data['fincl_langs'], $data['incl_langs'])));
2014
+ else
2015
+ $allowed_languages = $data['incl_langs'];
2016
+
2017
+ $accept_language = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
2018
+
2019
+ // for debug purposes only
2020
+ if(isset($_GET['gt_auto_switch_to']))
2021
+ $accept_language = $_GET['gt_auto_switch_to'];
2022
+
2023
+ if($accept_language == 'zh')
2024
+ $accept_language == 'zh-CN';
2025
+
2026
+ if($accept_language != $data['default_language'] and in_array($accept_language, $allowed_languages) and !isset($_COOKIE['gt_auto_switch'])) {
2027
+ // set cookie for 30 days and redirect
2028
+ setcookie('gt_auto_switch', 1, time() + 2592000);
2029
+
2030
+ if($data['pro_version'])
2031
+ header('Location: ' . home_url() . '/' . $accept_language . '/');
2032
+ if($data['enterprise_version'] and isset($_SERVER['HTTP_HOST']))
2033
+ header('Location: ' . str_replace('://'.$_SERVER['HTTP_HOST'], '://'.$accept_language.'.'.$_SERVER['HTTP_HOST'], site_url()));
2034
+
2035
+ // todo: special redirect for language hosting
2036
+
2037
+ exit;
2038
+ }
2039
+
2040
+ }
2041
+
2042
+ // convert wp_localize_script format into JSON + JS parser
2043
+ if($data['pro_version'] or $data['enterprise_version']) {
2044
+ function gtranslate_filter_l10n_scripts() {
2045
+ global $wp_scripts;
2046
+
2047
+ $translate_handles = array(
2048
+ 'agile-store-locator-script',
2049
+ 'wmc-wizard',
2050
+ 'wc-address-i18n',
2051
+ 'wc-checkout',
2052
+ 'wc-country-select',
2053
+ 'wc-add-to-cart',
2054
+ 'wc-password-strength-meter',
2055
+
2056
+ );
2057
+
2058
+ //echo '<!--' . print_r($wp_scripts, true). '-->';
2059
+ //return;
2060
+
2061
+ foreach($wp_scripts->registered as $handle => $script) {
2062
+ if(isset($script->extra['data']) and in_array($handle, $translate_handles)) {
2063
+ $l10n = $script->extra['data'];
2064
+ preg_match_all('/var (.+) = ({(.*)});/', $l10n, $matches);
2065
+ //echo '<!--' . print_r($matches, true). '-->';
2066
+
2067
+ if(isset($matches[1]) and isset($matches[2])) {
2068
+ $vars = $matches[1];
2069
+ $scripts = $matches[2];
2070
+ } else
2071
+ continue;
2072
+
2073
+ foreach($vars as $i => $var_name) {
2074
+ $attribute_ids = $wp_scripts->get_data($handle, 'attribute-ids');
2075
+ $attribute_ids[] = $var_name . '-gt-l10n-'.$i;
2076
+ $jsons = $wp_scripts->get_data($handle, 'jsons');
2077
+ $jsons[] = $scripts[$i];
2078
+ $jss = $wp_scripts->get_data($handle, 'jss');
2079
+ $jss[] = "var $var_name = JSON.parse(document.getElementById('$var_name-gt-l10n-$i').innerHTML);";
2080
+
2081
+ $wp_scripts->add_data($handle, 'attribute-ids', $attribute_ids);
2082
+ $wp_scripts->add_data($handle, 'jsons', $jsons);
2083
+ $wp_scripts->add_data($handle, 'jss', $jss);
2084
+ }
2085
+
2086
+ unset($wp_scripts->registered[$handle]->extra['data']);
2087
+ }
2088
+ }
2089
+
2090
+ //echo '<!--' . print_r($wp_scripts, true). '-->';
2091
+
2092
+ }
2093
+
2094
+ function gtranslate_add_script_attributes($tag, $handle) {
2095
+ global $wp_scripts;
2096
+
2097
+ gtranslate_filter_l10n_scripts();
2098
+
2099
+ if(isset($wp_scripts->registered[$handle]->extra['attribute-ids'])) {
2100
+ $attribute_ids = $wp_scripts->get_data($handle, 'attribute-ids');
2101
+ $jsons = $wp_scripts->get_data($handle, 'jsons');
2102
+ $jss = $wp_scripts->get_data($handle, 'jss');
2103
+
2104
+ $return = '';
2105
+ foreach($attribute_ids as $i => $attribute_id) {
2106
+ $json = $jsons[$i];
2107
+ $js = $jss[$i];
2108
+
2109
+ $return .= "<script id='$attribute_id' type='application/json'>$json</script>\n<script type='text/javascript'>$js</script>\n";
2110
+ }
2111
+
2112
+ return $return . $tag;
2113
+ }
2114
+
2115
+ return $tag;
2116
+ }
2117
+
2118
+ // filter for woocommerce script params
2119
+ function gt_filter_woocommerce_scripts_data($data, $handle) {
2120
+ switch($handle) {
2121
+ case 'wc-address-i18n': {
2122
+ $data['gt_translate_keys'] = array(
2123
+ array('key' => 'locale', 'format' => 'json'),
2124
+ 'i18n_required_text',
2125
+ 'i18n_optional_text',
2126
+ );
2127
+
2128
+ $locale = json_decode($data['locale']);
2129
+
2130
+ if(isset($locale->default->shipping->address_1))
2131
+ $locale->default->shipping->address_1->gt_translate_keys = array('label', 'placeholder');
2132
+ if(isset($locale->default->shipping->address_2))
2133
+ $locale->default->shipping->address_2->gt_translate_keys = array('label', 'placeholder');
2134
+ if(isset($locale->default->shipping->city))
2135
+ $locale->default->shipping->city->gt_translate_keys = array('label', 'placeholder');
2136
+ if(isset($locale->default->shipping->postcode))
2137
+ $locale->default->shipping->postcode->gt_translate_keys = array('label', 'placeholder');
2138
+ if(isset($locale->default->shipping->state))
2139
+ $locale->default->shipping->state->gt_translate_keys = array('label', 'placeholder');
2140
+
2141
+ if(isset($locale->default->billing->address_1))
2142
+ $locale->default->billing->address_1->gt_translate_keys = array('label', 'placeholder');
2143
+ if(isset($locale->default->billing->address_2))
2144
+ $locale->default->billing->address_2->gt_translate_keys = array('label', 'placeholder');
2145
+ if(isset($locale->default->billing->city))
2146
+ $locale->default->billing->city->gt_translate_keys = array('label', 'placeholder');
2147
+ if(isset($locale->default->billing->postcode))
2148
+ $locale->default->billing->postcode->gt_translate_keys = array('label', 'placeholder');
2149
+ if(isset($locale->default->billing->state))
2150
+ $locale->default->billing->state->gt_translate_keys = array('label', 'placeholder');
2151
+
2152
+ $data['locale'] = json_encode($locale);
2153
+ } break;
2154
+
2155
+ case 'wc-checkout': {
2156
+ $data['gt_translate_keys'] = array('i18n_checkout_error');
2157
+ } break;
2158
+
2159
+ case 'wc-country-select': {
2160
+ $data['gt_translate_keys'] = array('i18n_ajax_error', 'i18n_input_too_long_1', 'i18n_input_too_long_n', 'i18n_input_too_short_1', 'i18n_input_too_short_n', 'i18n_load_more', 'i18n_no_matches', 'i18n_searching', 'i18n_select_state_text', 'i18n_selection_too_long_1', 'i18n_selection_too_long_n');
2161
+ } break;
2162
+
2163
+ case 'wc-add-to-cart': {
2164
+ $data['gt_translate_keys'] = array('i18n_view_cart', array('key' => 'cart_url', 'format' => 'url'));
2165
+ } break;
2166
+
2167
+ case 'wc-password-strength-meter': {
2168
+ $data['gt_translate_keys'] = array('i18n_password_error', 'i18n_password_hint', '');
2169
+ } break;
2170
+
2171
+ default: break;
2172
+ }
2173
+
2174
+ return $data;
2175
+ }
2176
+
2177
+ //add_action('wp_print_scripts', 'gtranslate_filter_l10n_scripts', 1);
2178
+ //add_action('wp_print_header_scripts', 'gtranslate_filter_l10n_scripts', 1);
2179
+ //add_action('wp_print_footer_scripts', 'gtranslate_filter_l10n_scripts', 1);
2180
+
2181
+ add_filter('script_loader_tag', 'gtranslate_add_script_attributes', 100, 2);
2182
+
2183
+ add_filter('woocommerce_get_script_data', 'gt_filter_woocommerce_scripts_data', 10, 2 );
2184
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Author: Translate AI Multilingual Solutions
4
  Tags: translate wordpress, multilingual, translate, translation, language, bilingual, localization, translation proxy, i18n, l10n, localisation, multilanguage, google translate
5
  Requires at least: 2.8.1
6
  Tested up to: 4.9
7
- Stable tag: 2.8.40
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
  Donate link: https://gtranslate.io/?xyz=998#pricing
@@ -220,6 +220,13 @@ You need to go to the language you want to edit, for instance, French: http://do
220
 
221
  == Changelog ==
222
 
 
 
 
 
 
 
 
223
  = 2.8.40 =
224
  * Updated Hebrew language code for hreflang (he instead of iw)
225
  * Added translation for ajax_url javascript variables, previously only wp_ajax_url were translated
4
  Tags: translate wordpress, multilingual, translate, translation, language, bilingual, localization, translation proxy, i18n, l10n, localisation, multilanguage, google translate
5
  Requires at least: 2.8.1
6
  Tested up to: 4.9
7
+ Stable tag: 2.8.41
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
  Donate link: https://gtranslate.io/?xyz=998#pricing
220
 
221
  == Changelog ==
222
 
223
+ = 2.8.41 =
224
+ * Added support for WP REST API JSON translation for posts and categories
225
+ * Deeper integration with WooCommerce
226
+ * Integration with WooCommerce Multi-Step Checkout plugin
227
+ * Integration with Agile Store Locator plugin
228
+ * Auto switch language based on browser language settigns for paid version added
229
+
230
  = 2.8.40 =
231
  * Updated Hebrew language code for hreflang (he instead of iw)
232
  * Added translation for ajax_url javascript variables, previously only wp_ajax_url were translated