How to get locale (current selected language store)

Hi,

Is there a way to find out the locale within a Custom Element.

It’s possible within the wix editor using the following code however when i use that code in the Custom Element, it breaks the Custom Element.

Essentially I need to know the current language selected on the front store (the locale) and depending on the language selected load different scripts e.g.
if language == italian then load a specific script if language == german load another script

I’m essentially just looking for a way to access the locale (current selected store language) within a custom element or even within a custom code script or some other way so long as i can access the value. I can’ t access the value from the Velo Wix editor from my own script it would seem.

import wixWindow from ‘wix-window’ ;

$w . onReady ( function () {
console . log ( wixWindow . multilingual . currentLanguage );

});

You can add a setter function to your Custom element that set’s the Custom Element’s locale setting, and then call that function from standard Velo code to set the locale that you get in the page’s Velo code.

Hi Yisrael,

I think i’ve added a set function to the custom element, please see below code:

const createDiv = ( locale ) => {
const div = document . createElement ( ‘div’ );
div . className += “test_local” ;
div . setAttribute ( “locale” , locale );
return div ;
}

class myLogoDisplay extends HTMLElement {
constructor () {
super ();
this . local ;
}

set local ( value ){
this . local = value ;
this . appendChild ( createDiv ( this . local ));
}

connectedCallback () {
this . appendChild ( createDiv ( “de” ));
}

}

customElements . define ( ‘test-local’ , myLogoDisplay );

Now the next part i’m not sure what to do " then call that function from standard Velo code to set the locale that you get in the page’s Velo code"

Do i declare another js file with $w.onReady as follows as per below, I’m new to this and don’ t fully understand how to do this. Can you have a look please if possible ?

import wixWindow from ‘wix-window’ ;
import myLogoDisplay from ‘public/custom-elements/my-logo-display.js’

$w . onReady ( function () {

let test = new myLogoDisplay ();
test . local ( wixWindow . multilingual . currentLanguage );
});