For one of my projects, I had a requirement from a client to fallback to default language if a phrase in chosen language was not found. This had to be done using CakePHP's Translate behavior.
Following is the step by step guide to modify Translate behavior so that it will fallback to default language for such situation.
1. If you need to only use the fallback for find('all') or find('first'), then do following
- Modify beforeFind() and add one more condition
-
'I18n__'.$field.'.locale' => $locale
to $query['joins'] for else part of if (is_array($locale)). Also remove the
part just below it, as we have added needed condition to join conditions already.
- Modify afterFind() and replace $value = ''; with
.
2. If you need to use the fallback for find('count') only, then do following
- Modify beforeFind() and remove code in
-
if (is_string($query['fields']) && 'COUNT(*) AS '.$db->name('count') == $query['fields'])
block except return $query;.
3. If you need to use fallback for find('list') only, then do following alongwith option #1 above
- Modify beforeFind() and remove
loop.
Note :- This hack works with CakePHP-1.2 as well as CakePHP-1.3
This worked like a charm but unfortunately when I save a row for a specific language, the default row is also updated! Any idea how to prevent this?