04.30 php saved
franiglesias
Tags add more
1.2, i18n, multilingual and translate behavior  
Note
First, you need to set a
Configure::write('Config.languages', array('la1', 'la2'));

to indicate what languages your application supports.

Add 'Translate' and 'Multilingual' to the array of Behaviors of your model.

When you want to get multilingual versions of the data, previously you must call

model->getLocales();

to activate the multilingual find.

The translated results comes as an array

[field][la1]
[field][la2]
  1. <?php
  2.  
  3. /**
  4. * Combine multiple languages in returned result sets
  5. *
  6. * @package default
  7. * @author Fran Iglesias
  8. **/
  9. class MultilingualBehavior extends ModelBehavior {
  10.     var $_settings = array(
  11.         'configSupportedLocales' => 'Config.languages',
  12.         'returnLocales' => false,
  13.         );
  14.        
  15.     function setup(&$model, $settings = array()) {
  16.         $this->_settings = am($this->_settings, $settings);
  17.         if (!isset($model->multilingual)) {
  18.             $model->multilingual = $this->_settings['returnLocales'];
  19.         }
  20.     }
  21.    
  22.     function getLocales (&$model, $locales = '*') {
  23.         if (empty($locales)) {
  24.             $model->multilingual = false;
  25.             return;
  26.         }
  27.        
  28.         if ($locales == '*') {
  29.             $model->multilingual = Configure::read($this->_settings['configSupportedLocales']);
  30.             return;
  31.         }
  32.        
  33.         if (!is_array($locales)) {
  34.             $locales = array($locales);
  35.         }
  36.        
  37.         $model->multilingual = $locales;
  38.     }
  39.    
  40.     function afterFind (&$model, $results, $primary) {
  41.         if (empty($results)) {
  42.             return $results;
  43.         }
  44.        
  45.         if (empty($model->multilingual)) {
  46.             return $results;
  47.         }
  48.  
  49.         if ($model->multilingual) {
  50.             foreach ($results as $key => $result) {
  51.                 $id = $result[$model->name]['id'];
  52.                 $query = "SELECT locale, field, content FROM i18n WHERE model='{$model->name}' AND foreign_key = {$id}";
  53.                 $datos = $model->query($query);
  54.                 foreach ($datos as $i18) {
  55.                     extract($i18['i18n']);
  56.                     // Locale not asked for
  57.                     if (!in_array($locale, $model->multilingual)) {
  58.                         continue;
  59.                     }
  60.                     // Field not present in original query (2008-03-25)
  61.                     if (!isset($results[$key][$model->name][$field])) {
  62.                         continue;
  63.                     }
  64.                     if (!is_array($results[$key][$model->name][$field])) {
  65.                         unset($results[$key][$model->name][$field]);
  66.                     }
  67.                     $results[$key][$model->name][$field][$locale] = $content;
  68.                 }
  69.             }
  70.        
  71.         return $results;
  72.  
  73.         }
  74.        
  75.     }
  76. } // END class MultilingualBehavior extends ModelBehavior
  77.  
  78.  
  79. ?>
Parsed in 0.091 seconds, using GeSHi 1.0.7.14

Modify this Paste