Smile-SA / elasticsuite

Smile ElasticSuite - Magento 2 merchandising and search engine built on ElasticSearch
https://elasticsuite.io
Open Software License 3.0
761 stars 341 forks source link

Attribute option translation #3117

Open Nuranto opened 12 months ago

Nuranto commented 12 months ago

Preconditions

Magento Version : 2.4.6-p3

ElasticSuite Version : 2.11.4.1

Steps to reproduce

  1. Create filterable attribute with custom source model : [ ['value'=>1, 'label' => __('Apple')->render()], ['value'=>2, 'label' => __('Car')->render()]. ]
  2. Add in fr_FR translation file : "Car,Voiture" and "Apple,Pomme"
  3. Reindex catalogsearch_fulltext
  4. Check filters on your fr_FR store

Expected result

  1. Filter options are in french (Pomme, Voiture)

Actual result

  1. Filter options are in english (Apple, car)

Additional informations

Quick fix I found is to manually load translations in my source model :

        $areaList = ObjectManager::getInstance()->get(\Magento\Framework\App\AreaList::class);
        $area = $areaList->getArea($this->appState->getAreaCode());
        $area->load(\Magento\Framework\App\Area::PART_TRANSLATE);

But that's obviously not a good solution, that should be done somewhere in smile module. I did not found the correct place to do it though, we should probably emulate store somewhere in index rebuilding process.

EDIT : This solution does not work, it loads the translations, yes, but does not reload it for each store. So it just solve the problem to one store, and move the issue to the other one. So we definitely need store emulation here.

vahonc commented 11 months ago

Hello @Nuranto,

I was able to reproduce your issue. Indeed the attribute options translation does not work through any language (e.g. fr_FR) translation file no matter if it's using the custom source model or default. This is due to the fact that Elasticsearch indexes an Apple value, so if you try to search Pomme you don't find any products.

We're not a huge fan of adding an emulate store somewhere in the index rebuilding process. As an easy solution, Magento proposes a way to add translation via Admin Panel -> Attribute Properties -> Manage Options (Values of Your Attribute). Therefore I have a question, why you are using a custom source model and why is translation via Admin not suitable for you?

BR, Vadym

Nuranto commented 11 months ago

Hi @vahonc,

It reminds me a similar question from Romain here : https://github.com/Smile-SA/elasticsuite/issues/2932

Here are some examples :

Maybe store emulation is not necessary. Maybe locale emulation is enough (Magento\Framework\Locale\Resolver::emulate()) ?

romainruaud commented 11 months ago
  • An attribute that has a dynamic source. For example, you may want to create a Model/DB schema/CRUD for brands, and link those to products, so you can then easily, for instance, add a link to the custom brand page on product view, or display extra brand informations, etc.. Native attribute example : country_of_manufacture (Both example are not very relevant regarding translation, but you get the idea)

In any case, this is exactly what does these 2 modules :

https://github.com/Smile-SA/magento2-module-custom-entity https://github.com/Smile-SA/magento2-module-custom-entity-product-link (this one requires Elasticsuite)

Nuranto commented 11 months ago

I wasn't aware about those modules, and it looks great. I'll definitely look into it, however that won't solves the other 2 cases