412 lines
9.3 KiB
PHP
412 lines
9.3 KiB
PHP
<?php
|
|
|
|
class LanguageTranslation extends BaseObject implements Persistent
|
|
{
|
|
/**
|
|
* Fraza
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $phrase;
|
|
|
|
/**
|
|
* Fraza dla aplikacji backend
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $backendPhrase;
|
|
|
|
/**
|
|
* Domyślne tłumaczenie dla frazy
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $defaultPhraseTranslation;
|
|
|
|
/**
|
|
* Tłumaczenie użytkownika dla frazy
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $userPhraseTranslation;
|
|
|
|
/**
|
|
* Wersja językowa
|
|
*
|
|
* @var Language
|
|
*/
|
|
protected $language;
|
|
|
|
/**
|
|
* Id Wersji językowej
|
|
*
|
|
* @var int
|
|
*/
|
|
protected $language_id;
|
|
|
|
/**
|
|
* Katalog tłumaczeń
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $catalogue;
|
|
|
|
/**
|
|
* Indeks w katalogu
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $index;
|
|
|
|
public function buildPkeyCriteria()
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(constant($this->getPeer().'::CATALOGUE'), $this->catalogue);
|
|
$c->add(constant($this->getPeer().'::INDEX'), $this->index);
|
|
$c->add(constant($this->getPeer().'::LANGUAGE_ID'), $this->language_id);
|
|
|
|
return $c;
|
|
}
|
|
|
|
public function setBackendPhrase($phrase)
|
|
{
|
|
$this->backendPhrase = $phrase;
|
|
}
|
|
|
|
public function getBackendPhrase()
|
|
{
|
|
return $this->backendPhrase;
|
|
}
|
|
|
|
public function getPeer()
|
|
{
|
|
return get_class($this).'Peer';
|
|
}
|
|
|
|
public function getPrimaryKeyFields($keyType = BasePeer::TYPE_FIELDNAME)
|
|
{
|
|
return array_map(function($field) use ($keyType) {
|
|
return call_user_func(array($this->getPeer(), 'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, $keyType);
|
|
}, array('catalogue', 'index', 'language_id'));
|
|
}
|
|
|
|
public function delete($con = null)
|
|
{
|
|
}
|
|
|
|
public function getAdminGeneratorTitle()
|
|
{
|
|
return $this->getBackendPhrase() . ' (' . $this->getCatalogue() . ')';
|
|
}
|
|
|
|
public function getPrimaryKey()
|
|
{
|
|
return array($this->catalogue, $this->index, $this->language_id);
|
|
}
|
|
|
|
public function setPrimaryKey($primaryKey)
|
|
{
|
|
list($this->catalogue, $this->index, $this->language_id) = $primaryKey;
|
|
}
|
|
|
|
public function isNew()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get wersja językowa
|
|
*
|
|
* @return Language
|
|
*/
|
|
public function getLanguage()
|
|
{
|
|
if (null === $this->language && $this->language_id)
|
|
{
|
|
$this->language = LanguagePeer::retrieveByPK($this->language_id);
|
|
}
|
|
|
|
return $this->language;
|
|
}
|
|
|
|
/**
|
|
* Set wersja językowa
|
|
*
|
|
* @param Language $language Wersja językowa
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setLanguage(Language $language)
|
|
{
|
|
$this->language = $language;
|
|
$this->language_id = $language->getId();
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get fraza
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getPhrase()
|
|
{
|
|
return $this->phrase;
|
|
}
|
|
|
|
/**
|
|
* Set fraza
|
|
*
|
|
* @param string $phrase Fraza
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setPhrase($phrase)
|
|
{
|
|
$this->phrase = $phrase;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get domyślne tłumaczenie dla frazy
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getDefaultPhraseTranslation()
|
|
{
|
|
return $this->defaultPhraseTranslation;
|
|
}
|
|
|
|
/**
|
|
* Set domyślne tłumaczenie dla frazy
|
|
*
|
|
* @param string $defaultPhraseTranslation Domyślne tłumaczenie dla frazy
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setDefaultPhraseTranslation($defaultPhraseTranslation)
|
|
{
|
|
$this->defaultPhraseTranslation = $defaultPhraseTranslation;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function hasUserPhraseTranslation()
|
|
{
|
|
return null !== $this->userPhraseTranslation && $this->userPhraseTranslation != $this->defaultPhraseTranslation;
|
|
}
|
|
|
|
/**
|
|
* Get tłumaczenie użytkownika dla frazy
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getUserPhraseTranslation()
|
|
{
|
|
return null !== $this->userPhraseTranslation ? $this->userPhraseTranslation : $this->defaultPhraseTranslation;
|
|
}
|
|
|
|
/**
|
|
* Set tłumaczenie użytkownika dla frazy
|
|
*
|
|
* @param string $userPhraseTranslation Tłumaczenie użytkownika dla frazy
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setUserPhraseTranslation($userPhraseTranslation)
|
|
{
|
|
if ($this->defaultPhraseTranslation != $userPhraseTranslation || $this->userPhraseTranslation != $userPhraseTranslation)
|
|
{
|
|
$this->modifiedColumns[LanguageTranslationPeer::USER_PHRASE_TRANSLATION] = true;
|
|
$this->userPhraseTranslation = $userPhraseTranslation;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get katalog tłumaczeń
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getCatalogue()
|
|
{
|
|
return $this->catalogue;
|
|
}
|
|
|
|
/**
|
|
* Set katalog tłumaczeń
|
|
*
|
|
* @param string $catalogue Katalog tłumaczeń
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setCatalogue($catalogue)
|
|
{
|
|
$this->catalogue = $catalogue;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get indeks w katalogu
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getIndex()
|
|
{
|
|
return $this->index;
|
|
}
|
|
|
|
/**
|
|
* Set indeks w katalogu
|
|
*
|
|
* @param string $index Indeks w katalogu
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setIndex($index)
|
|
{
|
|
$this->index = $index;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get id Wersji językowej
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getLanguageId()
|
|
{
|
|
return $this->language_id;
|
|
}
|
|
|
|
/**
|
|
* Set id Wersji językowej
|
|
*
|
|
* @param int $language_id Id Wersji językowej
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setLanguageId($language_id)
|
|
{
|
|
$this->language_id = $language_id;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function restore()
|
|
{
|
|
$this->setUserPhraseTranslation(null);
|
|
}
|
|
|
|
public function save($con = null)
|
|
{
|
|
if ($this->isColumnModified(LanguageTranslationPeer::USER_PHRASE_TRANSLATION))
|
|
{
|
|
$file = sfConfig::get('sf_root_dir') . '/apps/frontend/i18n/' . $this->catalogue . '.user.' . $this->getLanguage()->getLanguage() . '.xml';
|
|
|
|
$dom = new DOMDocument();
|
|
$dom->formatOutput = true;
|
|
$dom->preserveWhiteSpace = false;
|
|
|
|
if (is_file($file))
|
|
{
|
|
$dom->load($file);
|
|
}
|
|
else
|
|
{
|
|
$dom->loadXML($this->getXmlBody());
|
|
}
|
|
|
|
$bodies = $dom->getElementsByTagName('body');
|
|
|
|
$body = $bodies->item(0);
|
|
|
|
$transUnit = $this->findTransUnit($body);
|
|
|
|
if (null !== $transUnit)
|
|
{
|
|
if (null === $this->userPhraseTranslation)
|
|
{
|
|
$body->removeChild($transUnit);
|
|
}
|
|
else
|
|
{
|
|
$source = $transUnit->childNodes->item(0);
|
|
$source->nodeValue = null;
|
|
$source->appendChild($dom->createCDATASection($this->phrase));
|
|
$target = $transUnit->childNodes->item(1);
|
|
$target->nodeValue = null;
|
|
$target->appendChild($dom->createCDATASection($this->userPhraseTranslation));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$transUnit = $dom->createElement('trans-unit');
|
|
|
|
$attr = $dom->createAttribute('id');
|
|
$attr->value = $this->index;
|
|
$transUnit->appendChild($attr);
|
|
|
|
$source = $dom->createElement('source');
|
|
$source->appendChild($dom->createCDATASection($this->phrase));
|
|
$transUnit->appendChild($source);
|
|
|
|
$target = $dom->createElement('target');
|
|
$target->appendChild($dom->createCDATASection($this->userPhraseTranslation));
|
|
$transUnit->appendChild($target);
|
|
|
|
$body->appendChild($transUnit);
|
|
}
|
|
|
|
$result = $dom->save($file);
|
|
|
|
stLanguage::clearI18nCache($this->catalogue);
|
|
|
|
return $result;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function getXmlBody()
|
|
{
|
|
$date = date("Y-m-d\Th:i:s\Z");
|
|
$targetLanguage = $this->getLanguage()->getLanguage();
|
|
|
|
return <<<XML
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<xliff version="1.0">
|
|
<file orginal="global" source-language="pl_PL" target-language="{$targetLanguage}" datatype="plaintext" date="$date">
|
|
<body></body>
|
|
</file>
|
|
</xliff>
|
|
XML;
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @param DOMElement $domNode
|
|
* @return DOMElement
|
|
*/
|
|
protected function findTransUnit(DOMElement $domNode)
|
|
{
|
|
/**
|
|
* @var DOMElement $node
|
|
*/
|
|
foreach ($domNode->getElementsByTagName('trans-unit') as $node)
|
|
{
|
|
if ($node->getAttribute('id') == $this->index || $node->childNodes->item(0)->textContent == $this->phrase)
|
|
{
|
|
return $node;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|