Skip to content

Commit

Permalink
Merge pull request #1258 from NatLibFi/fix-language-switch-url-take2
Browse files Browse the repository at this point in the history
More proper fix for language changing link URLs
  • Loading branch information
osma committed Dec 8, 2021
2 parents b7fd24e + bd07393 commit 5b9a75c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
9 changes: 7 additions & 2 deletions model/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
4 changes: 2 additions & 2 deletions view/topbar.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<ul class="dropdown-menu dropdown-menu-right">
{% for langcode, langdata in languages %}
{% if request.lang != langcode %}
<li><a id="language-{{ langcode }}" class="versal" href="{{ request.langurl|replace({("/#{request.lang}/"): "/#{langcode}/"}) }}"> {{langdata.lemma}}</a></li>
<li><a id="language-{{ langcode }}" class="versal" href="{{ request.langurl(langcode) }}"> {{langdata.lemma}}</a></li>
{% endif %}
{% endfor %}
</ul>
Expand All @@ -34,7 +34,7 @@
<div id="language"><span class="navigation-font">|</span>
{% for langcode, langdata in languages %}
{% if request.lang != langcode %}
<a id="language-{{ langcode}}" class="navigation-font" href="{{ request.langurl|replace({("/#{request.lang}/"): "/#{langcode}/"}) }}"> {{langdata.name}}</a>
<a id="language-{{ langcode}}" class="navigation-font" href="{{ request.langurl(langcode) }}"> {{langdata.name}}</a>
{% endif %}
{% endfor %}
</div>
Expand Down

0 comments on commit 5b9a75c

Please sign in to comment.