|
|
|
@ -8594,3 +8594,56 @@ function api_get_language_translate_html() |
|
|
|
|
}); |
|
|
|
|
'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Filter a multi-language HTML string (for the multi-language HTML |
|
|
|
|
* feature) into the given language (strip the rest). |
|
|
|
|
* |
|
|
|
|
* @param string $htmlString The HTML string to "translate". Usually <p><span lang="en">Some string</span></p><p><span lang="fr">Une chaîne</span></p> |
|
|
|
|
* @param string $language The language in which we want to get the |
|
|
|
|
* |
|
|
|
|
* @throws Exception |
|
|
|
|
* |
|
|
|
|
* @return string The filtered string in the given language, or the full string if no translated string was identified |
|
|
|
|
*/ |
|
|
|
|
function api_get_filtered_multilingual_HTML_string($htmlString, $language = null) |
|
|
|
|
{ |
|
|
|
|
if (api_get_configuration_value('translate_html') != true) { |
|
|
|
|
return $htmlString; |
|
|
|
|
} |
|
|
|
|
$userInfo = api_get_user_info(); |
|
|
|
|
$languageId = 0; |
|
|
|
|
if (!empty($language)) { |
|
|
|
|
$languageId = api_get_language_id($language); |
|
|
|
|
} elseif (!empty($userInfo['language'])) { |
|
|
|
|
$languageId = api_get_language_id($userInfo['language']); |
|
|
|
|
} |
|
|
|
|
$languageInfo = api_get_language_info($languageId); |
|
|
|
|
$isoCode = 'en'; |
|
|
|
|
|
|
|
|
|
if (!empty($languageInfo)) { |
|
|
|
|
$isoCode = $languageInfo['isocode']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Split HTML in the separate language strings |
|
|
|
|
// Note: some strings might look like <p><span ..>...</span></p> but others might be like combine 2 <span> in 1 <p> |
|
|
|
|
if (!preg_match('/<span.*?lang="(\w\w)">/is', $htmlString)) { |
|
|
|
|
return $htmlString; |
|
|
|
|
} |
|
|
|
|
$matches = []; |
|
|
|
|
preg_match_all('/<span.*?lang="(\w\w)">(.*?)<\/span>/is', $htmlString, $matches); |
|
|
|
|
if (!empty($matches)) { |
|
|
|
|
// matches[0] are the full string |
|
|
|
|
// matches[1] are the languages |
|
|
|
|
// matches[2] are the strings |
|
|
|
|
foreach ($matches[1] as $id => $match) { |
|
|
|
|
if ($match == $isoCode) { |
|
|
|
|
return $matches[2][$id]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// Could find the pattern but could not find our language. Return the first language found. |
|
|
|
|
return $matches[2][0]; |
|
|
|
|
} |
|
|
|
|
// Could not find pattern. Just return the whole string. We shouldn't get here. |
|
|
|
|
return $htmlString; |
|
|
|
|
} |
|
|
|
|