This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,206 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Translation\Model;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @author Gonzalo Vilaseca <gvilaseca@reiss.co.uk>
*/
abstract class AbstractTranslatable implements TranslatableInterface
{
/**
* Translations
*
* @var TranslationInterface[]
*/
protected $translations;
/**
* Current locale
*
* @var string
*/
protected $currentLocale;
/**
* Cache current translation. Useful in Doctrine 2.4+
*
* @var string
*/
protected $currentTranslation;
/**
* Fallback locale
*
* @var string
*/
protected $fallbackLocale;
/**
* Constructor.
*/
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* {@inheritdoc}
*/
public function getTranslations()
{
return $this->translations;
}
/**
* {@inheritdoc}
*/
public function addTranslation(TranslationInterface $translation)
{
if (!$this->translations->containsKey($translation->getLocale())) {
$this->translations->set($translation->getLocale(), $translation);
$translation->setTranslatable($this);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function removeTranslation(TranslationInterface $translation)
{
if ($this->translations->removeElement($translation)) {
$translation->setTranslatable(null);
}
return $this;
}
/**
* @param TranslationInterface $translation
*
* @return bool
*/
public function hasTranslation(TranslationInterface $translation)
{
return $this->translations->containsKey($translation->getLocale());
}
/**
* @param string $currentLocale
*
* @return $this
*/
public function setCurrentLocale($currentLocale)
{
$this->currentLocale = $currentLocale;
return $this;
}
/**
* @return string
*/
public function getCurrentLocale()
{
return $this->currentLocale;
}
/**
* @param TranslationInterface $currentTranslation
*
* @return $this
*/
public function setCurrentTranslation(TranslationInterface $currentTranslation)
{
$this->currentTranslation = $currentTranslation;
return $this;
}
/**
* @return TranslationInterface
*/
public function getCurrentTranslation()
{
return $this->currentTranslation;
}
/**
* @param string $fallbackLocale
*
* @return $this
*/
public function setFallbackLocale($fallbackLocale)
{
$this->fallbackLocale = $fallbackLocale;
return $this;
}
/**
* @return string
*/
public function getFallbackLocale()
{
return $this->fallbackLocale;
}
/**
* Translation helper method
*
* @param string $locale
*
* @return TranslationInterface
*
* @throws \RuntimeException
*/
public function translate($locale = null)
{
if (null === $locale) {
$locale = $this->currentLocale;
}
if (!$locale) {
throw new \RuntimeException('No locale has been set and currentLocale is empty');
}
if ($this->currentTranslation && $this->currentTranslation->getLocale() === $locale) {
return $this->currentTranslation;
}
// TODO Throw exception? Get default translation?
if (!$translation = $this->translations->get($locale)) {
if (!$fallbackTranslation = $this->translations->get($this->getFallbackLocale())) {
$className = $this->getTranslationEntityClass();
$translation = new $className();
$translation->setLocale($locale);
$this->addTranslation($translation);
} else {
$translation = clone $fallbackTranslation;
}
}
$this->currentTranslation = $translation;
return $translation;
}
/**
* Return translation entity class
*
* @return string
*/
abstract protected function getTranslationEntityClass();
}

View File

@@ -0,0 +1,82 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Translation\Model;
/**
* @author Gonzalo Vilaseca <gvilaseca@reiss.co.uk>
*/
class AbstractTranslation implements TranslationInterface
{
/**
* Locale
*
* @var string
*/
protected $locale;
/**
* Translatable object
*
* @var TranslatableInterface
*/
protected $translatable;
/**
* {@inheritdoc}
*/
public function getTranslatable()
{
return $this->translatable;
}
/**
* {@inheritdoc}
*/
public function setTranslatable(TranslatableInterface $translatable = null)
{
if ($translatable === $this->translatable) {
return $this;
}
$old = $this->translatable;
$this->translatable = $translatable;
if (null !== $old) {
$old->removeTranslation($this);
}
if (null !== $translatable) {
$translatable->addTranslation($this);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getLocale()
{
return $this->locale;
}
/**
* {@inheritdoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Translation\Model;
/**
* @author Gonzalo Vilaseca <gvilaseca@reiss.co.uk>
*/
interface TranslatableInterface
{
/**
* Get all translations
*
* @return TranslationInterface[]
*/
public function getTranslations();
/**
* Add a new translation
*
* @param TranslationInterface $translation
* @return self
*/
public function addTranslation(TranslationInterface $translation);
/**
* Remove a translation
*
* @param TranslationInterface $translation
* @return $this
*/
public function removeTranslation(TranslationInterface $translation);
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Translation\Model;
/**
* @author Gonzalo Vilaseca <gvilaseca@reiss.co.uk>
*/
interface TranslationInterface
{
/**
* Get the translatable object
*
* @return TranslatableInterface
*/
public function getTranslatable();
/**
* Set the translatable object
*
* @param TranslatableInterface $translatable
* @return self
*/
public function setTranslatable(TranslatableInterface $translatable = null);
/**
* Get the locale
*
* @return string
*/
public function getLocale();
/**
* Set the locale
*
* @param string $locale
* @return $this
*/
public function setLocale($locale);
}