| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util\Translation\Provider; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\Translation\Support\AbstractTranslationProvider; |
| 6 |
use BitCode\BitForm\Core\Util\Translation\TranslationManager; |
| 7 |
|
| 8 |
/** |
| 9 |
* TranslatePress adapter. |
| 10 |
* |
| 11 |
* TP translates the rendered HTML itself, so the manager keeps the string |
| 12 |
* filter off the render path to avoid translating twice. TP's blind spot is |
| 13 |
* Bit Form's AJAX JSON, which is translated here. |
| 14 |
* |
| 15 |
* No string store, so AbstractStringRegistrationProvider is not extended. |
| 16 |
* |
| 17 |
* Known limitation: client-side validation messages live in the inline |
| 18 |
* bf_globals JSON and depend on TP's dynamic-translation setting. |
| 19 |
*/ |
| 20 |
final class TranslatePressProvider extends AbstractTranslationProvider |
| 21 |
{ |
| 22 |
public function translatesRenderedHtml() |
| 23 |
{ |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Validated bf_lang, else TP's current language. Locale-style codes, e.g. de_DE. |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
protected function detectLanguage() |
| 33 |
{ |
| 34 |
$requestLang = TranslationManager::getRequestLang(); |
| 35 |
if ('' !== $requestLang && in_array($requestLang, self::knownLanguages(), true)) { |
| 36 |
return $requestLang; |
| 37 |
} |
| 38 |
if (function_exists('trp_get_current_language')) { |
| 39 |
$current = trp_get_current_language(); |
| 40 |
if (is_string($current) && '' !== $current) { |
| 41 |
return $current; |
| 42 |
} |
| 43 |
} |
| 44 |
if (isset($GLOBALS['TRP_LANGUAGE']) && is_string($GLOBALS['TRP_LANGUAGE'])) { |
| 45 |
return $GLOBALS['TRP_LANGUAGE']; |
| 46 |
} |
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param string $string |
| 52 |
* @param string $context |
| 53 |
* @param int $formId |
| 54 |
* |
| 55 |
* @return mixed |
| 56 |
*/ |
| 57 |
protected function lookup($string, $context, $formId) |
| 58 |
{ |
| 59 |
if (!function_exists('trp_translate')) { |
| 60 |
return $string; |
| 61 |
} |
| 62 |
$lang = $this->getCurrentLanguage(); |
| 63 |
if ('' === $lang) { |
| 64 |
return $string; |
| 65 |
} |
| 66 |
// Third argument must stay false: TP's default wraps output in |
| 67 |
// <span data-no-translation>, which would corrupt the JSON response. |
| 68 |
return trp_translate($string, $lang, false); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* TP's configured language codes. Empty when they cannot be read, which makes |
| 73 |
* an unverifiable bf_lang fall through to TP's own current language rather |
| 74 |
* than being trusted. |
| 75 |
* |
| 76 |
* @return string[] |
| 77 |
*/ |
| 78 |
private static function knownLanguages() |
| 79 |
{ |
| 80 |
$settings = get_option('trp_settings'); |
| 81 |
if (!is_array($settings) || empty($settings['translation-languages']) || !is_array($settings['translation-languages'])) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
return array_values(array_filter($settings['translation-languages'], 'is_string')); |
| 85 |
} |
| 86 |
} |
| 87 |
|