Skip to content

Commit

Permalink
Save urls only on page shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
diazoxide committed Nov 13, 2019
1 parent 17dfd4f commit 6742f32
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
72 changes: 52 additions & 20 deletions src/component/request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);

Expand All @@ -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
);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
*/
Expand All @@ -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;
Expand Down Expand Up @@ -597,6 +627,8 @@ private function _prepare(): bool
if ($this->_isExclusion()) {
return false;
}


$this->_setTranslation($this->context->translation);
$this->setFromLanguage($this->context->languages->getFromLanguage());

Expand Down Expand Up @@ -902,7 +934,7 @@ function (DOMXPath $xpath, DOMDocument $dom) {
* Translate content
* */
$content = $translator
->translate([$content])[$content][$this->getLanguage()]
->translate([$content])[$content][$this->getLanguage()]
?? $content;

}
Expand Down Expand Up @@ -1040,8 +1072,8 @@ public function start(): void
* Get <link rel="alternate"...> tags
* To add on HTML document <head>
*
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
*
* @return void
*/
Expand All @@ -1062,8 +1094,8 @@ private function _addAlternateLinkNodes(DOMDocument $dom, DOMNode $parent): void
* Get main JS object <script> tag
* To add on HTML document <head>
*
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
*
* @return void
*/
Expand Down Expand Up @@ -1119,8 +1151,8 @@ private function _addMainJavaScriptNode(
* Get Editor JS <script> tag
* To add on HTML document <head>
*
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
*
* @return void
*/
Expand Down Expand Up @@ -1163,8 +1195,8 @@ private function _addEditorAssets(DOMDocument $dom, DOMNode $parent): void
* Get XHR(ajax) Manipulation javascript <script> tag
* To add on HTML document <head>
*
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
* @param DOMDocument $dom Document object
* @param DOMNode $parent Parent element
*
* @return void
*/
Expand Down
8 changes: 7 additions & 1 deletion src/component/translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use NovemBit\i18n\component\translation\exceptions\TranslationException;
use NovemBit\i18n\models\exceptions\ActiveRecordException;
use NovemBit\i18n\system\Component;
use NovemBit\i18n\system\helpers\Arrays;

/**
* Translation abstract method
Expand Down Expand Up @@ -267,9 +268,14 @@ public function translate(
/**
* Merge new and saved translations
* */
$translations = $translations + $new_translations;
//$translations = $translations + $new_translations;
$translations = Arrays::arrayMergeRecursiveDistinct(
$translations,
$new_translations
);
}


/*
* Event after translate
* */
Expand Down

0 comments on commit 6742f32

Please sign in to comment.