Determine the current locale from JavaScript (ScriptTag)

lsapan
Shopify Partner
3 0 2

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!

XGen
Shopify Partner
1 0 0

I have the same issue. Any ideas?

0 Likes
ilanush
New Member
2 0 0

did you find a solution for this problem?

0 Likes
lsapan
Shopify Partner
3 0 2
Click to expand...
Unfortunately, no. We worked around it by just using the locale from the browser. It’s not perfect, but Shopify didn’t give us much of a choice.
0 Likes
ilanush
New Member
2 0 0

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.

0 Likes
AmalgaRabbitt
New Member
1 0 0

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

AmalgaRabbitt_1-1617965655986.png

I did cross reference this with some other languages and it does give the nn-NN codes where multiple variations exist.

AmalgaRabbitt_2-1617965997590.png

AmalgaRabbitt_3-1617966026554.png

AmalgaRabbitt_4-1617966057926.png

For English adding the -** part would fall into the developers hands.

0 Likes