I'm attending CakeFest 2010!

6.3.5 Retrieve all translation records for a field

There is no translation yet for this section. Please help out and translate this.. More information about translations

If you want to have all translation records attached to the current model record you simply extend the field array in your behavior setup as shown below. The naming is completely up to you.

<?php
class Post extends AppModel {
	var $name = 'Post';
	var $actsAs = array(
		'Translate' => array(
			'name' => 'nameTranslation'
		)
	);
}
?>
  1. <?php
  2. class Post extends AppModel {
  3. var $name = 'Post';
  4. var $actsAs = array(
  5. 'Translate' => array(
  6. 'name' => 'nameTranslation'
  7. )
  8. );
  9. }
  10. ?>

With this setup the result of $this->Post->find() should look something like this:

Array
(
     [Post] => Array
         (
             [id] => 1
             [name] => Beispiel Eintrag 
             [body] => lorem ipsum...
             [locale] => de_de
         )

     [nameTranslation] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [locale] => en_us
                     [model] => Post
                     [foreign_key] => 1
                     [field] => name
                     [content] => Example entry
                 )

             [1] => Array
                 (
                     [id] => 2
                     [locale] => de_de
                     [model] => Post
                     [foreign_key] => 1
                     [field] => name
                     [content] => Beispiel Eintrag
                 )

         )
)
  1. Array
  2. (
  3. [Post] => Array
  4. (
  5. [id] => 1
  6. [name] => Beispiel Eintrag
  7. [body] => lorem ipsum...
  8. [locale] => de_de
  9. )
  10. [nameTranslation] => Array
  11. (
  12. [0] => Array
  13. (
  14. [id] => 1
  15. [locale] => en_us
  16. [model] => Post
  17. [foreign_key] => 1
  18. [field] => name
  19. [content] => Example entry
  20. )
  21. [1] => Array
  22. (
  23. [id] => 2
  24. [locale] => de_de
  25. [model] => Post
  26. [foreign_key] => 1
  27. [field] => name
  28. [content] => Beispiel Eintrag
  29. )
  30. )
  31. )

Note: The model record contains a virtual field called "locale". It indicates which locale is used in this result.

Note that only fields of the model you are directly doing `find` on will be translated. Models attached via associations won't be translated because triggering callbacks on associated models is currently not supported.

6.3.5.1 Using the bindTranslation method

There is no translation yet for this section. Please help out and translate this.. More information about translations

You can also retrieve all translations, only when you need them, using the bindTranslation method

bindTranslation($fields, $reset)

$fields is a named-key array of field and association name, where the key is the translatable field and the value is the fake association name.

$this->Post->bindTranslation(array ('name' => 'nameTranslation'));
$this->Post->find('all', array ('recursive'=>1)); // need at least recursive 1 for this to work.
  1. $this->Post->bindTranslation(array ('name' => 'nameTranslation'));
  2. $this->Post->find('all', array ('recursive'=>1)); // need at least recursive 1 for this to work.

With this setup the result of your find() should look something like this:

Array
(
     [Post] => Array
         (
             [id] => 1
             [name] => Beispiel Eintrag 
             [body] => lorem ipsum...
             [locale] => de_de
         )

     [nameTranslation] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [locale] => en_us
                     [model] => Post
                     [foreign_key] => 1
                     [field] => name
                     [content] => Example entry
                 )

             [1] => Array
                 (
                     [id] => 2
                     [locale] => de_de
                     [model] => Post
                     [foreign_key] => 1
                     [field] => name
                     [content] => Beispiel Eintrag
                 )

         )
)
  1. Array
  2. (
  3. [Post] => Array
  4. (
  5. [id] => 1
  6. [name] => Beispiel Eintrag
  7. [body] => lorem ipsum...
  8. [locale] => de_de
  9. )
  10. [nameTranslation] => Array
  11. (
  12. [0] => Array
  13. (
  14. [id] => 1
  15. [locale] => en_us
  16. [model] => Post
  17. [foreign_key] => 1
  18. [field] => name
  19. [content] => Example entry
  20. )
  21. [1] => Array
  22. (
  23. [id] => 2
  24. [locale] => de_de
  25. [model] => Post
  26. [foreign_key] => 1
  27. [field] => name
  28. [content] => Beispiel Eintrag
  29. )
  30. )
  31. )