Download the PHP package sandstorm/gedmotranslatableconnector without Composer
On this page you can find all versions of the php package sandstorm/gedmotranslatableconnector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sandstorm/gedmotranslatableconnector
More information about sandstorm/gedmotranslatableconnector
Files in sandstorm/gedmotranslatableconnector
Package gedmotranslatableconnector
Short Description Connector to make Gedmo Translatable work with Neos Flow
License
Informations about the package gedmotranslatableconnector
Flow Framework Connector to Gedmo Translatable
by Sebastian Kurfürst, sandstorm|media. Thanks to Web Essentials for sponsoring this work initially. Currently maintained by @swisscomeventandmedia
Using Gedmo.Translatable in the Neos Flow framework proved a little harder than originally anticipated. This small package wraps up the necessary steps.
Getting started
Just include this package, and then use Gedmo Translatable as explained in their documentation (e.g. using the @Gedmo\Translatable annotation): https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/translatable.md
Make sure to clear the code cache completely in Data/Temporary after installing this package!
Furthermore, make sure to create a doctrine migration which creates the ext_translations SQL table; e.g. run ./flow doctrine:migrationgenerate
Check out the example package at https://github.com/sandstorm/GedmoTest.
This connector supports all the advanced options provided by Gedmo Translatable.
Model Annotations
Just annotate your model properties which shall be localized with Gedmo\Mapping\Annotation\Translatable
.
Translating a model (low-level)
Set current language
In order to set the current language for viewing, inject the Gedmo\Translatable\TranslatableListener
class and set
the current language on it: $translatableListener->setTranslatableLocale('de');
.
Translation management
Editing multiple languages at the same time
-
Mix-in the Trait
Sandstorm\GedmoTranslatableConnector\TranslatableTrait
and implement\Sandstorm\GedmoTranslatableConnector\Translatable
into your model, e.g. by doing: -
This trait adds a
getTranslations()
andsetTranslations()
method, allowing to get and set other translations of a model. - Now, you can easily edit multiple languages by binding the form element to
translations.[language].[fieldname]
, e.g. this works like the following:
Persist edited translations
With the by default enabled instantTranslation
setting, the translations are updated and persisted through the Gedmo\Translatable\Entity\Repository\TranslationRepository
immediately when calling the setTranslation
method.
Often, this might not be ideal because it persists the entity right away. Disable the setting and call the flush()
method of the TranslatableManager
to persist the changes according to your needs.
Fetching an object in another locale
If you have loaded an object in a specific locale, but later on need to change the object to be in another locale,
the method reloadInLocale($locale)
(which is defined inside the trait Sandstorm\GedmoTranslatableConnector\TranslatableTrait
)
can be called:
Translating associations
Warning: this feature is not yet 100% stable; please test it and give feedback!
Normally, associations towards other domain models such as images or assets are not translation-aware; but Translatable only works for simple properties.
The TranslatableConnector however contains some functionality to make translation of associations work; by using a little workaround: We store the identifier of the target object in the domain model, and manually load/store from this identifier.
This works as follows:
-
Makes sure you have the
TranslatableTrait
added to your domain class -
e.g. to make an
Asset
reference translatable, create a new propertyassetIdentifer
which is a string and will contain the asset identifier. This property should be marked asGedmo\Translatable
. -
Then, you need to configure the
translationAssociationMapping
, which tells the system that the (virtual) property shouldasset
should internally be stored asassetIdentifier
. - Furthermore, create
assetOnSave
andassetOnLoad
methods as outlined below, which convert the different representations
See the full example below:
Inner Workings
(as a further reference -- could also be reduced if we change Flow Framework a little on the relevant parts)
-
Settings.yaml: Ignore Gedmo namespace from Reflection, adds the Translatable Listener as Doctrine Event listener. This part is pretty standard for using other Gedmo Doctrine extensions as well.
-
Objects.yaml: Mark the
TranslatableListener
as singleton, such that you can inject it into your classes and set the current language. This part was straightforward as well. - Package.php: Make the entities of Gedmo Translatable known to Doctrine and in Reflection. This was quite tricky to archive, see the inline docs in the class how this was done.
Further recommendation
- Use ORM query hints when working with Gedmo Translatable to speed up queries.
- Use translation entities if you have large datasets with many translations.