CMS Development

darivers
Member

Add the country/language to the url from the switcher

Hello all,

 

My client is actually using a localize.js to translate their pages. The translation works fine but for SEO and other reasons, they would like to add the locale in to the url. So when they switch to Germany the url will switch from www.example.com to www.example.com/de. And if it switchs to Spanish, it's look liek www.example.com/es.

We've set up multilanguage groups if you simply type in the country with the correct url and locale, it does automatically switch to that language, just not from the switcher. Any ideas? Is there a javascript solution to maybe make this happen?

 

Cheers

0 Upvotes
6 Replies 6
dbeau79
Contributor | Diamond Partner
Contributor | Diamond Partner

Add the country/language to the url from the switcher

When you hit the switcher does it go to the correct url (i.e. when you click Germany you see the "/de" in the url)?  Trying to understand if the switcher isn't going to the right page or if it is but not showing the translated content.

darivers
Member

Add the country/language to the url from the switcher

It's going to the right page but there's no /de. But if I manually add the /de it works

0 Upvotes
dbeau79
Contributor | Diamond Partner
Contributor | Diamond Partner

Add the country/language to the url from the switcher

I'm going to recap what's happening for my benefit (and for anyone else that wants to chime in).  You are using the localize language switcher, not the HubSpot language switcher.  The localize language switcher works in that it translates the page but it is not interacting the HubSpot multi-language by going to the different versions of the page.  So basically you have an English, Spanish, and German version of the page and on each one you can translate to the different languages.

 

I would approach this a different way and use the HubSpot multilanguage switcher.  That solves the problem of properly redirecting to correct url (with the added benefit of defaulting to that region depending on the visitor's browser settings).  To fire the localize code you could set up something like this in the template file header:

 

<script>

{% if content.language == "es" %}

Localize.setLanguage('es');

{% elif content.language == "de" %}

Localize.setLanguage('de');

{# insert any other language conditionals here #}

{% else %}

Localize.setLanguage('en');

{% endif %}

</script>

 

* The above code won't work on a translated blog as blogs don't have the content.language set properly.  If you need to use this on a blog you could swap out the conditional of {% if content.language == "es" %} with something like {% if request.path is string_containing "/es/" %}

 

** I'm not familiar with localize and how they load their script so you may need to adjust the positioning of this code or wrap in require tags.  

darivers
Member

Add the country/language to the url from the switcher

Thank you for this. I will give it a shot today and report back if it works!

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Add the country/language to the url from the switcher

Hey @darivers 

You might need to provide some code for us to take a look at 🤔

Maybe a link to an example page as well?

@Anton , @dbeau79  and ideas here?

0 Upvotes
darivers
Member

Add the country/language to the url from the switcher

Here's an example page: https://www.godfreywing.com/godfrey-and-wing-culture as it shows from the menu. The switcher is below in the lower right. Now if you go to https://www.godfreywing.com/de/godfrey-and-wing-culture it will go to the German translations but if you use the switcher below to change the lanuguage, the languge changes but the /de remains. 

0 Upvotes