Skip to main content

Code Examples

I18n Context

NameDescription
getRouteItemreturn RouteItemInterface
getZonereturns ZoneInterface
getLocaleDefinitionreturns LocaleDefinitionInterface
getCurrentZoneSitereturns ZoneSiteInterface
getCurrentLocaleget current locale
getCurrentLocaleInfoget detailed information about active locale
getLocaleInfoget locale info of given locale from locale
getZoneDefaultLocaleget default locale of zone
getZoneActiveLocalesreturns an array of all active/allowed locales in zone
getZoneGlobalInfointernational state
getCurrentCountryAndLanguageget country and locale info
getLinkedLanguagesget all (raw) linked documents of current route
getActiveLanguageshelper method to list all active languages of current route (see navigation code example)
getActiveCountrieshelper method to list all active countries and languages of current route (see navigation code example)
getLanguageNameByIsoCodehelper to get language name by iso code
getCountryNameByIsoCodehelper to get country name by iso code

Fetch Context Data in Twig

{# 
be careful, i18n_current_context is allowed to return null!
#}

{% set current_locale = i18n_current_context().localeDefinition.locale %}
{% set current_language_iso = i18n_current_context().localeDefinition.languageIso %}
{% set current_country_iso = i18n_current_context().localeDefinition.countryIso %}

{{ dump(i18n_current_context().localeDefinition.locale) }}
{{ dump(i18n_current_context().linkedLanguages) }}
{{ dump(i18n_current_context().activeLanguages) }}
{{ dump(i18n_current_context().activeCountries) }}
{{ dump(i18n_current_context().languageNameByIsoCode(current_language_iso, current_locale)) }}
{{ dump(i18n_current_context().countryNameByIsoCode(current_country_iso, current_locale)) }}

{{ dump(i18n_current_context().zoneActiveLocales()) }}
{{ dump(i18n_current_context().localeInfo('de', 'id')) }}

Fetch Context Data in PHP

<?php

use Symfony\Component\HttpFoundation\RequestStack;
use OpenDxp\Bundle\I18nBundle\Http\I18nContextResolverInterface;

class Service
{
public function myAction(RequestStack $requestStack, I18nContextResolverInterface $i18nContextResolver)
{
$i18nContext = $i18nContextResolver->getContext($requestStack->getMainRequest());

$LinkedLanguages = $i18nContext->getLinkedLanguages();

// get current locale
$currentLocale = $i18nContext->getLocaleDefinition()->getLocale();

// get current language iso
$currentLanguageIso = $i18nContext->getLocaleDefinition()->getLanguageIso();

// get linked languages
$linkedLanguages = $i18nContext->getLinkedLanguages();

// get linked languages, but only rootDocuments
$linkedLanguages = $i18nContext->getLinkedLanguages(true);

// get language name by iso code
$languageName = $i18nContext->getLanguageNameByIsoCode($currentLanguageIso, $currentLocale);

// locale provider info
$localProviderActiveLocales = $i18nContext->getZoneActiveLocales();
$localeProviderLocaleInfo = $i18nContext->getLocaleInfo('de', 'id');
}
}

Zone

The zone represents an instance of I18nZoneInterface which comes with some helper methods:

NameDescription
getIdgiven zone id (null, if it no zones have been configured
getNamegiven zone name (null, if no zones have been configured
getDomainsall available domains for given zone.
getTranslationsarray, translations for dynamic routes
isActiveZonecheck if zone is active one
getLocaleUrlMappingFor example: de-ch. Mostly used to build static routes
getGlobalInfointernational state
getSitesget all corresponding sites

Fetch Zone Data in Twig

{# 
be careful, i18n_current_context is allowed to return null!
#}

{{ dump(i18n_current_context().zone.sites) }}

Fetch Zone Data in PHP

<?php

use Symfony\Component\HttpFoundation\RequestStack;
use OpenDxp\Bundle\I18nBundle\Http\I18nContextResolverInterface;

class Service
{
public function myAction(RequestStack $requestStack, I18nContextResolverInterface $i18nContextResolver)
{
$i18nContext = $i18nContextResolver->getContext($requestStack->getMainRequest());

$zone = $i18nContext->getZone();

// list all zone sites
$zoneSites = $zone->getSites(true);
}
}

Zone Current Site Information

To retrieve data from an active site, you may want to use the $i18nContext->getCurrentZoneSite() method. Since the current site gets defined via the current locale, be sure that locale is always available.

The current site represents an instance of ZoneSiteInterface which comes with some helper methods:

NameDescription
getSiteRequestContextreturns SiteRequestContext object
SiteRequestContext::getSchemeFor example: http
SiteRequestContext::getHttpPortFor example: 80
SiteRequestContext::getHttpsPortFor example: 443
SiteRequestContext::getDomainUrlFor example: https://opendxp-domain4.test
SiteRequestContext::getHostFor example: www.opendxp-domain4.test
SiteRequestContext::getNone3wHostFor example: opendxp-domain4.test
isRootDomainbool
getLocaleFor example: de_CH
getCountryIsoFor example: CH
getLanguageIsoFor example: de
getHrefLangFor example: de-ch
getLocaleUrlMappingFor example: de-ch. Mostly used to build static routes.
getUrlFor example: https://opendxp-domain4.test/de-ch
getHomeUrlstring
getFullPathFor example: domain4/de-ch
getTypeFor example: hardlink
getSubSitesarray
hasSubSitesbool

Fetch Zone Site Data in Twig

{# get current context info #}
{{ dump(i18n_current_context().currentZoneSite.url) }}
{{ dump(i18n_current_context().currentZoneSite.localeUrlMapping) }}

Fetch Zone Site Data in Twig

<?php

use Symfony\Component\HttpFoundation\RequestStack;
use OpenDxp\Bundle\I18nBundle\Http\I18nContextResolverInterface;

class Service
{
public function myAction(RequestStack $requestStack, I18nContextResolverInterface $i18nContextResolver)
{
$i18nContext = $i18nContextResolver->getContext($requestStack->getMainRequest());

$currentContextInfo = $i18nContext->getCurrentZoneSite()->getUrl();
$currentContextInfo = $i18nContext->getCurrentZoneSite()->getLocaleUrlMapping();
}
}

Language Selection

{% set current_context = i18n_current_context() %}
{% if current_context is not null %}
{% set languages = current_context.activeLanguages %}
{% if languages is iterable %}
<nav id="navigation">
<select>
{% for language in languages %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% endfor %}
</select>
</nav>
{% endif %}
{% endif %}

Country Grouped Selection

{% 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">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li{{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
</nav>
{% endif %}
{% endif %}

Complex Country / Language Selection based on Current Zone

<nav id="navigation">
{% set current_context = i18n_current_context() %}
{% if current_context is not null %}

{# country grouped dropdown #}
{% set countries = current_context.activeCountries %}
{% if countries is iterable %}
{% for country in countries %}
<ul>
<li class="country">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li{{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
{% endif %}

{# simple language dropdown #}
{% set languages = current_context.activeLanguages %}
{% if languages is iterable %}
<select>
{% for language in languages %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% endfor %}
</select>
{% endif %}

{% endif %}
</nav>