Symfony/Add locale selector

From Aimeos documentation

Symfony
Other languages:
English 100%


2016.x version

For shops offering multiple languages, currencies or both, Aimeos contains a locale selector component that renders menus of the configured language/currency combinations, so visitors are able to choose their preferred language and/or currency. By default, both will be part of the URL afterwards.

How to add locales for language/currency combinations is described in the user manual.

Configuration

To make the locale selector available in the templates, you need to add the component name to the page configuration. Add these settings for the "page" configuration in your ./app/config/config.yml file:

  1. aimeos_shop:
  2.     page:
  3.         account-index: ['locale/select','account/history','account/favorite','account/watch','basket/mini','catalog/session']
  4.         basket-index: ['locale/select','basket/standard','basket/related']
  5.         catalog-detail: ['locale/select','basket/mini','catalog/stage','catalog/detail','catalog/session']
  6.         catalog-list: ['locale/select','basket/mini','catalog/filter','catalog/stage','catalog/lists']

They add the "locale/select" component to the account, basket, catalog detail and catalog list templates where they can be rendered afterwards.

Templates

Symfony applications can overwrite the templates of bundles by copying a modified version of the template to the ./app/Resources/AimeosShopBundle/views/ directory.

In each page template where the locale selector should be available, you need to add the lines for rendering the header and body section of the locale selector:

  1. {% block aimeos_header %}
  2.     ...
  3.     {{ aiheader['locale/select']|raw }}
  4. {% endblock %}
  5.  
  6. {% block aimeos_head %}
  7.     ...
  8.     {{ aibody['locale/select']|raw }}
  9. {% endblock %}

Templates that should be modified:

  • Account/index.html.twig
  • Basket/index.html.twig
  • Catalog/detail.html.twig
  • Catalog/list.html.twig

Depending on the section you've added the body of the locale selector to, it will be rendered at different places. For more details about templates, please have a look into the article about adapting pages.

Routes

Language and currency ID chosen by the visitor are part of the URL by default, so they are explicit and can be cached (contrary to information based on session data). To make this work, you need to redefine all routes besides the "*adm" (admin) routes with a prefix in your ./app/config/routing.yml:

  1. aimeos_shop_account:
  2.     path: /{locale}/myaccount
  3.     defaults: { _controller: AimeosShopBundle:Account:index, locale: 'en' }
  4.  
  5. # ...

Adapt selector

The locale selector is a normal component with subparts, that can be adapted like any other component. If you e.g. only need a language or currency menu, you can remove the subpart you don't need via the client/html/locale/select/standard/subparts configuration.

Adapting the layout of the locale selector is possible via CSS.