views:

148

answers:

4

I'm updating an php web application that is becoming multilingual, based on the Zend MVC framework, and I'm trying to figure out the best approach to passing the translation object to different classes/layers.

Most of my translation is done at the View level, but there are a few cases where I need to return status messages from custom libraries.

I could just create a property for the library and set the translator, but I'm wondering if there is a better way to integrate a translator object into an existing application?

A: 

You could always instantiate the translator in the bootstrap.php so it is available to all classes as a global. That's how I'd do it since you probably need to use it everywhere. It isn't elegant, but it keeps you from having to add code wherever a class needs to throw an exception or return an error message.

Byron Whitlock
yeah that's one option I'm leaning towards, which as you stated isn't elegant but the most practical. I may not have to do this at all though, trying to push language specific code up a level so the library is just sending back status codes that the controller should know about.Thanks for the reply.
AndreLiem
A: 

If you don't have that many controllers setup can you not extend the base controller and instantiate the translator there? It should be available for use throughout the system then.

Something like this to extend:

<?php
class BaseController extends Zend_Controller_Action
{
  public function init()
  {
    //setup translation object
  }
}
?>
Andi
+2  A: 

Hold the users lanaguage in a Memento, and pass that through the program logic, when you need to do that translation use it identify the language.

Martin Spamer
+1  A: 

If using Zend_Translate, it's best option to use register.

Zend_Registry::set('Zend_Translate', $translate);

This way all classes can find it automatically (Zend_Form, Zend_Validate, ...)

Tomáš Fejfar