Localization with subfolders: most optimal way to determine "base url"

Localization with subfolders: most optimal way to determine "base url"

Joost_Plas
Shopify Partner
16 1 2

I recently added a second locale to my shop, for which I'm using the subfolder structure.

But now I'm struggling to come up with a clean implementation to make sure all my links include the subfolder in the final URL.

 

The first issue seems to routes.root_url returns  / for the primary locale, and something like /nl for other locales. This means that you cannot just insert routes.root_url in your urls, but have to come up with some conditional logic (otherwise you will end up with double slashes for the primary domain). So my best attempt is currently:

relative base url: 
{% if localization.language.primary == false %}{{ routes.root_url }}{% endif %}

absolute base url:
{{ shop.url }}{% if localization.language.primary == false %}{{ routes.root_url }}{% endif %}


 This seems to work, but not very clean. I would suspect that Liquid would have better support for this. Am I missing an obvious variable?

 

And then the next issue is how I cleanly implement this throughout my application. I could ofcourse add the above code to all my links, but that feels very ugly.

 

I rather define a custom global variable, something like:

{% assign absoluteBaseUrl = {{ shop.url }}{% if localization.language.primary == false %}{{ routes.root_url }}{% endif %} %}

But I don't see a clean path forward in using absoluteBaseUrl throughout my files. I can't declare it in theme.liquid (because the variable is then only accessible in that file), so I would have to include this as a snippet in all my templates and pass the variable to all snippets which are includes using 'render'. Again; that feels far from clean.

 

Am I missing something? Any tips?

Replies 0 (0)