Trait TranslateStrategyTrait
Contains common code needed by TranslateBehavior strategy classes.
Property Summary
-
$_config protected
array<string, mixed>Runtime config
-
$_configInitialized protected
boolWhether the config property has already been configured with defaults
-
$locale protected
string|nullThe locale name that will be used to override fields in the bound table from the translations table
-
$table protected
Cake\ORM\TableTable instance
-
$translationTable protected
Cake\ORM\TableInstance of Table responsible for translating
Method Summary
-
_configDelete() protected
Deletes a single config key.
-
_configRead() protected
Reads a config key.
-
_configWrite() protected
Writes a config key.
-
afterSave() public
Unsets the temporary
_i18nproperty after the entity has been saved -
buildMarshalMap() public
Build a set of properties that should be included in the marshaling process.
-
configShallow() public
Merge provided config with existing config. Unlike
config()which does a recursive merge for nested keys, this method does a simple merge. -
deleteConfig() public
Deletes a config key.
-
getConfig() public
Returns the config.
-
getConfigOrFail() public
Returns the config for this specific key.
-
getLocale() public
Returns the current locale.
-
getTranslationTable() public
Return translation table instance.
-
setConfig() public
Sets the config.
-
setLocale() public
Sets the locale to be used.
-
unsetEmptyFields() protected
Unset empty translations to avoid persistence.
Method Detail
_configDelete() ¶ protected
_configDelete(string $key): void
Deletes a single config key.
Parameters
-
string$key Key to delete.
Returns
voidThrows
Cake\Core\Exception\CakeExceptionif attempting to clobber existing config
_configRead() ¶ protected
_configRead(string|null $key): $key is null ? array : mixed
Reads a config key.
Parameters
-
string|null$key Key to read.
Returns
$key is null ? array : mixed_configWrite() ¶ protected
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
Parameters
-
array<string, mixed>|string$key Key to write to.
-
mixed$value Value to write.
-
string|bool$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Returns
voidThrows
Cake\Core\Exception\CakeExceptionif attempting to clobber existing config
afterSave() ¶ public
afterSave(Cake\Event\EventInterface<Cake\ORM\Table> $event, Cake\Datasource\EntityInterface $entity): void
Unsets the temporary _i18n property after the entity has been saved
Parameters
-
Cake\Event\EventInterface<Cake\ORM\Table>$event The beforeSave event that was fired
-
Cake\Datasource\EntityInterface$entity The entity that is going to be saved
Returns
voidbuildMarshalMap() ¶ public
buildMarshalMap(Cake\ORM\Marshaller<Cake\Datasource\EntityInterface> $marshaller, array<string, callable> $map, array<string, mixed> $options): array<string, callable>
Build a set of properties that should be included in the marshaling process.
Add in _translations marshaling handlers. You can disable marshaling
of translations by setting 'translations' => false in the options
provided to Table::newEntity() or Table::patchEntity().
Parameters
-
Cake\ORM\Marshaller<Cake\Datasource\EntityInterface>$marshaller The marshaler of the table the behavior is attached to.
-
array<string, callable>$map The property map being built.
-
array<string, mixed>$options The options array used in the marshaling call.
Returns
array<string, callable>A map of [property => callable] of additional properties to marshal.
configShallow() ¶ public
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config() which does
a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
-
array<string, mixed>|string$key The key to set, or a complete array of configs.
-
mixed|null$value optional The value to set.
Returns
$thisdeleteConfig() ¶ public
deleteConfig(string $key): $this
Deletes a config key.
Parameters
-
string$key Key to delete. It can be a dot separated string to delete nested keys.
Returns
$thisgetConfig() ¶ public
getConfig(string|null $key = null, mixed $default = null): $key is null ? array : mixed
Returns the config.
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
-
string|null$key optional The key to get or null for the whole config.
-
mixed$default optional The return value when the key does not exist.
Returns
$key is null ? array : mixedConfiguration data at the named key or null if the key does not exist.
getConfigOrFail() ¶ public
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
Parameters
-
string$key The key to get.
Returns
mixedConfiguration data at the named key
Throws
InvalidArgumentExceptiongetLocale() ¶ public
getLocale(): string
Returns the current locale.
If no locale has been explicitly set via setLocale(), this method will return
the currently configured global locale excluding any options set after @.
Returns
stringSee Also
\Cake\ORM\Behavior\TranslateBehavior::setLocale()
getTranslationTable() ¶ public
getTranslationTable(): Cake\ORM\Table
Return translation table instance.
Returns
Cake\ORM\TablesetConfig() ¶ public
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
-
array<string, mixed>|string$key The key to set, or a complete array of configs.
-
mixed|null$value optional The value to set.
-
bool$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
Returns
$thisThrows
Cake\Core\Exception\CakeExceptionWhen trying to set a key that is invalid.
setLocale() ¶ public
setLocale(string|null $locale): $this
Sets the locale to be used.
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
-
string|null$locale The locale to use for fetching and saving records. Pass
nullin order to unset the current locale, and to make the behavior falls back to using the globally configured locale.
Returns
$thisunsetEmptyFields() ¶ protected
unsetEmptyFields(Cake\Datasource\EntityInterface $entity): void
Unset empty translations to avoid persistence.
Should only be called if $this->_config['allowEmptyTranslations'] is false.
Parameters
-
Cake\Datasource\EntityInterface$entity The entity to check for empty translations fields inside.
Returns
void