如何防止显示/接收未翻译的内容? [英] How to prevent showing/receiving untranslated content?
问题描述
这是我的距离单元
public function display()
{
$this->loadModel('Distances');
$distances = $this->Distances->find('all',[
'order' => 'Distances.id ASC',
])->toArray();
$this->set('distances',$distances);
}}
问题是,如果内容尚未翻译并存储在db中,则原始未翻译
Problem, if content is not yet translated and stored in db, original untranslated content is displayed on the page.
如何防止这种情况,并仅显示目前语言的翻译内容?
How to prevent this, and show only translated content in current language?
推荐答案
onlyTranslated
选项
Translate
行为支持 onlyTranslated
选项,这将导致只找到那些在当前
The onlyTranslated
option
This is unfortunately not documented yet, but the Translate
behavior supports a onlyTranslated
option, which will cause only those records to be found for which a translation in the current locale exists.
因此,它可能像启用该选项一样简单,在配置中加载行为:
So it could be as simle as enabling that option, either in the configuration when loading the behavior:
$this->addBehavior('Translate', [
'onlyTranslated' => true,
// ...
]);
或即时:
$this->Distances->behaviors()->get('Translate')->config('onlyTranslated', true);
但是,这只会在当前语言环境不是默认语言环境时工作。即,当您切换区域设置以查看不同语言的内容时,在大多数情况下,这正是您想要和需要的!
However, this will only work when the current locale is not the default locale. ie when you've switched the locale in order to view your content in a different language, in most cases however this is exactly what you want and need!
如果您只想检索存在翻译的记录,而不考虑当前语言环境或翻译的语言环境,则可以使用自定义查询 INNER
加入翻译表将是一个选项。
In cases where you want to retrieve only those records for which a translation exists, irrespectively of the current locale, or the locale of the translations, then a custom query with an INNER
join on the translation table would be an option.
这应该是很漂亮的使用 Query :: innerJoinWith()
。这里有一个基本的例子,应该是自我解释的:
This should be pretty simle using Query::innerJoinWith()
. Here's a basic example which should be rather self-explantory:
$TranslateBehavior = $this->Distances->behaviors()->get('Translate');
$translationTable = $TranslateBehavior->config('translationTable');
$distances = $this->Distances
->find()
->innerJoinWith($translationTable)
->order('Distances.id ASC')
->toArray();
另请参阅
- Cookbook>数据库访问& ORM>检索数据&结果集>使用innerJoinWith
- Cookbook>数据库访问& ORM>行为>翻译>使用单独的翻译表
- API> \Cake\ORM\Behavior\TranslateBehavior :: locale()
- API > \Cake\ORM\Behavior\TranslateBehavior :: $ _ defaultConfig()
- Cookbook > Database Access & ORM > Retrieving Data & Results Sets > Using innerJoinWith
- Cookbook > Database Access & ORM > Behaviors > Translate > Using a Separate Translations Table
- API > \Cake\ORM\Behavior\TranslateBehavior::locale()
- API > \Cake\ORM\Behavior\TranslateBehavior::$_defaultConfig()
See also
这篇关于如何防止显示/接收未翻译的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!