TranslateStrategyTrait
Contains common code needed by TranslateBehavior strategy classes.
Table of Contents
Properties
- $locale : string|null
- The locale name that will be used to override fields in the bound table from the translations table
- $table : Table
- Table instance
- $translationTable : Table
- Instance of Table responsible for translating
Methods
- afterSave() : void
- Unsets the temporary `_i18n` property after the entity has been saved
- buildMarshalMap() : array<string|int, mixed>
- Build a set of properties that should be included in the marshalling process.
- getLocale() : string
- Returns the current locale.
- getTranslationTable() : Table
- Return translation table instance.
- setLocale() : $this
- Sets the locale to be used.
- unsetEmptyFields() : void
- Unset empty translations to avoid persistence.
Properties
$locale
The locale name that will be used to override fields in the bound table from the translations table
protected
string|null
$locale
$table
Table instance
protected
Table
$table
$translationTable
Instance of Table responsible for translating
protected
Table
$translationTable
Methods
afterSave()
Unsets the temporary `_i18n` property after the entity has been saved
public
afterSave(EventInterface $event, EntityInterface $entity) : void
Parameters
- $event : EventInterface
-
The beforeSave event that was fired
- $entity : EntityInterface
-
The entity that is going to be saved
buildMarshalMap()
Build a set of properties that should be included in the marshalling process.
public
buildMarshalMap(Marshaller $marshaller, array<string|int, mixed> $map, array<string, mixed> $options) : array<string|int, mixed>
Add in _translations
marshalling handlers. You can disable marshalling
of translations by setting 'translations' => false
in the options
provided to Table::newEntity()
or Table::patchEntity()
.
Parameters
- $marshaller : Marshaller
-
The marhshaller of the table the behavior is attached to.
- $map : array<string|int, mixed>
-
The property map being built.
- $options : array<string, mixed>
-
The options array used in the marshalling call.
Return values
array<string|int, mixed> —A map of [property => callable]
of additional properties to marshal.
getLocale()
Returns the current locale.
public
getLocale() : string
If no locale has been explicitly set via setLocale()
, this method will return
the currently configured global locale.
Tags
Return values
stringgetTranslationTable()
Return translation table instance.
public
getTranslationTable() : Table
Return values
TablesetLocale()
Sets the locale to be used.
public
setLocale(string|null $locale) : $this
When fetching records, the content for the locale set via this method, and likewise when saving data, it will save the data in that locale.
Note that in case an entity has a _locale
property set, that locale
will win over the locale set via this method (and over the globally
configured one for that matter)!
Parameters
- $locale : string|null
-
The locale to use for fetching and saving records. Pass
null
in order to unset the current locale, and to make the behavior fall back to using the globally configured locale.
Return values
$thisunsetEmptyFields()
Unset empty translations to avoid persistence.
protected
unsetEmptyFields(EntityInterface $entity) : void
Should only be called if $this->_config['allowEmptyTranslations'] is false.
Parameters
- $entity : EntityInterface
-
The entity to check for empty translations fields inside.