Welcome to the Cookbook

loading...

6.3.6 Saving in another language

You can force the model which is using the TranslateBehavior to save in a language other than the one detected.

To tell a model in what language the content is going to be you simply change the value of the $locale property on the model before you save the data to the database. You can do that either in your controller or you can define it directly in the model.

Example A: In your controller
<?php
class PostsController extends AppController {
	var $name = 'Posts';
	
	function add() {
		if ($this->data) {
			$this->Post->locale = 'de_de'; // we are going to save the german version
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->redirect(array('action' => 'index'));
			}
		}
	}
}
?>
  1. <?php
  2. class PostsController extends AppController {
  3. var $name = 'Posts';
  4. function add() {
  5. if ($this->data) {
  6. $this->Post->locale = 'de_de'; // we are going to save the german version
  7. $this->Post->create();
  8. if ($this->Post->save($this->data)) {
  9. $this->redirect(array('action' => 'index'));
  10. }
  11. }
  12. }
  13. }
  14. ?>
Example B: In your model
<?php
class Post extends AppModel {
	var $name = 'Post';
	var $actsAs = array(
		'Translate' => array(
			'name'
		)
	);
	
	// Option 1) just define the property directly
	var $locale = 'en_us';
	
	// Option 2) create a simple method 
	function setLanguage($locale) {
		$this->locale = $locale;
	}
}
?>
  1. <?php
  2. class Post extends AppModel {
  3. var $name = 'Post';
  4. var $actsAs = array(
  5. 'Translate' => array(
  6. 'name'
  7. )
  8. );
  9. // Option 1) just define the property directly
  10. var $locale = 'en_us';
  11. // Option 2) create a simple method
  12. function setLanguage($locale) {
  13. $this->locale = $locale;
  14. }
  15. }
  16. ?>