I know this post is pretty old, but for everyone who struggles with this topic, I want to share my solution.
First of all my case:
I want to implement a multi-language search-results-page for our website.
The primary language is English, so the URL for those pages does not have any language identifier in the slug.
www.example.com/some-page
The secundary language is German, so the URL needs the language identifier in the slug.
www.example.com/de/some-page
On several locations of the website we use hard coded phrases, which are translated with the help of the “html_lang” variable provided by HubSpot. To manage those hard coded phrases, I created a separate file called “lang.html”, which holds those phrases in different languages like this:
{
...
"search results": {
"en": "Search Results",
"de": "Suchergebnisse"
}
...
}
This works pretty well on our entire website, because now I only need to insert this file in my custom coded base template and then call a translation by …
{{ lang['search results'][html_lang] }}
BUT this won’t work on the search results page, since HubSpot does not provide to create a multi-lanugage version for those pages. So I needed to find a workaround. Therefore I setup the site search so it does contain the “language” param in the URL. So when I send a search query on the German website, I’ll get redirected to the URL www.example.com/search-results?term=something&language=de.
Pretty nice, since I now just need a little bit of HubL to manipulate the page content of the search results page.
First I need this URL somehow and I found the variable “request.path_and_query”, which works on every HubSpot Page. The rest is simple. Let me show you my code…
{# Get language from URL ('...&language=en') #}
{# First, store URL params into a list, like ['term=something', 'language=de'] #}
{% set url_params = request.path_and_query|regex_replace("(.*)\\?", '')|split('&') %}
{# Next, attach needed params to a dict, to make it usable later, like {'language': 'de'} #}
{# Feel free to add more params if needed #}
{% set params = {} %}
{% for param in url_params %}
{% set key_value = param|split('=')%}
{% if key_value[0] == 'language' %}
{% do params.update({
'language': key_value[1]
}) %}
{% endif %}
{% endfor %}
{# Finally, set language for search results page from params dict created earlier #}
{% if params.language %}
{% set html_lang = params.language %}
{% endif %}
Super nice, now I can use the “html_lang” variable again for things like “Search Results” which should be translated to “Suchergebnisse” by calling my language dictionary via …
<h1>{{ lang['search results'][html_lang] }}</h1>
{# English Search Results #}
<h1>Search Results</h1>
{# German Search Results #}
<h1>Suchergebnisse</h1>
If you found this helpful, give it a thumbs up.
Best,
Fabian