I need to convert a part of a commercial script to use a translation mechanism. As it was written with cakephp 1.2.6 I decided to use the Translate behavior.
I made the i18n table filled up with the old content for two languages hungarian/hun and english/eng (insert/select). So now I have the same old content for both langs. The owner will translate them. I dropped the translated columns from the model table and made a controller function and view.
Editing is fine, I can edit both data and they are saved to the i18n table perfectly. Now I need to extend the languages to have german translation also. Cake informs me that saving was succesful but it is not in the db. Where is the problem?
Controller function:
function admin_translate($id=null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid id for News', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
foreach($this->data as $langdata){
$this->News->locale=$langdata['locale'];
$langdata['News']['id']=$this->data['News']['id'];
if($this->News->save($langdata)) {
$this->Session->setFlash(__('Translation successfully saved.', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('Cannot save translation. Please try again.', true));
$this->redirect(array('action'=>'index'));
}
}
}
if (empty($this->data)) {
$this->data = $this->News->read(null, $id);
$this->loadModel('Language');
$this->set('languages',$this->Language->find('list',array('fields'=>array('language'),'recursive'=>0)));
}
}
View:
<?php
$html->addCrumb(__('Manage Content', true), '/admin/pages');
$html->addCrumb(__('News', true), '/admin/news');
$html->addCrumb(__('Translate', true), '/admin/'.$this->params['controller'].'/translate/'.$this->data['News']['id']);
echo $this->element('admin/crumb');
?>
<div class="news form">
<div id="current_language_data">
<h3><?php echo __('Current language: ',true).$appConfigurations['language']; ?></h3>
<ul>
<li><span class="label"><?php __('Title: '); ?></span><?php echo $this->data['News']['title']; ?></li>
<li><span class="label"><?php __('Brief: '); ?></span><?php echo $this->data['News']['brief']; ?></li>
<li><span class="label"><?php __('Content: '); ?></span><?php echo $this->data['News']['content']; ?></li>
<li><span class="label"><?php __('Meta description: '); ?></span><?php echo $this->data['News']['meta_description']; ?></li>
<li><span class="label"><?php __('Meta keywords: '); ?></span><?php echo $this->data['News']['meta_keywords']; ?></li>
</ul>
</div>
<?php
echo $form->create('News',array('action'=>'translate'));?>
<fieldset>
<legend><?php __('Translate this News Article');?></legend>
<?php
$data=array();
foreach($this->data as $key=>$field) {
if($key<>'News') {
foreach($field as $fielddata) {
$data[$fielddata['locale']][$fielddata['field']] = $fielddata['content'];
}
}
}
foreach($languages as $lang) {
if($lang<>$appConfigurations['language']){
echo '<h3>'.__('Language: ',true).$lang.'</h3>';
echo $form->input($lang.'.locale.',array('type'=>'hidden','value'=>$lang));
echo $form->input($lang.'.News.title', array('label' => __('Title *',true),'value'=>$data[$lang]['title']));
echo $form->input($lang.'.News.brief', array('label' => __('Brief *',true),'value'=>$data[$lang]['brief']));
echo $form->input($lang.'.News.content', array('label' => __('Content *',true),'type'=>'textarea','rows'=>'5','cols'=>'40','value'=>$data[$lang]['content']));
echo $form->input($lang.'.News.meta_description',array('label' => __('Meta description',true),'value'=>$data[$lang]['meta_description']));
echo $form->input($lang.'.News.meta_keywords',array('label' => __('Meta keywords',true),'value'=>$data[$lang]['meta_keywords']));
}
}
echo $form->input('id');
?>
</fieldset>
<?php echo $form->end(__('Save Article', true));?>
</div>
<div>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('<< Back to news articles', true), array('action' => 'index'));?></li>
</ul>
</div>
I store the 3 letter code of the used languages in model languages, currently only this 3 is in it: hun, eng, ger