$result * @param $default_language * @param $current_language * @param $options * @return */ function bpml_google_translate_activity_filter($result, $default_language, $current_language, $type_options) { $to_translate_title = FALSE; $to_translate_content = FALSE; if ((!empty($result->lang_recorded))) { if ($result->lang_recorded != $current_language && $type_options['translate_title']) { $to_translate_title = TRUE; } } else if ($result->lang != $current_language && $type_options['translate_title']) { $to_translate_title = TRUE; } if ($result->lang != $current_language && !empty($result->content) && $type_options['translate_content']) { $to_translate_content = TRUE; } // Apply display filters $result->action = apply_filters('bp_get_activity_action', $result->action); if (!empty($result->content)) { $result->content = apply_filters('bp_get_activity_content_body', $result->content); } global $bpml; // Stored in DB if ($bpml['activities']['enable_google_translation'] === 'store') { // Store original values $original_translation_title = $result->action; $original_translation_content = $result->content; // Check if cached $translation = bp_activity_get_meta($result->id, 'bpml_google_translation'); // Translate title if ($to_translate_title && $type_options['translate_title_cache']) { if (empty($translation) || !isset($translation[$current_language]) || !isset($translation[$current_language]['title'])) { bpml_debug('

Fetched Google translation for activity title ID:' . $result->id . ' (original lang \'' . $result->lang . '\', current lang \'' . $current_language . '\')

'); // Translate with Google if (!empty($result->lang_recorded)) { $translation_title = bpml_google_translate($result->action, $result->lang_recorded, $current_language); } else { $translation_title = bpml_google_translate($result->action, $result->lang, $current_language); } $translation[$current_language]['title'] = $translation_title; // Set cache bp_activity_update_meta($result->id, 'bpml_google_translation', $translation); } else { bpml_debug('

Cached Google translation for activity title ID:' . $result->id . ' (original lang \'' . $result->lang . '\', current lang \'' . $current_language . '\')

'); $translation_title = $translation[$current_language]['title']; } } else if ($to_translate_title) { if (!empty($result->lang_recorded)) { $translation_title = bpml_google_translate($result->action, $result->lang_recorded, $current_language); } else { $translation_title = bpml_google_translate($result->action, $result->lang, $current_language); } } // Translate content if ($to_translate_content && $type_options['translate_content_cache']) { if (empty($translation) || !isset($translation[$current_language]) || !isset($translation[$current_language]['content'])) { bpml_debug('

Fetched Google translation for activity content ID:' . $result->id . ' (original lang \'' . $result->lang . '\', current lang \'' . $current_language . '\')

'); // Translate with Google $translation_content = bpml_google_translate($result->content, $result->lang, $current_language); $translation[$current_language]['content'] = $translation_content; // Set cache bp_activity_update_meta($result->id, 'bpml_google_translation', $translation); } else { bpml_debug('

Cached Google translation for activity content ID:' . $result->id . ' (original lang \'' . $result->lang . '\', current lang \'' . $current_language . '\')

'); $translation_content = $translation[$current_language]['content']; } } else if ($to_translate_content) { $translation_content = bpml_google_translate($result->content, $result->lang, $current_language); } // Wrap contents if ($to_translate_title) { $result->action = '
' . $original_translation_title . '
' . $translation_title; } if ($to_translate_content) { $result->content = '
' . $original_translation_content . '
' . $translation_content; } } else if ($bpml['activities']['enable_google_translation'] === 'js') { // Translate with JS (wrap contents) if ($to_translate_title) { if (!empty($result->lang_recorded)) { $result->action = bpml_google_translate_wrap($result->action, $result->lang_recorded); } else { $result->action = bpml_google_translate_wrap($result->action, $result->lang); } } if ($to_translate_content) { $result->content = bpml_google_translate_wrap($result->content, $result->lang); } } return $result; } /** * Translates strings using Google Translate. * * @staticvar string $client * @param $item * @param $current_language * @return */ function bpml_google_translate($content, $from_language, $to_language) { static $client = NULL; if (is_null($client)) { $client = new WP_Http(); } $gtranslateurl = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s|%s'; $url = sprintf($gtranslateurl, urlencode($content), $from_language, $to_language); $url = str_replace('|', '%7C', $url); $response = $client->request($url); if (!is_wp_error($response) && $response['response']['code'] == '200' && $response['response']['code'] == '403') { $translation = json_decode($response['body']); $content = $translation->responseData->translatedText; } else { $content = null; } return $content; } /** * Renders Google translate control button. * * Only rendered once per page. * * @global $bpml_google_translate_js * @staticvar boolean $called * @param string $output * @return string */ function bpml_google_translate_button($output = '') { global $bpml_google_translate_js, $bpml_in_activity_loop; // if (empty($bpml_in_activity_loop)) { // return ''; // } static $called = FALSE; if ($called) { return ''; } $called = TRUE; if (!empty($bpml_google_translate_js)) { $output .= '
' . bpml_google_translate_js(); } return $output; } /** * Wraps content in divs found by Google translate. * * @global boolean $bpml_google_translate_js * Sets flag that translate button is needed * @param $content * @param $lang * @param $element * @return */ function bpml_google_translate_wrap($content, $lang, $element = 'div') { global $bpml_google_translate_js, $bpml_google_translate_ajax; $suffix = !empty($bpml_google_translate_ajax) ? '-ajax' : ''; $bpml_google_translate_js = TRUE; return '
' . $content . '
'; } /** * Returns JS needed for Google translate. * * @param string $lang * @return */ function bpml_google_translate_js($lang = NULL) { if (is_null($lang)) { $lang = ICL_LANGUAGE_CODE; } global $bpml_google_translate_ajax; $suffix = !empty($bpml_google_translate_ajax) ? '-ajax' : ''; return ' '; }