Liquid, JavaScript, themes, sales channels
Hello,
We're trying to update our app (which is completely loaded via a ScriptTag, no liquid templating required) to support multiple locales on Shopify. The problem is, we can't seem to find any reference to the current locale on the site. I checked the Shopify window object, but it's seemingly absent.
How can we detect the current locale that Shopify is using? I suppose we could fall back to navigator.language, but I want to make sure we're using the same language that Shopify actively is.
Thanks!
I have the same issue. Any ideas?
did you find a solution for this problem?
Thank you.
There is another option that I have used maybe it can help you also: I have picked the selected language from the languages dropdown list and according to this i have manipulated the content by javascript.
I am not sure if something changed or not, but I have noticed that it can be daunting to find some JavaScript objects/methods.
Looking around I found a few ways that may help anyone trying to get the locale.
If you have any access to the Liquid templates you can inject and grab this from anywhere
{{ shop.locale }}
A common use case is (and most Shopify themes have had this from what I seen):
<html lang="{{ shop.locale }}">
Also looking around in the DOM (could not find it in my theme and I am assuming Shopify may be injecting this, but you can grab this script tag and grab that locale value)
<script id="shopify-features" type="application/json">{
"accessToken":"<token>",
"betas":[],
"domain":<domain>",
"predictiveSearch":true,
"shopId":ID,
"smart_payment_buttons_url":"<URL>",
"dynamic_checkout_cart_url":"<URL>",
"locale":"en"
}</script>
However the easiest way within Shopify and probably should have just lead with.. It is part of the Shopify global object.
Shopify.locale
I did cross reference this with some other languages and it does give the nn-NN codes where multiple variations exist.
For English adding the -** part would fall into the developers hands.
Is this Shopify.locale a must-have object in js? I can't find it on one of my client stores
User | RANK |
---|---|
21 | |
18 | |
9 | |
7 | |
6 |
Explore the 30-30-30 rule, a dynamic social media strategy for new businesses. Learn how t...
By Trevor Sep 20, 2023Discover how to leverage the often overlooked footer of your ecommerce site to gain custom...
By Skye Sep 15, 2023In this blog, we’ll be shining a light on Shopify Partners, Experts, and Affiliates. Who a...
By Imogen Sep 13, 2023