https://github.com/softspring/doctrine-simple-translation-type-bundle
This package provides a simple translation type for doctrine using JSON type.
https://github.com/softspring/doctrine-simple-translation-type-bundle
component doctrine symfony-bundle
Last synced: about 1 year ago
JSON representation
This package provides a simple translation type for doctrine using JSON type.
- Host: GitHub
- URL: https://github.com/softspring/doctrine-simple-translation-type-bundle
- Owner: softspring
- License: mit
- Created: 2019-10-24T22:49:54.000Z (over 6 years ago)
- Default Branch: 5.3
- Last Pushed: 2025-03-24T13:11:13.000Z (over 1 year ago)
- Last Synced: 2025-03-28T08:03:04.356Z (about 1 year ago)
- Topics: component, doctrine, symfony-bundle
- Language: PHP
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Doctrine simple translation type bundle





[](https://github.com/softspring/doctrine-simple-translation-type-bundle/actions/workflows/ci.yml)
This package provides a simple translation type for Doctrine, and its Symfony integration bundle.
## Installation
### Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
```console
$ composer require softspring/doctrine-simple-translation-type-bundle
```
## Configure
Configure the Doctrine type:
# config/packages/doctrine.yaml
doctrine:
dbal:
types:
simple_translation: 'Softspring\DoctrineSimpleTranslationTypeBundle\Doctrine\Type\SimpleTranslationType'
## Usage
### Configure entity that uses the type
use Doctrine\ORM\Mapping as ORM;
/**
* @var SimpleTranslation
* @ORM\Column(name="translated_name", type="simple_translation", nullable=false)
*/
protected $translatedName;
public function __construct()
{
$this->translatedName = new SimpleTranslation();
}
/**
* @return SimpleTranslation
*/
public function getName(): SimpleTranslation
{
return $this->translatedName;
}
/**
* @param SimpleTranslation $translatedName
*/
public function setName(SimpleTranslation $translatedName): void
{
$this->translatedName = $translatedName;
}
### Manage the model
The model class is *Softspring\DoctrineSimpleTranslationTypeBundle\Model\SimpleTranslation*.
**Set the default translation**
$entity->getName()->setDefaultLocale('es');
$entity->getName()->setTranslation(null, 'Nombre de la entidad'); // null means default locale
$entity->getName()->setTranslation('es', 'Nombre de la entidad'); // it's also posible to specify the locale
**Add additional translations**
$entity->getName()->setTranslation('en', 'Entity name');
**Get the value**
$entity->getName()->translate(); // returns the default value 'Nombre de la entidad'
$entity->getName()->translate('es'); // returns 'Nombre de la entidad'
$entity->getName()->translate('en'); // returns 'Entity name'
**Use the full methods**
$entity->getName()->getTranslations(); // returns ['es'=>'Nombre de la entidad', 'en'=>'Entity name']
$entity->getName()->setTranslations(['es'=>'Nombre de la entidad', 'en'=>'Entity name']);
**Use it as array**
The model implements ArrayAccess, so it's possible to use it as an array:
$entity->getName()['en']; // returns 'Entity name'
$entity->getName()['es']; // returns 'Nombre de la entidad'
### Twig usage
{{ entity.name|translate }} {# returns 'Nombre de la entidad' if app.request.locale is 'es' #}
{{ entity.name|translate('es') }} {# returns 'Nombre de la entidad' #}
{{ entity.name|translate('en') }} {# returns 'Entity name' #}
### Edit values in forms
You can use the *Softspring\DoctrineSimpleTranslationTypeBundle\Form\SimpleTranslationType*
use Symfony\Component\Form\FormBuilderInterface;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('translatedName'); // automatically uses the SimpleTranslationType thanks to the TypeGuesser
}
**Force languages**
$builder->add('translatedName', SimpleTranslationType::class, [
'languages' => ['es','en','de'],
]);