On Jul 11, 8:14 am, zwobot <[EMAIL PROTECTED]> wrote:
> I have done everything you said but I still did not manage to add a
> different language to an existing Post or Article.
>
> Maybe you could send me a working code example and a dump of your
> database?

Hi there,

I've tried the TranslationIt behaviour, did not find any difference
either.

Worked out the edit action for multiple languages at once. A working
edit example

On request of zwobot a full dump of the database & code (regarding
this issue ;-)

Tables (MySQL dump)

--
-- Table structure for table `articles`
--

CREATE TABLE `articles` (
  `id` int(8) NOT NULL auto_increment,
  `created_at` datetime NOT NULL,
  `active` varchar(3) collate utf8_unicode_ci NOT NULL default 'yes',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=2 ;

--
-- Dumping data for table `articles`
--

INSERT INTO `articles` (`id`, `created_at`, `active`) VALUES (1,
'2007-07-04 15:33:56', 'yes');

--
-- Table structure for table `i18n`
--

CREATE TABLE `i18n` (
  `id` int(10) NOT NULL auto_increment,
  `locale` varchar(6) collate utf8_unicode_ci NOT NULL,
  `i18n_content_id` int(10) NOT NULL,
  `model` varchar(255) collate utf8_unicode_ci NOT NULL,
  `row_id` int(10) NOT NULL,
  `field` varchar(255) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `locale` (`locale`),
  KEY `i18n_content_id` (`i18n_content_id`),
  KEY `row_id` (`row_id`),
  KEY `model` (`model`),
  KEY `field` (`field`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=97 ;

--
-- Dumping data for table `i18n`
--

INSERT INTO `i18n` (`id`, `locale`, `i18n_content_id`, `model`,
`row_id`, `field`) VALUES
(91, 'eng', 107, 'Article', 1, 'title'),
(92, 'eng', 108, 'Article', 1, 'content'),
(93, 'fra', 109, 'Article', 1, 'title'),
(94, 'fra', 110, 'Article', 1, 'content'),
(95, 'dut', 111, 'Article', 1, 'title'),
(96, 'dut', 112, 'Article', 1, 'content');

--
-- Table structure for table `i18n_content`
--

CREATE TABLE `i18n_content` (
  `id` int(10) NOT NULL auto_increment,
  `content` text collate utf8_unicode_ci,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=113 ;

--
-- Dumping data for table `i18n_content`
--

INSERT INTO `i18n_content` (`id`, `content`) VALUES
(107, 'English title'),
(108, 'English content'),
(109, 'French titel'),
(110, 'French content'),
(111, 'Dutch title'),
(112, 'Dutch content');

Article model

<?php
class Article extends AppModel {

        var $name = 'Article';

        var $actsAs = array('Translate' => array('title' ,'content'));
}
?>


Article controller (the admin_edit action part)


function admin_edit($id = null) {
        $languages = explode(',',$this->Conf->get('site.languages'));
        //using Conf component, will give something like 'eng,dut,fra,etc...'

        if(!$id && empty($this->data)) {
                $this->Session->setFlash('Invalid Article');
                $this->redirect(array('action'=>'index'), null, true);
        }
        if(!empty($this->data)) {
                $this->cleanUpFields();
                $updateCounter = 0;
                foreach($languages as $lang){
                        //get TranlationXXX arrays and the locale field in the 
Article
model, set the locale and save it.
                        $this->record['Article'] = $this->data['Article'];
                        $this->record['Article']['locale'] = $lang;
                        $transKey = 'Translation' . Inflector::camelize($lang);

                        foreach($this->data[$transKey] as $key => $value){
                                $this->record['Article'][$key] = $value;
                        }

                        $this->Article->locale = $lang;

                        if($this->Article->save($this->record)){
                                $updateCounter++;
                        }
                }
                if($updateCounter > 0){
                        $this->Session->setFlash('The Article saved, ' . 
$updateCounter . '
translations saved');
                        $this->redirect(array('action'=>'index'), null, true);
                }
                else{
                        $this->Session->setFlash('The Article could not be 
saved. Please,
try again.');
                }

        }
        if(empty($this->data)) {

                $counter = 0;

                foreach($languages as $lang){
                        $this->Article->locale = $lang;
                        $result = $this->Article->find(array ("id => 
$id"),'','',2);

                        if($counter == 0){
                                $thisKeys = array_keys($result['Article']);
                                $this->data = $result;
                                $counter = 1;
                        }
                        foreach($thisKeys as $key){
                                $this->data[('Translation' . 
Inflector::camelize($lang))][$key] =
$result['Article'][$key];
                        }
                        //this will give back the TranslationXXX for every 
language, XXX =
language code - in this example TranslationEng, TranslationDut &
TranslationFra. See end of this post for an example

                }

        }
}


Article admin_edit view

<div class="article">
<h2>Edit Article</h2>
        <?php echo $form->create('Article');?>
                <?php echo $form->input('id');?>
                <?php echo $form->input('active');?>
                <?php echo $form->input('TransTitle.eng.content');?>

                <div id="langNav">
                        <?php foreach($this->data as $key => $value): ?>
                                <?php if(substr($key,0,11) == 'Translation'): ?>
                                        <?php $langTab[substr($key,11)] = 
$value; ?>
                                        <?php $tabLabel = substr($key,11); ?>
                                        <a href="javascript:doTabs('tab<?php 
echo $tabLabel; ?>');"><?php
echo $html->image('flags/' . strtolower($tabLabel) .
'.gif',array('border'=>'0','align'=>'top','width'=>'16','height'=>'11'),false); 
?
> <?php echo strtoupper($tabLabel); ?></a> -
                                <?php endif; ?>
                        <?php endforeach; ?>
                </div>


                <?php foreach($langTab as $key => $value): ?>
                        <div id="tab<?php echo $key; ?>" class="langTab">
                                <?php echo $form->input('Translation' . $key . 
'.title'); ?>
                                Content:<br /><?php echo 
$form->textarea('Translation' . $key .
'.content',array('cols'=>'100','rows'=>'10','class'=>'fck'));?>
                                <?php //echo $fck->load('Translation' . $key . 
'/content')?>
                        </div>
                <?php endforeach; ?>
                <?php echo $form->submit('Update');?>

        </form>
</div>

Example data dump for an edit action  <?php debug($this->data); ?>:
   [Article] => Array
        (
            [id] => 1
            [created_at] => 2007-07-04 15:33:56
            [active] => yes
            [locale] => eng
            [title] => English title
            [content] => English content
        )

    [TranslationEng] => Array
        (
            [id] => 1
            [created_at] => 2007-07-04 15:33:56
            [active] => yes
            [locale] => eng
            [title] => English title
            [content] => English content
        )

    [TranslationDut] => Array
        (
            [id] => 1
            [created_at] => 2007-07-04 15:33:56
            [active] => yes
            [locale] => dut
            [title] => Dutch title
            [content] => Dutch content
        )

    [TranslationFra] => Array
        (
            [id] => 1
            [created_at] => 2007-07-04 15:33:56
            [active] => yes
            [locale] => fra
            [title] => French titel
            [content] => French content
        )





Don't mind the fancy stuff, I use a tab like interface buid in jquery,
and the fck editor. For each language a tab with fields is created

Ok this works. Some issues:
- When a translation for a language is not already in the i18n /
i18n_content tables, a errors occurs when saving. Cake tries to insert
a new record in the article table for each language not already
present. Expected behaviour would be to only create the i18n &
i18n_content records.
- Did not figure out how to hand back validation errors to the view.


Comments are welcome


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to