views:

37

answers:

2

I have a project that uses Zend Framework and Zend_Translate.

I had to alter the standard CSV Adapter to Zend_Translate slightly. Now I'm confronted with the question where to put this altered adapter.

By default, adapters are stored in

/Library/Zend/Translate/Adapter/Adaptername.php

This is where I've put the new adapter as well.

However, I wouldn't like to "pollute" the Zend library with my custom extensions: I would like to stay able to update ZF without having to worry about losing files; I want to remain able to use a centrally installed version of ZF; and the custom adapter is part of the project I'm working on, really.

Is there a Zend Framework way of dealing with this, or specifying an alternative loading location?

Language adapters are loaded using

$this->_adapter = new $adapter($data, $locale, $options); 

(Where $adapter will be Zend_Translate_Adapter_Adaptername)

so standard autoloading rules apply. Is there a simple way to tell the Zend Autoloader to look in a second location?

+5  A: 

You can add it to the lib folder

/lib
 /Zend
  /Translate
   /Adapter
    /Csv.php
 /My
  /Translate
   /Adapter
    /Csv.php

Depending on how your autoloader is setup, you have to setup the "namespace" with it:

$autoloader->registerNamespace('My_');

Or, if you dont like this, put it into your models folder. Basically, it doesnt matter where you put it, as long as it is accessible somehow by the autoloader. The Zend_Autoloader can register arbitrary autoloader callbacks, so it's really up to you.

Gordon
My answer was about exactly the same, yours was just much better worded :) +1
Brad F Jacobs
Cheers @Gordon, but that would mean that I have to rewrite `Zend_Translate` itself as well, right? Because it constructs the class name from `Zend_Translate_Adapter`... according to the `$adapter` parameter you pass it, there is no possibility top make it a `My_Translate_Adapter_...` instead. What I would have to do is make Zend look in two directory structures for the *same* namespace - which may well be impossible. I'll look into Zend_Loader and see what I can find out.
Pekka
@Pekka no, that shouldnt be necessary. See http://framework.zend.com/manual/en/zend.translate.adapter.html#zend.translate.adapter.selfwritten
Gordon
@Gordon cheers, I overlooked that (even looked in the source to see whether it does exactly that.) It works now with a custom class. Thanks!!
Pekka
+1  A: 

I can't comment on Gordon's answer due to this site's rules, but he's got it right. To answer your question regarding how to load the adapter, you'll need to pass the full class name to the constructor of the translate object:

$translate = new Zend_Translate('My_Translate_Adapter_Class', ...);

The component first checks in the Zend namespace in case you've passed in a short name (like 'gettext'), but will then attempt to load the adapter name as a class directly.

At least, this is true in 1.10, and I imagine has been for a while.

Ryan Chouinard
I overlooked this fact - it works now. Thanks!
Pekka