Get localized populate

System Information
  • Strapi Version: 4.3.8
  • Operating System: MacOS (local), Ubuntu Server 20.04 (deployment env)
  • Database: MariaDB 10
  • Node Version: 16.15.0
  • NPM Version: 8.10.0
  • Yarn Version: 1.22.11

Hi folks

I have an issue depending on localizations on related fields.
This is my scenario:

Collection type company (no localization) has a relation to collection type company-type (localized).
Every company has an entry from company-type (value e.g. inc, ltd, etc.).

Structure of company-type:

- id (integer)
- key (string, not localized)
- value (string, localized)

Let’s say a company registers under the english language, the id in the property company.type is the english entry in company-type (e.g. key ltd, which has id 1).

Now a user opens the company’s profile /companies/1, and has to see the company.type value. It will return the english entry from company-type. But the user may be using german has his interface language.

How i did it to solve it, but it doesnt seem to be very nice tbh.

const company = await strapi.query('api::company.company').findOne({
  where: { id: companyId },
  populate: {
    type: true
  }
})

company.type = await strapi.query('api::company-type.company-type').findOne({
  where: {
    $and: [
      { key: company.type.key },
      { locale: ctx.state.user.ui_lang } // property custom created 
    ]
  }
})

With this solution I am sending 2 requests, which creates unnecessary load. Is there a way to implement what I want into the populate request?

I already tried it like this:

populate: {
  type: {
    locale: ctx.state.user.ui_lang
  }
}

// or
populate: {
  type: {
    where: { locale: ctx.state.user }
  }
}

// or
populate: {
  type: {
    where: {
      locale: { $eq: ctx.state.user }
    }
  }
}

But my output on company.type is still the english one. Somewhat logical, because it’s bound to the id, which cannot relate directly to the localized entry:

"id": 1,
"slug": "353940-company-name",
"name": "Company Name",
"type": {
    "id": 1,
    "key": "inc",
    "label": "Inc.",
    "label_long": "Joint-stock company",
    "description": "The stock corporation (AG) is the typical legal form for large companies. It is the only corporate form with access to the capital market, i.e. the stock exchange. Its main advantage is the possibility of direct equity financing.",
    "locale": "en"
}

Kind regards

1 Like

Same problem, never found answer to that problem anywhere.