softspring / doctrine-simple-translation-type-bundle
This package provides a simple translation type for doctrine using JSON type.
Installs: 7 546
Dependents: 0
Suggesters: 1
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 2
Type:symfony-bundle
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2.5
- doctrine/orm: ^2.10 | ^3.0
- symfony/form: ^5.4|^6.0|^7.0
- symfony/http-kernel: ^5.4|^6.0|^7.0
- twig/twig: ^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.58.*
- phpstan/phpstan: ^1.10
- rector/rector: ^1.0
- 5.3.x-dev
- 5.2.x-dev
- v5.2.7
- v5.2.6
- v5.2.5
- v5.2.4
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.2.0-rc10
- v5.2.0-rc9
- v5.2.0-rc8
- v5.2.0-rc7
- v5.2.0-rc6
- v5.2.0-rc5
- v5.2.0-rc4
- v5.2.0-rc1
- 5.1.x-dev
- v5.1.25
- v5.1.24
- v5.1.23
- v5.1.22
- v5.1.21
- v5.1.20
- v5.1.19
- v5.1.18
- v5.1.17
- v5.1.16
- v5.1.15
- v5.1.14
- v5.1.13
- v5.1.12
- v5.1.11
- v5.1.10
- v5.1.9
- v5.1.8
- v5.1.7
- v5.1.6
- v5.1.5
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0-rc1
- v5.0.0-beta1
- v4.1.1
- 4.1.0
- 4.0.0
- v1.0.0
- dev-dependabot/composer/friendsofphp/php-cs-fixer-3.64.star
- dev-dependabot/composer/doctrine/orm-tw-3.1
- dev-detached
- dev-some-front-improvements
This package is auto-updated.
Last update: 2024-09-11 18:01:57 UTC
README
This package provides a simple translation type for Doctrine, and its Symfony integration bundle.
This bundle is under development, more features will be added soon, and existing ones may change.
Installation
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
$ 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'],
]);