Find a record regardless its locale

Hey,

Let’s assuming my model has a localized string field called myLocalizedField with 2 locales: locale1, locale2 where locale1 is the default one.

Is there a way to find a record where myLocalizedField has either myLocalizedField[locale1] or myLocalizedField[locale2] that match with a value?

Here is where I ended up if myLocalizedField[locale1] is equal to foo and myLocalizedField[locale2] is equal to bar

query withoutLocale {
  form(filter: { myLocalizedField: {eq: "foo"}}) {
    fieldA
  }
}

=> record found as myLocalizedField[locale1] = foo

query withLocale {
  form(filter: {myLocalizedField: {eq: "foo"}}, locale: locale2) {
    fieldA
  }
}

=> record not found as myLocalizedField[locale2] = bar

Having said that, I don’t want to loop on each locale to find the right record.

I will appreciate any hints on this :slight_smile:

Hi @paul1 :slight_smile:

I’m sorry that’s not possible at the moment :frowning: It seems like a good idea though. Can you please open a feature request here in https://community.datocms.com?

I think you can accomplish something like this by fetching all locale values for that field and then filter client side.

For example, if you don’t know the locales beforehand, you can use the _allFIELDNAMELocales field. Like this example:

query MyQuery {
  allCats {
    _allMyLocFieldLocales {
      locale
      value
    }
    id
  }
}

returns:

{
  "data": {
    "allCats": [
      {
        "_allMyLocFieldLocales": [
          {
            "locale": "en",
            "value": "another english content"
          },
          {
            "locale": "it",
            "value": "another italian content"
          }
        ],
        "id": "25552609"
      },
      {
        "_allMyLocFieldLocales": [
          {
            "locale": "en",
            "value": "english content"
          },
          {
            "locale": "it",
            "value": "italian content"
          }
        ],
        "id": "24756586"
      }
    ]
  }
}

Or you can use this other syntax too:

query MyQuery {
  allCats {
    id
    englishMyLocField:myLocField(locale: en),
    italianMyLocField:myLocField(locale: it)
  }
}

returns:

{
  "data": {
    "allCats": [
      {
        "id": "25552609",
        "englishMyLocField": "another english content",
        "italianMyLocField": "another italian content"
      },
      {
        "id": "24756586",
        "englishMyLocField": "english content",
        "italianMyLocField": "italian content"
      }
    ]
  }
}

But pay attention: these queries are paginated ( https://www.datocms.com/docs/content-delivery-api/pagination ). So maybe it’s better to find a way to reduce the scope, so that you get less records to analyze.