From bd0739317a32edc1faff83857f1e3f7a6fcd7004 Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Wed, 8 Dec 2021 14:05:48 +0200 Subject: [PATCH] More proper fix for language changing link URLs. Related to PR #1234 which broke them on front page --- model/Request.php | 9 +++++++-- tests/RequestTest.php | 43 +++++++++++++++++++++++++++++++++++++++++++ view/topbar.twig | 4 ++-- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/model/Request.php b/model/Request.php index 4111bd310..28da72b48 100644 --- a/model/Request.php +++ b/model/Request.php @@ -174,11 +174,16 @@ public function getRequestUri() /** * Returns the relative page url eg. '/yso/fi/search?clang=en&q=cat' + * @param string $newlang new UI language to set * @return string the relative url of the page */ - public function getLangUrl() + public function getLangUrl($newlang=null) { - return substr(str_replace(str_replace('/index.php', '', $this->getServerConstant('SCRIPT_NAME')), '', $this->getServerConstant('REQUEST_URI')), 1); + $langurl = substr(str_replace(str_replace('/index.php', '', $this->getServerConstant('SCRIPT_NAME')), '', $this->getServerConstant('REQUEST_URI')), 1); + if ($newlang !== null) { + $langurl = preg_replace("#^(.*/)?{$this->lang}/#", "$1{$newlang}/", $langurl); + } + return $langurl; } public function getLetter() diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 7a432f750..c8e75897a 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -165,4 +165,47 @@ public function testGetVersion() { $version = $this->request->getVersion(); $this->assertNotEmpty($version); } + + /** + * @covers Request::getLangUrl + */ + public function testGetLangUrlNoParamRoot() { + $this->request->setServerConstant('SCRIPT_NAME', '/Skosmos/index.php'); + $this->request->setServerConstant('REQUEST_URI', '/Skosmos/en/'); + $langurl = $this->request->getLangUrl(); + $this->assertEquals("en/", $langurl); + } + + /** + * @covers Request::getLangUrl + */ + public function testGetLangUrlNoParamVocab() { + $this->request->setServerConstant('SCRIPT_NAME', '/Skosmos/index.php'); + $this->request->setServerConstant('REQUEST_URI', '/Skosmos/myvocab/en/'); + $langurl = $this->request->getLangUrl(); + $this->assertEquals("myvocab/en/", $langurl); + } + + /** + * @covers Request::getLangUrl + */ + public function testGetLangUrlNewLangRoot() { + $this->request->setServerConstant('SCRIPT_NAME', '/Skosmos/index.php'); + $this->request->setServerConstant('REQUEST_URI', '/Skosmos/en/'); + $this->request->setLang('en'); + $langurl = $this->request->getLangUrl("sv"); + $this->assertEquals("sv/", $langurl); + } + + /** + * @covers Request::getLangUrl + */ + public function testGetLangUrlNewLangVocab() { + $this->request->setServerConstant('SCRIPT_NAME', '/Skosmos/index.php'); + $this->request->setServerConstant('REQUEST_URI', '/Skosmos/myvocab/en/'); + $this->request->setLang('en'); + $langurl = $this->request->getLangUrl("sv"); + $this->assertEquals("myvocab/sv/", $langurl); + } + } diff --git a/view/topbar.twig b/view/topbar.twig index b7145f76f..260b4879b 100644 --- a/view/topbar.twig +++ b/view/topbar.twig @@ -25,7 +25,7 @@ @@ -34,7 +34,7 @@
| {% for langcode, langdata in languages %} {% if request.lang != langcode %} - {{langdata.name}} + {{langdata.name}} {% endif %} {% endfor %}