<?php
namespace BaseBundle\Service;
use Pimcore\Model\Translation;
use Pimcore\Tool;
class TranslationService {
public function onTranslationChange() {
foreach (Tool::getValidLanguages() as $locale) {
$filePath = PIMCORE_WEB_ROOT.'/var/tmp/translation-'.$locale.'.js';
if (file_exists($filePath)) unlink($filePath);
}
}
public static function generateJsFiles() {
foreach (Tool::getValidLanguages() as $locale) {
$filePath = PIMCORE_WEB_ROOT.'/var/tmp/translation-'.$locale.'.js';
if (file_exists($filePath)) {
continue;
}
$fileContent = ["var trmsg = {"];
$listClass = new Translation\Listing();
$listClass->setCondition('language = ? AND text != \'\'', [$locale]);
$translations = $listClass->loadRaw();
foreach ($translations as $translation) {
$translationTerm = Tool\Text::removeLineBreaks($translation['text']);
if (!empty($translationTerm)) {
$fileContent[] = "'".str_replace(["'", "\n"], ['\\\'', ''], $translation['key'])."': '".str_replace("'", '\\\'', $translationTerm)."',";
}
}
$fileContent[] = "};";
file_put_contents($filePath, join(PHP_EOL, $fileContent));
}
}
}