Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5490

How to make multi-lingual Text in Shopware6

$
0
0

Introduction

In this blog we learn about How to make multi-lingual Text in Shopware6.

Overview

It is not possible to create dynamic text in different languages with snippets so we use multilingual database. Also shopware6 has a state machine to handle the state of an entity. You should use StateMachineState Field for the state transition, like it is done for transaction, shipments and orders.

Creating Migration

Let’s start with How to make multi-lingual Text in Shopware6 . For example we are creating two tables in database reason and reason_translation for dynamic reason.In reason table we insert only technical name of reason and another table reason_translation we insert reason name with reason id and language id.

$reasonId = Uuid::randomBytes();
        $connection->insert('wk_plugin_rma_reason', ['id'=> $inProgressId,'technical_name'=>'size','created_at'=>(new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)]);
        $connection->insert('wk_plugin_rma_reason_translation', ['id'=>Uuid::randomBytes(),'status_id'=>$reasonId, 'language_id'=>$languageEN, 'name'=>'Size not fit', 'created_at'=> (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)]);
        $connection->insert('wk_plugin_rma_status_translation', ['id'=>Uuid::randomBytes(),'status_id'=>$reasonId, 'language_id'=>$languageDE, 'name'=>'Größe nicht passen', 'created_at'=> (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)]);

Display on Storefront

Now we are showing reasons in front-end , so filter reasons for current language in our controller.For this we fetch current language id from sales channel, then with help of reason repository we can get reasons according to current sales channel language.

$currentLanguageId = $context->getSalesChannel()->getLanguageId();
  $reasonRepository = $this->container->get( 'wk_plugin_rma_reason_translation.repository' );
    $reasonList = $reasonRepository->search(
                    (new Criteria())->addFilter(new EqualsFilter('languageId', $currentLanguageId)),
                    Context::createDefaultContext()
                );

Creating reason in admin end

For creating a new reason we will add a language switch in our reason detail page before smart bar action. So we can select a language for a reason. By using below code in twig file we can implement the language select drop down like as below image.

{% block rma_detail_language_switch %}
        <sw-language-switch slot="language-switch"></sw-language-switch>
 {% endblock %}

Now we perform inserting multilingual reason with selecting language id into database, so we can get current language id at index.js file by (Shopware.Context.api.languageId) on selecting language. Like migration we insert technical name for reason in reason table and insert current language id. For this purpose we get a reason repository by repositoryFactory method then insert entity data with language id into a reason object and then save the reason data into entity .Now we have done creating multilingual text in shopware6. Thanks!! Happy Coding -)

Shopware.Context.api.languageId

I hope It will help you. Thanks


Viewing all articles
Browse latest Browse all 5490

Trending Articles