我已經實現了CakePHP的翻譯行爲,並且都相當流暢,但我現在已經注意到我的i18n
表的翻譯數據不存在,當我假設該模型是contain()
待翻譯。CakePHP的翻譯行爲不適用於Containable
翻譯行爲不適用於包含的模型嗎?如果是這樣,是不是幾乎完全消除了這種行爲的任何有用性? (或者,也許這只是我 - 但我幾乎可以使用Containable)。
如果我打算使用Containable很多,是否有不同的「CakePHP方法」可以很容易地進行翻譯?
我已經實現了CakePHP的翻譯行爲,並且都相當流暢,但我現在已經注意到我的i18n
表的翻譯數據不存在,當我假設該模型是contain()
待翻譯。CakePHP的翻譯行爲不適用於Containable
翻譯行爲不適用於包含的模型嗎?如果是這樣,是不是幾乎完全消除了這種行爲的任何有用性? (或者,也許這只是我 - 但我幾乎可以使用Containable)。
如果我打算使用Containable很多,是否有不同的「CakePHP方法」可以很容易地進行翻譯?
我遇到類似的問題,看了幾十個來自谷歌的網頁,但無法找到一個簡單的解決我的問題。經過一些調試後,我創建了這個解決方法片段。請考慮到這是一個黑客攻擊。它主要是爲Croogo編寫的,因此相關模型將在網站上進行翻譯。但我瀏覽過翻譯行爲,它也應該爲它工作。基本上將它粘貼到你的AppModel類中。這是蛋糕2.x
// DIRTY LITTLE HACKS, FORCING TRANSLATE BEHAVIOR AFTERFIND CALLBACK
/**
* Hacking the afterFind so it will call the afterFind() from
* behavior
* Pase this in your AppModel Class
*
* @param array $results
* @param bool $primary
* @return array
*/
public function afterFind(array $results, $primary = false) {
parent::afterFind($results, $primary);
# calling only if not primary model, as they get translated pretty well
if (!$primary) {
# iterating behaviors to look for one that has something to do
# with translations (Translate for cake general behavior, CroogoTranslate for Croogo based apps)
foreach ($this->Behaviors->enabled() as $behavior) {
if (preg_match('/(.*)[T|t]ranslate(.*)/', $behavior)) {
# setting locale, not sure if it gets set on secondary models
$this->locale = Configure::read('Config.language');
# hacking the result set to match behaviours requirments
# so basically creating the result set to look like called from originated model
# $k => array('ModelAlias' => array $results)
$results_tmp = array(
0 => array(
$this->alias => $results,
)
);
# if we find such behavior we force it's afterFind with prepared data
$results = $this->Behaviors->{$behavior}->afterFind($this, $results_tmp, true); # forcing true on primary - CroogoTranslate requires that
# restoring orginal structure like nothing ever happened
$results = $results[0][$this->alias];
# not sure if should break or not ?
# on one hand what's the point of having multiple translate behaviors in one app ?
# on the other i've seen more weird stuff that multiple translate behaviors
break;
}
}
}
return $results;
}
顯然這是一個常見問題。 CakePHP的食譜有關於如何處理它的一些提示:
http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
它在哪裏「提示如何處理它」?除了提到我需要使用fields()數組,這似乎沒有任何影響。 – Dave
好吧,我發現了另一個關於這個問題的長時間的討論,關於如何解決這個問題的更多建議:[link](http://cakephp.lighthouseapp.com/projects/42648/tickets/95-afterfind-and -beforefind-callbacks-not-working-on-associated-models-was-translate-behavior-should-translate-associated-model-data) –
我目前沒有能力測試這個,但已經標記爲答案希望它能夠幫助其他人找到一種管理方式。謝謝。 – Dave