Reputation: 73
I'm tried to create some translate behaviour on a model. But how do i delete a specific translation.
My example is a Category which has a name that should be translated. Lets say i have a translation in english and spanish. How do i delete the spanish translationsh? offcouse i could create my own sql query but is there a cakephp way to do that.
Upvotes: 3
Views: 558
Reputation: 77
Suprisingly there is no such native CakePHP method to delete any translations for a given entity. @AD7six is obviously right, but let me give you a more "by-the-book" approach.
You could do sth like this:
<a href="<?= $this->Url->build(['action' => 'deleteTranslation', 'foreign_key' => 123, 'model' => 'Pluginname.Controller', 'locale' => 'de_DE']); ?>"><?= __('Remove translation'); ?></a>
and then create a method that utilizes that functionality:
//put this into src/AppController.php to enable that functionality across the whole application
public function deleteTranslation() {
$this->loadModel('I18n');
if($this->I18n->deleteAll([
'locale' => $this->request->getQuery('locale'),
'model' => $this->request->getQuery('model'),
'foreign_key' => $this->request->getQuery('foreign_key')
])) {
$this->Flash->success(__('Successfully deleted a translation.'));
}
else {
$this->Flash->error(__('Could not delete a translation.'));
}
return $this->redirect($this->referer());
}
Upvotes: 1
Reputation: 66299
However, it's easy enough to achieve
To delete all translations for a given locale (As with any destructive action - take a db backup before doing anything so you can recover if there's a mistake or it turns out something unexpected is deleted):
DELETE FROM i18n WHERE locale = 'esp';
Of course, the locale depends on your specific application - you can check your config, or just ask the db:
SELECT DISTINCT(locale) FROM i18n;
Just add a model condition:
DELETE FROM i18n
WHERE
locale = 'esp' AND
model = 'Category'
;
You'll need to know the id of the record you want to operate, then:
DELETE FROM i18n
WHERE
locale = 'esp' AND
model = 'Category' AND
foreign_key = 2468789 AND
field = 'name'
;
If you're not confident directly running queries on the db, run a select first to see what you're going to delete:
SELECT * FROM i18n
WHERE
locale = 'esp' AND
model = 'Category' AND
foreign_key = 2468789 AND
field = 'name'
;
I.e. find what you want, and then simply change the SELECT
to a DELETE
statement.
Upvotes: 2