Message-ID: <1290032861.3586.1485853765658.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3585_1914745309.1485853765633" ------=_Part_3585_1914745309.1485853765633 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Version compatibility
Language switcher in Symfony stack is compatible with eZ Pu= blish 5.3 / 2014.05
A content item can be translated in several languages. Those langu= ages are configured in the system and exposed in SiteAccesses via a priorit= ized list of languages:
ezpublish system: eng: languages: [eng-GB] # In fre siteaccess, fre-FR is always preferred, and fallback to en= g-GB if needed. fre: languages: [fre-FR, eng-GB]=20
When visiting a Content item, it may be useful to let the us= er switch from one translation to another, more appropriate to him. This is= precisely the goal of the language switcher.
The language switcher relies on the Cross-SiteAccess linking feature<=
/a> to generate links to the content's translation, and on =
RouteReference feature.
Tip
If you install the DemoBundle with at least 2 differen= t languages, you will be able to see the Language Switcher and to test it.<= /span>
Configuration is not mandatory, but can help to = distinguish which SiteAccesses can be considered translatio= n SiteAccesses.
ezpublish: siteaccess: default_siteaccess: eng list: - ezdemo_site - eng - fre - ezdemo_site_admin groups: ezdemo_frontend_group: - ezdemo_site - eng - fre # ... system: # Specifying which SiteAccesses are used for translation ezdemo_frontend_group: translation_siteaccesses: [fre, eng] eng: languages: [eng-GB] fre: languages: [fre-FR, eng-GB] ezdemo_site: languages: [eng-GB]=20
Note: Top prioritized language is always used for as th=
e SiteAccess language reference (e.g. fre-FR
for&nbs=
p;fre
SiteAccess in the example above).
If several translation SiteAccesses share the same language reference,&n= bsp;the first declared SiteAccess always wins.
There are some cases where your SiteAccesses share settings (repository,=
content settings...), but you don't want all of them to share the same&nbs=
p;translation_siteaccesses
setting. This can be the case =
when you use SiteAccesses for mobile.
Solution is as easy as defining new groups:
ezpublish: siteaccess: default_siteaccess: eng list: - ezdemo_site - eng - fre - mobile_eng - mobile_fre - ezdemo_site_admin groups: # This group can be used for common front settings ezdemo_common_group: - ezdemo_site - eng - fre - mobile_eng - mobile_fre ezdemo_frontend_group: - ezdemo_site - eng - fre ezdemo_mobile_group - mobile_eng - mobile_fre # ... system: # Translation SiteAccesses for regular frontend ezdemo_frontend_group: translation_siteaccesses: [fre, eng] # Translation SiteAccesses for mobile frontend ezdemo_mobile_group: translation_siteaccesses: [mobile_fre, mobile_eng] eng: languages: [eng-GB] fre: languages: [fre-FR, eng-GB] ezdemo_site: languages: [eng-GB] mobile_eng: languages: [eng-GB] mobile_fre: languages: [fre-FR, eng-GB]=20
If translation_siteaccesses
setting is not provid=
ed, implicit related SiteAccesses will be used instead. =
SiteAccesses are considered related if they share:
To generate a language switch link, you need to generate the =
RouteReference
, with the langua=
ge
parameter. This can easily be done with
{# Given that "location" variable is a valid Location object #} <a href=3D"{{ url( ez_route( location, {"language": "fre-FR"} ) ) }}">= ;{{ ez_content_name( content ) }}</a> {# Generating a link to a declared route instead of Location #} <a href=3D"{{ url( ez_route( 'my_route', {"language": "fre-FR"} ) ) }}"&= gt;My link</a>=20
You can also omit the route, in this case, the current route= will be used (i.e. switch the current page):
{# Using Twig named parameters #} <a href=3D"{{ url( ez_route( params=3D{"language": "fre-FR"} ) ) }}">= My link</a> {# Identical to the following, using ordered parameters #} <a href=3D"{{ url( ez_route( null, {"language": "fre-FR"} ) ) }}">My = link</a>=20
When using sub-requests, you lose the context of the master request (e.g= . current route, current location...). This is because sub-requests can be = displayed separately, with ESI or Hinclude.
If you want to render language switch links in a sub-request with a corr=
ect RouteReference
, you must pass it as an argument to yo=
ur sub-controller from the master request.
{# Render the language switch links in a sub-controller #} {{ render( controller( 'AcmeTestBundle:Default:languages', {'routeRef': ez_= route()} ) ) }}=20
namespace Acme\TestBundle\Controller; use eZ\Bundle\EzPublishCoreBundle\Controller; use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference; class DefaultController extends Controller { public function languagesAction( RouteReference $routeRef ) { return $this->render( 'AcmeTestBundle:Default:languages.html.twi= g', array( 'routeRef' =3D> $routeRef ) ); } }=20
{# languages.html.twig #} {# Looping over all available languages to display the links #} {% for lang in ezpublish.availableLanguages %} {# This time, we alter the "siteaccess" parameter directly. #} {# We get the right siteaccess with the help of ezpublish.translationSi= teAccess() helper #} {% do routeRef.set( "siteaccess", ezpublish.translationSiteAccess( lang= ) ) %} <a href=3D"{{ url( routeRef ) }}">{{ lang }}</a><br />= ; {% endfor %}=20
ezpublish.translationSiteAccess( language )
returns t=
he SiteAccess name for provided language (or null
if=
it cannot be found)ezpublish.availableLanguages()
returns the list of av=
ailable languages.You can easily generate language switch links from PHP too, with t=
he RouteReferenceGenerator
service:
// Assuming we're in a controller /** @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGener= atorInterface $routeRefGenerator */ $routeRefGenerator =3D $this->get( 'ezpublish.route_reference.generator'= ); $routeRef =3D $routeRefGenerator->generate( $location, array( 'language'= =3D> 'fre-FR' ) ); $link =3D $this->generateUrl( $routeRef );=20
You can also retrieve all available languages with the =
TranslationHelper
:
/** @var \eZ\Publish\Core\Helper\TranslationHelper $translationHelp= er */ $translationHelper =3D $this->get( 'ezpublish.translation_helper' ); $availableLanguages =3D $translationHelper->getAvailableLanguages();=20