templates/vitrine/components/canonical.html.twig line 1

Open in your IDE?
  1. {% set available_locales = app_locales|split('|') %}
  2. {% if page is not empty %}
  3.     {% set current_locale = app.request.locale %}
  4.     {% set current_url = app.request.schemeAndHttpHost ~ app.request.basePath ~ app.request.pathInfo %}
  5.     {# Fallback pages statiques pour EN #}
  6.     {% set static_fallbacks = {
  7.         'homepage': '/',
  8.         'press': '/press',
  9.         'faq': '/faq',
  10.         'articles': '/articles'
  11.     } %}
  12.     {# Construire les URLs par locale #}
  13.     {% set locale_urls = {} %}
  14.     {% for locale in available_locales %}
  15.         {% set path = getPathPageHasLanguages(page.id, locale) %}
  16.         {# Fallback EN pour pages statiques #}
  17.         {% if (path is empty or path == "#") and locale == "en" and page.name in static_fallbacks|keys %}
  18.             {% set path = static_fallbacks[page.name] %}
  19.         {% endif %}
  20.         {# Correction des doubles slashes (ex: /fr//) #}
  21.         {% set prefix = locale == "en" ? "" : "/" ~ locale %}
  22.         {% if path == prefix ~ "//" %}
  23.             {% set path = prefix == "" ? "/" : prefix %}
  24.         {% endif %}
  25.         {% set has_path = path is not empty and path != "#" %}
  26.         {% if has_path %}
  27.             {# Si c'est la locale courante, utiliser l'URL rĂ©elle #}
  28.             {% if locale == current_locale %}
  29.                 {% set locale_urls = locale_urls|merge({ (locale): current_url }) %}
  30.             {% else %}
  31.                 {% set locale_urls = locale_urls|merge({ (locale): domain ~ path }) %}
  32.             {% endif %}
  33.         {% endif %}
  34.     {% endfor %}
  35.     {# Canonical = URL de la locale courante #}
  36.     {% if locale_urls[current_locale] is defined %}
  37.         <link rel="canonical" href="{{ locale_urls[current_locale] }}" />
  38.     {% endif %}
  39.     {# Hreflang alternates #}
  40.     {% if locale_urls["en"] is defined %}
  41.         <link rel="alternate" hreflang="x-default" href="{{ locale_urls['en'] }}" />
  42.     {% endif %}
  43.     {% for locale, url in locale_urls %}
  44.         <link rel="alternate" hreflang="{{ locale }}" href="{{ url }}" />
  45.     {% endfor %}
  46. {% endif %}