Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email: hello@sanisoft.com

Blog

[CakePHP] How to fallback to default language while using Translate behavior

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

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

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

About the Author

Amit Badkas is Zend certified PHP5 and Zend Framework engineer, and has been working in SANIsoft for past 10 years, his present designation is 'Technical Manager'

2 comments

  1. 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?

  2. Pingback: [CakePHP] Tweak translate behavior get belongsTo translations at SANIsoft

Leave a Reply