sonofwinter / translation-bundle
This Bundle provides a translator from Entity
Installs: 792
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.0
- doctrine/annotations: ^2.0
- symfony/dependency-injection: ^6.0
- symfony/framework-bundle: ^6.0
- symfony/orm-pack: *
- symfony/yaml: ^6.0
Requires (Dev)
- phpunit/phpunit: ^9
- symfony/phpunit-bridge: ^6.0
This package is not auto-updated.
Last update: 2025-03-19 20:20:02 UTC
README
Installation
Open a command console, enter your project directory and execute:
$ composer require sonofwinter/translation-bundle
Configuration
You can override sow_translation.available_locales parameter to a new list for set your available lang list default is [ 'en', 'fr', 'es', 'de', 'it' ]
By default a Translation entity class exists but you can create your translation entity class who extends AbstractTranslation To use it, set the sow_translation.translation_class_name parameter to
<parameter key="sow_translation.translation_class_name">App\Entity\YourTranslationClass</parameter>
Usage
Your translated entities must implements Translatable interface Then define translated properties in your entity
/** * @var string * @Translate(key="firstname") */ private $firstname; /** * @var string * @Translate(key="lastname", setter="setOtherName") */ private $lastname;
You can defined the key property for matching another name, if it's not, the property name is taken by default. The setter property is used if you want to use another setter. A TranslatableConfigurationException is throws if the setter doens't exist.
New n V0.8
This bundle now require php >= 8.0
you can use attribute instead of annotation
use SOW\TranslationBundle\Attribute\Translation; class MyClasse { #[Translation(key: "firstname")] private string $firstname = ''; #[Translation(key: "lastname", setter: "setOtherName")] private string $lastname = ''; }
By default, the bundle use annotation method, you have to change configuration to use attributes
sow_translation.translation_method: attribute
If you want to override attribute class, don't forget to define it in configuration
sow_translation.attribute_class_name: SOW\TranslationBundle\Attribute\Translation
Translate
You can use some methods for translate an entity :
- translate(Translatable $entity, string $lang) to translate the entity in $lang
- translateForLangs(Translatable $entity, array $langs) to translate the entity in multiple languages
Set translations
These methods is use for set translations :
- setTranslationForLangAndValue(Translatable $translatable, string $lang, string $key, string $value) to set a single translation
- setTranslationForLangAndValues(Translatable $translatable, string $lang, array $values) for set multiple values in one lang
- setTranslations(Translatable $translatable, array $translations) for set multiple translation for multiple languages
Remove translations
These methods is use for remove translations :
- removeByObjectKeyAndLang(Translatable $object, string $key, string $lang) remove a specific translation
- removeAllForTranslatable(Translatable $object) remove all translation for object
- removeAllByKey(string $key) remove all translation for property