Welcome to the Cookbook

loading...

6.3.7 Multiple Translation Tables

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

If you expect a lot of entries you probably wonder how to deal with a rapidly growing database table. There are two properties introduced by TranslateBehavior that allow you to specify which "Model" to bind as the model containing the translations.

These are $translateModel and $translateTable.

Lets say we want to save our translations for all posts in the table "post_i18ns" instead of the default "i18n" table. To do so you need to setup your model like this:

<?php
class Post extends AppModel {
	var $name = 'Post';
	var $actsAs = array(
		'Translate' => array(
			'name'
		)
	);
	
	// Use a different model (and table)
	var $translateModel = 'PostI18n';
}
?>
  1. <?php
  2. class Post extends AppModel {
  3. var $name = 'Post';
  4. var $actsAs = array(
  5. 'Translate' => array(
  6. 'name'
  7. )
  8. );
  9. // Use a different model (and table)
  10. var $translateModel = 'PostI18n';
  11. }
  12. ?>

Important to note is that you have to pluralize the table. It is now a usual model and can be treated as such and thus comes with the conventions involved. The table schema itself must be identical with the one generated by the CakePHP console script. To make sure it fits one could just initialize an empty i18n table using the console and rename the table afterwards.

6.3.7.1 Create the TranslateModel

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

For this to work you need to create the actual model file in your models folder. The reason is that there is no property to set the displayField directly in the model using this behavior yet.

Make sure that you change the $displayField to 'field'.

<?php
class PostI18n extends AppModel { 
	var $displayField = 'field'; // important
}
// filename: post_i18n.php
?>
  1. <?php
  2. class PostI18n extends AppModel {
  3. var $displayField = 'field'; // important
  4. }
  5. // filename: post_i18n.php
  6. ?>

That's all it takes. You can also add all other model stuff here like $useTable. But for better consistency we could do that in the model which actually uses this translation model. This is where the optional $translateTable comes into play.

6.3.7.2 Changing the Table

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

If you want to change the name of the table you simply define $translateTable in your model, like so:

<?php
class Post extends AppModel {
	var $name = 'Post';
	var $actsAs = array(
		'Translate' => array(
			'name'
		)
	);
	
	// Use a different model
	var $translateModel = 'PostI18n';
	
	// Use a different table for translateModel
	var $translateTable = 'post_translations';
}
?>
  1. <?php
  2. class Post extends AppModel {
  3. var $name = 'Post';
  4. var $actsAs = array(
  5. 'Translate' => array(
  6. 'name'
  7. )
  8. );
  9. // Use a different model
  10. var $translateModel = 'PostI18n';
  11. // Use a different table for translateModel
  12. var $translateTable = 'post_translations';
  13. }
  14. ?>

Please note that you can't use $translateTable alone. If you don't intend to use a custom $translateModel then leave this property untouched. Reason is that it would break your setup and show you a "Missing Table" message for the default I18n model which is created in runtime.