Run different scripts depending on active shop language

Hi,

I am trying to run different scripts depending on the currently active shop language.

I have tried solving this using the “if request.host contains” function as below.

{% if request.host contains '.com' or '.co.uk' or '.at/en' or '.fr/en' %}

 {% elsif request.host contains '.at' %} 

{% elsif request.host contains '.fr' %} 

{% endif %}

This generally works well, the problem is that I also have EN language enabled on my german storefront so the domain looks like this ‘.at/en’. Somehow the " / " seems to break this function so I was wondering if anyone knows of a different way to execute different scripts for different shop languages?

I would prefer not to use browser languages but the shop language itself.

Desperately looking for help so I’d appreciate any.

Best regards
Daniel

Hi @senkels ,

You can use ‘request.locale.iso_code’, it will better support using ‘request.host’. Refer https://shopify.dev/api/liquid/objects/shop-locale#shop_locale-iso_code

Ex code:

{% if request.locale.iso_code contains 'en' %}

 {% elsif request.locale.iso_code contains 'de' %} 

{% elsif request.locale.iso_code contains 'fr' %} 

{% endif %}

Hope it helps!

Hi @LitExtension

I greatly appreciate your help. I tried it this way, but it is causing a problem as you can see here:

https://senkels.at/pages/length-popup-1

Any other Idea how to do that?

Best regards,
Daniel

@LitExtension it actually seems to work but there is a problem in the theme.

If you have a look at this page and click the “Find my length” button, it should load that page with your if rules and scripts. but the popup stays empty.

Any Idea why?

https://p5swe4blaqkk7c5f-28767944738.shopifypreview.com/collections/sneaker-laces/products/white-6mm-flat

Hi @senkels ,

I checked and the content is not an empty popup yet. I don’t think it has anything to do with JS code, it could be because of your liquid declaration.

If it helped you solve your issue, please mark it as a solution. Thank you and good luck.