Get current locale url and redirect to new locale url

supermanoul
New Member
2 0 0

Hello.

 

We are translating our store using the GraphiQL API (https://shopify.dev/tutorials/translating-content-for-online-store). Now that we have our content translated we are willing to develop our own language switcher and redirect users accordingly when they use it.

 

So if our site is shopifystore.com and the user clicks on French in the language switcher we want the page to redirect to to shopifystore.com/fr. We can do this using pure javascript but is there a way to detect current locale url and get the rest of locale urls in liquid? If so, it would be a better solution than having to parse and deal with the actual url in a javascript string.

 

The "problem" is not dealing with shopifystore.com/lang but with shopifystore.com/lang/products/productname. Also taking into account that the main active language of the store doesn't have the /lang subdirectory. This is why it would be nice if there is already a liquid workaround to access to the locales urls.

 

Thank you.

 

Manuel.

Replies 4 (4)
vix
Shopify Staff
Shopify Staff
537 103 113

@supermanoul You can retrieve the shops locales from Liquid but using JS to get the customer is the correct way to go. 

Vix | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

nordov
New Member
1 0 2

The way I see it I already have the value in shop_locale. Using JS to retrieve something I already have seems counterproductive to me. Why is it then the correct way to go?

PublicApps
Shopify Partner
142 5 34

 

<script>
  const urls = {
  {% for locale in shop.published_locales %}
    {{ iso_code }}: {{ locale.root_url }}
  {% endfor %}
  };
</script>

 

 Active locale is: 

 

const activeLocale = '{{ request.locale.iso_code }}';

 

 

Public Apps | Theme customization & App development
 - Was my reply useful? Like it to let me know!
 - Did I answer your question? Please mark as Accepted Solution.
 - Need more help? Contact us.

sillycube
Shopify Partner
693 16 101

@PublicApps 

Why not just use Shopify.locale for js but use 

const activeLocale = '{{ request.locale.iso_code }}';

?? 

BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview