#6094: Translate Behavior - Find'ing multiple locales
--------------------------+-------------------------------------------------
Reporter: zaboo | Type: Enhancement
Status: new | Priority: Medium
Milestone: 1.2.x.x | Component: i18n/l10n
Version: 1.2 Final | Severity: Normal
Keywords: | Php_version: n/a
Cake_version: |
--------------------------+-------------------------------------------------
As you are able to have a form, and save model data in the form of:
{{{
data => array(
Model => array(
field => array(
en_us => 'US Text',
en_gb => 'GB Text',
en_au => 'AU Text',
en_ca => 'CA Text',
)
)
)
}}}
It would be very nice, if there was a method to do a Model::find and
select multiple locales and have it in the right format to automagically
populate the form.
{{{
$this->Model->locale = 'en_us'; // selects US fine
$this->Model->locale = array('en_us', 'en_ca'); // selects US fine, with
fallback locales
}}}
A boolean would be sufficient to tell cake to index all the locale
information into an array, instead of just using the first locale found.
I ended up just doing a custom behavior (extends TranslateBehavior) to
handle my desired functionality.
{{{
/**
* afterFind Callback
*
* @param array $results
* @param boolean $primary
* @return array Modified results
* @access public
*/
function afterFind(&$model, $results, $primary) {
$this->runtime[$model->alias]['fields'] = array();
$locale = $this->_getLocale($model);
if (empty($locale) || empty($results) ||
empty($this->runtime[$model->alias]['beforeFind'])) {
return $results;
}
$beforeFind = $this->runtime[$model->alias]['beforeFind'];
foreach ($results as $key => $row) {
$results[$key][$model->alias]['locale'] =
(is_array($locale)) ? @$locale[0] : $locale;
foreach ($beforeFind as $field) {
if (is_array($locale)) {
foreach ($locale as $_locale) {
//
// Additional check here; To see if all locale information should be
indexed into an array
//
if(isset($model->getAllLocales) && $model->getAllLocales == true) {
$results[$key][$model->alias][$field][$_locale] =
$results[$key]['I18n__'.$field.'__'.$_locale]['content'];
} else {
if
(!isset($results[$key][$model->alias][$field]) &&
!empty($results[$key]['I18n__'.$field.'__'.$_locale]['content'])) {
$results[$key][$model->alias][$field] =
$results[$key]['I18n__'.$field.'__'.$_locale]['content'];
}
unset($results[$key]['I18n__'.$field.'__'.$_locale]);
}
}
if
(!isset($results[$key][$model->alias][$field])) {
$results[$key][$model->alias][$field] = '';
}
} else {
$value = '';
if
(!empty($results[$key]['I18n__'.$field]['content'])) {
$value =
$results[$key]['I18n__'.$field]['content'];
}
$results[$key][$model->alias][$field] = $value;
unset($results[$key]['I18n__'.$field]);
}
}
}
return $results;
}
}}}
--
Ticket URL: <https://trac.cakephp.org/ticket/6094>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC.
Our primary goal is to provide a structured framework that enables PHP users at
all levels to rapidly develop robust web applications, without any loss to
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"tickets cakephp" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---