cakephp - How to prevent showing/receiving untranslated content? -
this distance cell
public function display() { $this->loadmodel('distances'); $distances = $this->distances->find('all',[ 'order' => 'distances.id asc', ])->toarray(); $this->set('distances',$distances); }}
problem, if content not yet translated , stored in db, original untranslated content displayed on page.
how prevent this, , show translated content in current language?
the onlytranslated
option
this unfortunately not documented yet, translate
behavior supports onlytranslated
option, cause records found translation in current locale exists.
so simle enabling option, either in configuration when loading behavior:
$this->addbehavior('translate', [ 'onlytranslated' => true, // ... ]);
or on fly:
$this->distances->behaviors()->get('translate')->config('onlytranslated', true);
however, work when current locale not default locale. ie when you've switched locale in order view content in different language, in cases want , need!
a custom query
in cases want retrieve records translation exists, irrespectively of current locale, or locale of translations, custom query inner
join on translation table option.
this should pretty simle using query::innerjoinwith()
. here's basic example should rather self-explantory:
$translatebehavior = $this->distances->behaviors()->get('translate'); $translationtable = $translatebehavior->config('translationtable'); $distances = $this->distances ->find() ->innerjoinwith($translationtable) ->order('distances.id asc') ->toarray();
Comments
Post a Comment