Integrations / Platforms / Magento 2 / How to Implement Multi-Language Search on a Single Store

How to Implement Multi-Language Search on a Single Store

This is a step by step guide on how to implement the ability to perform a search in several languages at the same time on a single store view in Algolia for the Magento 2 extension.

How it works

By default, the extension indexes each store-view data to a separate index and therefore each store view has its own dedicated index in a specific language to search in.

Examples of default records:

Index name: magento2_products_en

1
2
3
4
5
{
    “name”: “Shirt”
    “description”: “Very nice blue shirt”,
    // ... other attributes
}

Index name: magento2_products_de

1
2
3
4
5
{
    “name”: “Hemd”
    “description”: “Sehr schönes blaues Hemd”,
    // ... other attributes
}

Index name: magento2_products_es

1
2
3
4
5
{
    “name”: “Camisa”
    “description”: “Muy bonita camisa azul”,
    // ... other attributes
}

Each index is searched on a specific language instance of a store (a store view); for example, searching for Hemd on an English store won’t return any results.

To have the correct results when searching for Hemd, the extension needs to be customized and those 3 indices needs to be merged together.

In order to search on one store view in multiple languages, the records need to contain values in all languages. It should look like this:

Showcase record

1
2
3
4
5
6
7
8
9
10
11
12
13
{
    “name”: {
        “en”: “Shirt”,
        “de”: “Hemd”,
        “es”: “Camisa”
    },
    “description”: {
        “en”: “Very nice blue shirt”,
        “de”: “Sehr schönes blaues Hemd”,
        “es”: “Muy bonita camisa azul”
    },
    // ... other attributes
}

How to create this record in the extension

  1. Create a custom extension, which will allow you to listen on the extension’s custom events.
  2. Write a listener on algolia_after_create_product_object event, the same way as it’s written in the custom extension in the tutorial for event algolia_products_index_before_set_settings.
  3. From $observer variable passed to the execute() method you can fetch custom_data, productObject and subProducts data.
  4. The prepared Algolia record is structured as array in custom_data data and can be modified as easy as any other PHP array.
  5. Write custom code to fetch searchable attributes (like name, description, manufacturer, …) in all other languages and add it to the record in custom_data. Structure the data according to the Showcase record.
  6. Done!

Sample of Observer’s execute() method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public function execute(Observer $observer)
{
    /* @var \Magento\Catalog\Model\Product $product */
    $product = $observer->getData(productObject);

    /*
     * Fetch language specific values based on $product
     */

    $productRecord = $observer->getData(custom_data);

    $originalName = $productRecord[name];
    $originalDescription = $productRecord[description];

    $productRecord[name] = [
        en => $originalName,
        de => $germanName,
        es => $spanishName,
    ];

    $productRecord[description] = [
        en => $originalDescription,
        de => $germanDescription,
        es => $spanishDescription,
    ];
}

Index settings

If the record is structured as in a Showcase record, no specific settings need to be sent to Algolia because only parent attributes (like name, description, manufacturer, …) need to be set as searchable. This can be done in the UI configuration of the extension in Magento administration.

Did you find this page helpful?