Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow fetching active locales in any context #146

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Context/I18nContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ public function getActiveLanguages(): array

$languageData = $this->mapLanguageInfo($site->getLocale(), $site->getUrl());
$languageData['linkedHref'] = $site->getUrl();
$languageData['active'] = $site->getLanguageIso() === $this->localeDefinition->getLanguageIso();
$languageData['active'] = $site->getLocale() === $this->localeDefinition->getLocale();

foreach ($linkedLanguages as $linkedLanguage) {
if ($linkedLanguage['languageIso'] === $site->getLanguageIso()) {
if ($linkedLanguage['locale'] === $site->getLocale()) {
$languageData['linkedHref'] = $linkedLanguage['url'];

break;
Expand Down Expand Up @@ -192,6 +193,7 @@ public function getActiveCountries(): array
$countryData = [];
if (!empty($validCountries)) {
foreach ($validCountries as $country) {

if (is_null($country['isoCode'])) {
continue;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/FunctionalDefault/ActiveLocalesCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DachcomBundle\Test\FunctionalDefault;

use DachcomBundle\Test\Support\FunctionalTester;

class ActiveLocalesCest
{
public function testActiveLanguagesSelector(FunctionalTester $I): void
{
$site = $I->haveASite('test-domain1.test');

$document1 = $I->haveAPageDocumentForSite($site, 'de', ['action' => 'languageSelectorAction'], 'de');
$document2 = $I->haveAPageDocumentForSite($site, 'en', ['action' => 'languageSelectorAction'], 'en');

$I->haveTwoConnectedDocuments($document1, $document2);

$I->amOnPageWithLocale('http://test-domain1.test/en', 'en');

$I->seeElement('select option[selected][value="http://test-domain1.test/en"]');
}

public function testActiveCountriesSelector(FunctionalTester $I): void
{
$site = $I->haveASite('test-domain1.test');

$document1 = $I->haveAPageDocumentForSite($site, 'de', ['action' => 'languageCountrySelectorAction'], 'de');
$document2 = $I->haveAPageDocumentForSite($site, 'en', ['action' => 'languageCountrySelectorAction'], 'en');

$I->haveTwoConnectedDocuments($document1, $document2);

$I->amOnPageWithLocale('http://test-domain1.test/en', 'en');

$I->seeElement('li[data-country="GLOBAL"] li[data-language="en"]');
}
}
36 changes: 36 additions & 0 deletions tests/FunctionalDefaultCountry/ActiveLocalesCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DachcomBundle\Test\FunctionalDefaultCountry;

use DachcomBundle\Test\Support\FunctionalTester;

class ActiveLocalesCest
{
public function testActiveLanguagesSelector(FunctionalTester $I): void
{
$site = $I->haveASite('test-domain1.test');

$document1 = $I->haveAPageDocumentForSite($site, 'de-de', ['action' => 'languageSelectorAction'], 'de_DE');
$document2 = $I->haveAPageDocumentForSite($site, 'de-ch', ['action' => 'languageSelectorAction'], 'de_CH');

$I->haveTwoConnectedDocuments($document1, $document2);

$I->amOnPageWithLocale('http://test-domain1.test/de-de', 'de');

$I->seeElement('select option[selected][value="http://test-domain1.test/de-de"]');
}

public function testActiveCountriesSelector(FunctionalTester $I): void
{
$site = $I->haveASite('test-domain1.test');

$document1 = $I->haveAPageDocumentForSite($site, 'de-de', ['action' => 'languageCountrySelectorAction'], 'de_DE');
$document2 = $I->haveAPageDocumentForSite($site, 'de-ch', ['action' => 'languageCountrySelectorAction'], 'de_CH');

$I->haveTwoConnectedDocuments($document1, $document2);

$I->amOnPageWithLocale('http://test-domain1.test/de-ch', 'de');

$I->seeElement('li[data-country="CH"] li[data-language="de"]');
}
}
1 change: 1 addition & 0 deletions tests/_etc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ setup_files:
- { path: app/controller/DefaultController.php, dest: ./src/Controller/ }
- { path: app/templates/default.html.twig, dest: ./templates/default/ }
- { path: app/templates/language-selector.html.twig, dest: ./templates/default/ }
- { path: app/templates/country-selector.html.twig, dest: ./templates/default/ }
preload_files:
- { path: Services/TestAlternateListener.php }
- { path: Services/TestLinkGenerator.php }
Expand Down
5 changes: 5 additions & 0 deletions tests/_etc/config/app/controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function languageSelectorAction(Request $request): Response
{
return $this->render('default/language-selector.html.twig');
}

public function languageCountrySelectorAction(Request $request): Response
{
return $this->render('default/country-selector.html.twig');
}
}
53 changes: 53 additions & 0 deletions tests/_etc/config/app/templates/country-selector.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="{{ app.request.locale }}">
<head>
<meta charset="UTF-8">
<title>Test Page for I18n</title>
{{ pimcore_head_meta() }}
{{ pimcore_head_link() }}
</head>
<body>
<div id="site">
{% set current_context = i18n_current_context() %}
{% if current_context is not null %}
{% set countries = current_context.activeCountries %}
{% if countries is iterable %}
<nav id="navigation">
{% for country in countries %}
<ul>
<li class="country" data-country="{{ country.country.isoCode }}">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li data-language="{{ language.iso }}" {{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
</nav>
{% endif %}
{% endif %}

<div class="navigation">
{% set menu = pimcore_build_nav({
active: document,
root: document,
cache: app.request.locale
}) %}

{{ pimcore_render_nav(menu, 'menu', 'renderMenu', {
maxDepth: 10,
ulClass: 'nav navbar-nav'
}) }}
</div>
<div class="content">
{% if document is defined %}
<span id="page-id">{{ document.id }}</span>
{% endif %}
</div>
{% block content %}
{{ pimcore_areablock('dachcomBundleTest') }}
{% endblock %}
</div>
</body>
</html>
Loading