/** * Convert a language string into another language string. * * @param mixed The string to convert * @param LONG_TEXT The language to convert to * @return LONG_TEXT The converted string */ function google_translate($str_in, $lang) { $tempcode = is_object($str_in); $GLOBALS['NO_QUERY_LIMIT'] = true; if (get_option('enable_google_translate', true) !== '1') { return $str_in; } if ($tempcode) { $str_in = $str_in->evaluate(); } global $DOING_TRANSLATE; if (!isset($DOING_TRANSLATE)) { $DOING_TRANSLATE = false; } if ($DOING_TRANSLATE) { return $tempcode ? protect_from_escaping($str_in) : $str_in; } // Don't want loops if ($str_in == '') { return $tempcode ? protect_from_escaping(escape_html('')) : escape_html(''); } if (strpos($str_in, 'gtranslate_cache') !== false) { return $tempcode ? protect_from_escaping($str_in) : $str_in; } // Stop loops about corrupt/missing database tables $language_list = array('ar' => 'Arabic', 'bg' => 'Bulgarian', 'zh-cn' => 'Simplified Chinese', 'zh-tw' => 'Traditional Chinese', 'hr' => 'Croatian', 'cs' => 'Czech', 'da' => 'Danish', 'nl' => 'Dutch', 'en' => 'English', 'fi' => 'Finnish', 'fr' => 'French', 'de' => 'German', 'el' => 'Greek', 'hi' => 'Hindi', 'it' => 'Italian', 'ja' => 'Japanese', 'ko' => 'Korean', 'pl' => 'Polish', 'pt' => 'Portuguese', 'ro' => 'Romanian', 'ru' => 'Russian', 'es' => 'Spanish', 'sv' => 'Swedish'); $lang = strtolower($lang); if (!array_key_exists($lang, $language_list)) { return $tempcode ? protect_from_escaping($str_in) : $str_in; } $DOING_TRANSLATE = true; require_lang('lang'); $chache = check_google_cache($str_in, $lang); if (count($chache) == 0) { require_code('GTranslate'); $translate = new GTranslate(); $num_matches = array(); $matches = array(); $rep = array(); $prepped = $str_in; $j = 0; foreach (array(array('[', ']'), array('{', '}')) as $symbol) { $_matches = array(); $_num_matches = preg_match_all('#[' . preg_quote($symbol[0]) . '][^' . preg_quote($symbol[0]) . preg_quote($symbol[1]) . ']*[' . preg_quote($symbol[1]) . ']#', $str_in, $_matches); $matches[$symbol[0]] = $_matches; $num_matches[$symbol[0]] = $_num_matches; for ($i = 0; $i < $_num_matches; $i++) { $from = $_matches[0][$i]; $to = '<span class="notranslate">' . strval($j) . '</span>'; $rep['!' . strval($j)] = $from; // The '!' bit is because we can't trust indexing in PHP arrays if it is numeric $pos = 0; do { $pos = strpos($prepped, $from, $pos); if ($pos !== false) { $pos_open = strrpos(substr($prepped, 0, $pos), '<'); $pos_close = strrpos(substr($prepped, 0, $pos), '>'); if ($pos_open === false || $pos_close !== false && $pos_close > $pos_open) { $prepped = substr($prepped, 0, $pos) . $to . substr($prepped, $pos + strlen($from)); $pos += strlen($to); } else { $pos_title = strrpos(substr($prepped, 0, $pos), 'title="'); $pos_alt = strrpos(substr($prepped, 0, $pos), 'alt="'); $pos_quote = strrpos(substr($prepped, 0, $pos), '"'); if ($pos_alt !== false && $pos_alt > $pos_open && $pos_quote == $pos_alt + 4 || $pos_title !== false && $pos_title > $pos_open && $pos_quote == $pos_title + 6) { $to2 = ' conv' . strval($j) . ' '; $prepped = substr($prepped, 0, $pos) . $to2 . substr($prepped, $pos + strlen($from)); $pos += strlen($to2); } else { $pos += strlen($from); } } } } while ($pos !== false); $j++; } } if (strpos(preg_replace('#<[^>]*>#', '', $prepped), '{') !== false) { $DOING_TRANSLATE = false; return $tempcode ? protect_from_escaping($str_in) : $str_in; // Cannot translate as it has very complex Tempcode in it } $to = $language_list[$lang]; $from_lang = strtolower(get_site_default_lang()); try { $convertedstring = $translate->Text($prepped)->From(array_key_exists($from_lang, $language_list) ? $language_list[$from_lang] : 'English')->To($to); } catch (Exception $e) { } if ($convertedstring === NULL) { $convertedstring = $str_in; } do { $before = $convertedstring; $convertedstring = preg_replace('#(<span class="notranslate">\\d+) (.*</span>)#', '${1}</span> <span class="notranslate">${2}', $convertedstring); } while ($before != $convertedstring); foreach (array_reverse($rep) as $_j => $from) { $j = intval(substr($_j, 1)); $convertedstring = preg_replace('#\\s*<span class="notranslate">\\s*' . preg_quote(strval($j)) . '\\s*</span>\\s*#', $from, $convertedstring); $convertedstring = preg_replace('# conv' . preg_quote(strval($j)) . '\\s*#', $from, $convertedstring); } $convertedstring = str_replace('<html> ', '', $convertedstring); $convertedstring = str_replace(''', '', $convertedstring); save_google_cache($str_in, $lang, $convertedstring); $str = $convertedstring; } else { $str = $chache['t_result']; } $DOING_TRANSLATE = false; if (function_exists('ocp_mark_as_escaped') && ocp_is_escaped($str_in)) { ocp_mark_as_escaped($str); } return $tempcode ? protect_from_escaping($str) : $str; }