{% set available_locales = app_locales|split('|') %}
{% if page is not empty %}
{% set current_locale = app.request.locale %}
{% set current_url = app.request.schemeAndHttpHost ~ app.request.basePath ~ app.request.pathInfo %}
{# Fallback pages statiques pour EN #}
{% set static_fallbacks = {
'homepage': '/',
'press': '/press',
'faq': '/faq',
'articles': '/articles'
} %}
{# Construire les URLs par locale #}
{% set locale_urls = {} %}
{% for locale in available_locales %}
{% set path = getPathPageHasLanguages(page.id, locale) %}
{# Fallback EN pour pages statiques #}
{% if (path is empty or path == "#") and locale == "en" and page.name in static_fallbacks|keys %}
{% set path = static_fallbacks[page.name] %}
{% endif %}
{# Correction des doubles slashes (ex: /fr//) #}
{% set prefix = locale == "en" ? "" : "/" ~ locale %}
{% if path == prefix ~ "//" %}
{% set path = prefix == "" ? "/" : prefix %}
{% endif %}
{% set has_path = path is not empty and path != "#" %}
{% if has_path %}
{# Si c'est la locale courante, utiliser l'URL réelle #}
{% if locale == current_locale %}
{% set locale_urls = locale_urls|merge({ (locale): current_url }) %}
{% else %}
{% set locale_urls = locale_urls|merge({ (locale): domain ~ path }) %}
{% endif %}
{% endif %}
{% endfor %}
{# Canonical = URL de la locale courante #}
{% if locale_urls[current_locale] is defined %}
<link rel="canonical" href="{{ locale_urls[current_locale] }}" />
{% endif %}
{# Hreflang alternates #}
{% if locale_urls["en"] is defined %}
<link rel="alternate" hreflang="x-default" href="{{ locale_urls['en'] }}" />
{% endif %}
{% for locale, url in locale_urls %}
<link rel="alternate" hreflang="{{ locale }}" href="{{ url }}" />
{% endfor %}
{% endif %}