Language Switcher

Hello hubspot Community,
im currently looking to integrade a custom language switch to our project. Currently im using following solution and for the most sites this works. But on a few Subpages it does not pick the en href and if im in the en version it uses the english content slug for the german site. Does anyone ran into those problems before. I know there are a lot of posts about this but i coundent figure it out, since this is our first Hubspot project our knowledge is expandable.
Best regards,
Joshua Miller

One example:
this is the href it creates for my language switch:

/en/news/e3-gruppe-in-deutschland-mit-neuem-director-sales-europe-frank-marreau-verstaerkt-regionale-fuehrung-der-live-marketinggruppe

But /en/trends/head was maintained.

{% set eng_url = "/"~content.slug %}
{% set ger_url = "/"~content.slug %}
{% if content.translated_content['en'] %}
{% set eng_url = "/"~content.translated_content['en'].slug %}
{% endif %}
{% if content.translated_content['de'] %}
{% set ger_url = "/"~content.translated_content['de'].slug %}
{% endif %}
<a class="lang-switch language-switch--current" href="{{ ger_url }}" data-language="de">DE</a>
<a class="lang-switch" href="{{ eng_url}}" data-language="en">EN</a>

Hey, @joshuamiller :waving_hand: Thanks for your question.

For debugging, have you already tried:

  • Using the browser’s developer tools to inspect the generated links and ensure they’re correctly formed?
  • Simplifying the HubL code to test basic URL generation before adding conditions for language checks?
  • Temporarily adding HubL print statements to log out the `eng_url` and `ger_url` values to the console or as comments in the HTML for debugging?

Thanks for the additional information! — Jaycee

I tried this method, and noticed that HubSpot includes the query parameter in the retrieved url:

I fixed it by pre-emptively adding the hsLang parameter:

{% set eng_url = "/"~content.translated_content['en'].slug + "hsLang=en" %}