<?php
namespace App\mvk\Middleware\Web\EventListener;
use Exception;
use Pimcore\Event\Model\TranslationEvent;
use Pimcore\Model\Translation;
class TranslationSaveListener {
public function __construct()
{
define('MVK_APPLICATION_PREFIX', 'app');
}
public function saveTranslations(TranslationEvent $info) {
$translationKey = $info->getTranslation()->getKey();
if (!preg_match('/'. MVK_APPLICATION_PREFIX .'_.*?_(.*?)_.*/', $translationKey, $match)) {
return;
}
$type = $match[1];
$domain = Translation::DOMAIN_DEFAULT;
$objectPrefix = MVK_APPLICATION_PREFIX . '\_%\_' . $type . '\_%';
$list = new Translation\Listing();
$list->setDomain($domain);
$list->setOrder('asc');
$list->setLanguages(['de_DE']);
$list->setCondition("translations_messages.key LIKE ?", $objectPrefix);
$list->setOrderKey('translations_messages.key', false);
$list->load();
$listData = $list->getData();
$response = [];
foreach ($listData as $data) {
$response[$data->getKey()] = $data->getTranslation('de_DE');
}
$path = PIMCORE_PROJECT_ROOT . '/public/frontend/translation/' . $type;
if (!file_exists($path)) {
mkdir($path, 0775, true);
}
$jsonString = json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS);
file_put_contents($path . '/de.json', str_replace('\\\\','\\',$jsonString));
}
}