1

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?

4

2 回答 2

5

您可以将其添加到 lib 文件夹

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

根据您的自动加载器的设置方式,您必须使用它设置“命名空间”:

$autoloader->registerNamespace('My_');

或者,如果您不喜欢这个,请将其放入您的模型文件夹中。基本上,你把它放在哪里并不重要,只要它可以被自动加载器以某种方式访问​​。Zend_Autoloader可以注册任意自动加载器回调,所以这完全取决于你。

于 2010-08-04T18:19:57.870 回答
1

由于该网站的规则,我无法评论 Gordon 的回答,但他说得对。要回答有关如何加载适配器的问题,您需要将完整的类名传递给翻译对象的构造函数:

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

组件首先检查 Zend 命名空间,以防您传入短名称(如“gettext”),但随后会尝试将适配器名称作为类直接加载。

至少,这在 1.10 中是正确的,我想已经有一段时间了。

于 2010-08-04T19:13:25.157 回答