Webeks.net - freelance programming
freelance programming - php, Joomla, Zend ...
Home :: Articles :: Programming :: PHP :: Zend Translate Adapter for DB (MySql)

Zend Translate Adapter for DB (MySql)

Written by Miha

 

Zend Framework offers multiple adapters for translations. Among them is not an adapter for database translating. Here is how-to :)

I need to point out that there are other possibilities that offer much more flexibility than database and are designed to handle translations (one of them that is widely used and I recommend is Gettext for which Zend already has native adapter). Use this one if you are not sure!

Advantages for storing translations in database

  • you can program custom translation tool
  • more flexible

Disadvantages for storing translations in database

  • you have to program custom translation tools (gettext has tools like PO Edit)
  • performance should be slower that with other tools like gettext (although I did not test this)

Zend Translate Adapter for Database

As I said before Zend is offering different adapters for translating. They are located under library/Zend/Translate/Adapter. When writing a custom code it usually pays off to review the code already written. If we open few translate adapters and look at the code it's quite obvious that it does two basic things:

  • opens translation storage
  • parses translations in one array for later use in translate object

With this knowledge we can write our own adapter that works with database.

Note: ALWAYS extend the code in your own library NEVER modify the core!

Prepare model that reads translations from db

Firstly we create a model that fetches our translations from database. For simplicity sake I used the same approach as gettext uses (msgid, msgstring).

  1. /**
  2. * Database model for photos
  3. */
  4. class Application_Model_Translation extends Zend_Db_Table_Abstract {
  5.  
  6. /**
  7. * The default table name
  8. */
  9. protected $_name = 'translation';
  10.  
  11. /**
  12. * The primary key column.
  13. *
  14. * @var string
  15. */
  16. protected $_primary = 'translation_id';
  17.  
  18.  
  19. /**
  20. * Get list of locale translations
  21. * @param locale
  22. */
  23. public function getListByLocale($locale) {
  24. $select = $this->_db->select()
  25. ->from( $this->_name, array('msgid', 'msgstring'))
  26. ->where('locale = ?', $locale );  
  27.  
  28. return $this->_db->fetchAll($select);
  29. }
  30.  
  31. }

 

Prepare translation helper

  1.  
  2.  
  3. /** Zend_Locale */
  4. require_once 'Zend/Locale.php';
  5.  
  6. /** Zend_Translate_Adapter */
  7. require_once 'Zend/Translate/Adapter.php';
  8.  
  9. class App_Translate_Adapter_Db extends Zend_Translate_Adapter
  10. {
  11.  
  12. private $_data = array();
  13.  
  14. /**
  15. * override the default Zend_Translate_Adapter function with our own
  16. */
  17.  
  18. protected function _loadTranslationData($filename, $locale, array $options = array())
  19. {
  20.  
  21. $this->_data = array();
  22.  
  23. //get translations from db
  24.  
  25. $db = Zend_Registry::get('db');
  26.  
  27. $translation = new Application_Model_Translation( array('db'=>$db) );
  28. $translations = $translation->getListByLocale( Zend_Registry::get('Zend_Locale')->__toString() );
  29.  
  30. foreach ($translations as $trans) {
  31. $this->_data[$locale][ $trans['msgid'] ] = $trans['msgstring'];
  32. }
  33.  
  34. return $this->_data;
  35.  
  36. }
  37.  
  38. }
  39.  
  40.  

Put it all together

You'll probably want to initialize translation object with this adapter at bootstrap.

 


blog comments powered by Disqus