Reading list Switch to dark mode

    How to Make Multilingual Text in Shopware 6

    Updated 23 November 2022

    In this blog, we learn about how to make multilingual text in Shopware 6.

    It is not possible to create dynamic text in different languages with snippets so we use a multilingual database. Also, Shopware 6 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 transactions, shipments and orders.

    Creating Migration

    Let’s start with How to make multilingual Text in Shopware 6. For example, we are creating two tables in the database reason and reason_translation for a dynamic reason.

    In the reason table, we insert only the technical name of the reason and in another table reason_translation, we insert the reason name with reason id and language id.

    Start your headless eCommerce
    now.
    Find out More
    $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 the frontend, so filter reasons for the current language in our controller. For this, we fetch the current language id from the sales channel.

    Then with help of the reason repository, we can get reasons according to the 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 the admin end

    For creating a new reason we will add a language switch in our reason detail page before the smart bar action. So we can select a language for a reason.

    By using the below code in the twig file we can implement the language select drop-down like in the below image.

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

    Now we perform inserting multilingual reason by selecting the language id into the database, so we can get the current language id at index.js file by (Shopware.Context.api.languageId) on the selected language.

    Like migration, we insert the technical name for the reason in the reason table and insert the current language id. For this purpose, we get a reason repository by the repository factory method.

    Then insert entity data with language id into a reason object and then save the reason data into the entity. Now we have done creating multilingual text in shopware6. Thanks!! Happy Coding -)

     Shopware.Context.api.languageId

    I hope It will help you. Thanks

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home