How to get supported locales in Plugins

Hi,

I want to be able to get the supported locales on the site that my plugin is installed on.

I had a look around in the CTX and Client but I couldnā€™t find the right thing, I assumed it would be:

const client = buildClient({
    apiToken,
  });

  client.site.locales

As I know for my website I can run this query:

const localesQuery = `query {
  _site {
    locales # -> ["en", "it", "fr"]
  }
}`

Essentially I am looking for the equivalent of this call (that I can do on my website that uses Dato) within the plugin I am writing:

let supportedLanguages:string[] = [];
export const getSupportedLocales = (): string[] => {
    if (supportedLanguages.length === 0) {
        
         fetchDataDato<QueryResult>(localesQuery).then((root: QueryResult) => {            
             supportedLanguages = root.data._site.locales;

        });
    }

    return supportedLanguages;
};

Would I just have to run a real GQL query? using the apiToken?

Hey @chrispepper1989,

You should be able to use ctx.site.attributes.locales to get that info. Returns something like [ "en", "bg" ]. The first one is the projectā€™s primary locale.

Does that help? Let me know if thereā€™s somewhere youā€™re expecting that to be available but it isnā€™t.

Related:

  • ctx.ui.locale is what the editorā€™s UI is currently set to (the text they see for menus, buttons, etc.)
  • ctx.locale is what the current ā€œrecord localeā€ is set to (i.e., which locale the user is currently editing). Thatā€™s in quotes because this is a UI abstractionā€¦ the underlying record is actually localized on a per-field basis, but we show it to the editors as though it were a record-wide setting. Itā€™s just for visual clarity (so they donā€™t accidentally edit one field in English and another in Bulgarian, for example).

That is perfect, thank you @roger !! :slight_smile: