PHP code example of phpro / mage2-module-translations

1. Go to this page and download the library: Download phpro/mage2-module-translations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpro / mage2-module-translations example snippets


location /media/phpro_translations/ {
    add_header X-Frame-Options "SAMEORIGIN";
    add_header Cache-Control "public";
    expires +1y;
}

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\NonTransactionableInterface;
use Phpro\Translations\Model\TranslationDataManagement;

class HelloWorldTranslations implements DataPatchInterface, NonTransactionableInterface
{
    private TranslationDataManagement $translationDataManagement;
    
    public function __construct(
        TranslationDataManagement $translationDataManagement
    ) {
        $this->translationDataManagement = $translationDataManagement;
    }

    public function apply() 
    {
        $this->translationDataManagement->prepare('Hello world!');
        $this->translationDataManagement->prepare('Welcome %1', 'Hello %1');
        // other translation keys here...
    }
}    

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\NonTransactionableInterface;
use Phpro\Translations\Model\TranslationDataManagement;

class HelloWorldTranslations implements DataPatchInterface, NonTransactionableInterface
{
    private TranslationDataManagement $translationDataManagement;

    public function __construct(
        TranslationDataManagement $translationDataManagement
    ) {
        $this->translationDataManagement = $translationDataManagement;
    }

    public function apply() 
    {
        $this->translationDataManagement->create('Hello world!', 'Hallo wereld!!!', ['nl_NL', 'nl_BE']);
        $this->translationDataManagement->create('Hello world!', 'Hello world!!!', ['en_US']);
        // other translation keys here...
    }
}

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\NonTransactionableInterface;
use Phpro\Translations\Model\TranslationDataManagement;

class DeleteHelloWorldTranslations implements DataPatchInterface, NonTransactionableInterface
{
    private TranslationDataManagement $translationDataManagement;

    public function __construct(
        TranslationDataManagement $translationDataManagement
    ) {
        $this->translationDataManagement = $translationDataManagement;
    }

    public function apply() 
    {
        $this->translationDataManagement->delete('Hello world!');
        $this->translationDataManagement->delete('Welcome %1', ['nl_BE', 'nl_NL']);
        // other translation keys here...
    }
}