From 6742f32c0044896c7bce407402841fcf87ad34cd Mon Sep 17 00:00:00 2001 From: diazoxide Date: Wed, 13 Nov 2019 18:49:44 +0400 Subject: [PATCH] Save urls only on page shutdown --- src/component/request/Request.php | 72 +++++++++++++++++------- src/component/translation/Translator.php | 8 ++- 2 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/component/request/Request.php b/src/component/request/Request.php index f4c90a7..2741c4d 100644 --- a/src/component/request/Request.php +++ b/src/component/request/Request.php @@ -288,7 +288,7 @@ public function setRefererTranslations(array $_referer_translations): void * Get Source Url from translate * Using ReTranslate method of Translation * - * @param string $translate Translated url + * @param string $translate Translated url * @param string $to_language Language of translated string * * @return string|null @@ -434,19 +434,24 @@ private function _prepareRefererSourceUrl(): bool */ private function _prepareSourceUrl(): bool { + $is_root_path = parse_url($this->getDestination(), PHP_URL_PATH) == '/'; /** * If current language is from_language * Then translate current url for all languages * */ if ($this->getLanguage() == $this->getFromLanguage() - || $this->getDestination() == '/' + || $is_root_path || !$this->getTranslation()->url->isPathTranslation() ) { $this->_setUrlTranslations( $this->getTranslation() ->setLanguages($this->context->languages->getAcceptLanguages()) - ->url->translate([$this->getDestination()]) + ->url->translate( + [$this->getDestination()], + $verbose, + true + ) [$this->getDestination()] ?? null ); @@ -472,7 +477,12 @@ private function _prepareSourceUrl(): bool $this->_setUrlTranslations( $this->getTranslation() ->setLanguages($this->context->languages->getAcceptLanguages()) - ->url->translate([$this->getSourceUrl()])[$this->getSourceUrl()] + ->url + ->translate( + [$this->getSourceUrl()], + $verbose, + true + )[$this->getSourceUrl()] ?? null ); } @@ -520,6 +530,26 @@ private function _prepareSourceUrl(): bool } } + /** + * Finally register shutdown function + * that should translate url for all languages, + * */ + register_shutdown_function( + function () { + if (!in_array(http_response_code(), [400, 401, 402, 403, 404])) { + $this->getTranslation() + ->setLanguages( + $this->context->languages->getAcceptLanguages() + ) + ->url->translate( + [$this->getSourceUrl()], + $verbose + ); + + } + } + ); + return true; } @@ -539,8 +569,8 @@ private function _redirect(string $url) /** * Restore non translated urls * - * @param string|null $url Url - * @param string $language Language + * @param string|null $url Url + * @param string $language Language * * @return string|null */ @@ -553,11 +583,11 @@ private function _restoreNonTranslatedUrl( * Get translation from source * */ $url = $this->getTranslation()->setLanguages([$language])->url - ->translate( - [$url], - $verbose, - true - )[$url][$language] ?? null; + ->translate( + [$url], + $verbose, + true + )[$url][$language] ?? null; if ($url == null) { return null; @@ -597,6 +627,8 @@ private function _prepare(): bool if ($this->_isExclusion()) { return false; } + + $this->_setTranslation($this->context->translation); $this->setFromLanguage($this->context->languages->getFromLanguage()); @@ -902,7 +934,7 @@ function (DOMXPath $xpath, DOMDocument $dom) { * Translate content * */ $content = $translator - ->translate([$content])[$content][$this->getLanguage()] + ->translate([$content])[$content][$this->getLanguage()] ?? $content; } @@ -1040,8 +1072,8 @@ public function start(): void * Get tags * To add on HTML document * - * @param DOMDocument $dom Document object - * @param DOMNode $parent Parent element + * @param DOMDocument $dom Document object + * @param DOMNode $parent Parent element * * @return void */ @@ -1062,8 +1094,8 @@ private function _addAlternateLinkNodes(DOMDocument $dom, DOMNode $parent): void * Get main JS object