I'm having a hard time making my website multilingual and I after going through the net seeking for answers, I found out that I wasn't the first one to have these issues with version 2.x.
I'm trying to translate my blog. So I have a Post.php model, a PostsController.php and a view admin_edit.ctp.
Using the console, I've created the i18n table by hitting the command > cake i18n.
Following are my files:
bootstrap.php
Configure::write('Config.languages', array('fra','eng'));
Configure::write('Config.language','fra');
Post.php
<?php
class Post extends AppModel{
public $actsAs = array(
'Translate' => array(
'name' => '_name',
'slug' => '_slug',
'content' => '_content'
)
);
}
PostsController.php
function admin_edit($id=null){
if (!empty($this->request->data)) {
$this->Post->create();
$this->Post->locale = Configure::read('Config.languages');
if (!empty($this->request->data['Post']['file']['name']) && !empty($this->request->data['Post']['photo'])) {
$dir = IMAGES.'news';
if (!file_exists($dir)) {
mkdir($dir,0777);
}
$photo = preg_replace('~[^\w-\.]~', '-', strtolower($this->request->data['Post']['photo']['fra']));
$f = explode('.',$this->request->data['Post']['file']['name']);
$ext = '.'.end($f);
$filename = $photo.date('-YmdHis');
move_uploaded_file($this->request->data['Post']['file']['tmp_name'], $dir.DS.$filename.$ext);
}
if ($this->Post->save($this->request->data, false)) {
debug($this->request->data);
// $this->redirect(array('action' => 'admin_index'));
}
if ($id!=null) {
$this->Post->id = $id;
$this->request->data = $this->Post->readAll();
}
}
}
admin_edit.ctp
<?php
echo $this->Form->create('Post', array('type' => 'file'));
echo $this->Form->input('file', array('type' => 'file'));
echo $this->Form->input('photo', array();
foreach (Configure::read('Config.languages') as $lang):
echo $this->Form->input('Post.name.'.$lang, array('div' => false, 'label' => false));
echo $this->Form->input('Post.slug.'.$lang, array('div' => false, 'label' => false));
echo $this->Form->textarea('Post.content.'.$lang, array('div' => false, 'label' => false));
endforeach;
echo $this->Form->input('id');
echo $this->Form->hidden('created');
echo $this->Form->end('done');
?>
----------------------
You will notice that in my controller I use $this->Post->locale = Configure::read('Config.languages'); to save all my languages at once.
To do that, I also loop my input in the view foreach (Configure::read('Config.languages') as $lang):
This code is working as it but it's not enough. As you can see I have a picture and I would like to insert its name in the posts table.
Right now, if I want to save the name of the picture, I have to do like that: my-image.jpg, meaning I have to manually format it in the form.
Also, I have a slug and to create it I would like to use the name field and add "-" instead of the spaces to format my slug.
With my code above, I just do a simple $this->Post->save($this->request->data, false) but I cannot format my data.
What I'd like to do is something like that for example:
$success = array(
'name' => $name,
'slug' => $slug,
'photo' => $photo,
'content' => $content,
'id' => $id
);
$this->Post->save($success,false);
The problem when I do that is that it needs to be saved in as many languages as I have because. When I loop it to save all the languages, everything is save in the i18n table,
all the values are translated, the only problem is that the locale column data are all the same, the locale that has been used when saved.
By the way, you will notice that I disabled the validation in the second parameter of the save. If I don't do that, nothing happens.
That's one of the problem I can't figure, why couldn't I do validations?
So my questions are, how can I save my data in both tables i18n and posts with the possibility to format the data before saving?
How come I have to disable the validation to make the saving work?
If you already done that on V2.x, how did you do it?
I'm really stuck on this, the only solution I have right now is no validations, and format data manually in the form.
This is really not convenient and not pro.
I really hope somebody will help me, I've posted this problem in every forum and group I know, and nobody answered me yet.
Help for a desperate guy...
Thank you very very much for your help!
-- Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment