I’m trying to add Zend_Translate in a PHP project, using Zend components as standalone libraries.
I’m already using cache for several items using the following method:
$cache = Zend_Cache::factory( ...
if (!($obj = $cache->load('OBJ')))
{
$obj = ...
$cache->save($obj);
}
Now, following the documentation of Zend_Translate, I set the same $cache object to my Zend_Translate with a setCache before to actually create the object:
Zend_Translate::setCache($cache);
$translate = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => 'languages',
null,
array('scan' => Zend_Translate::LOCALE_FILENAME)
)
);
So, here I’m steering away from my usual method which instead would have been to put the whole Zend_Translate in the cache. The overall result, as of now is the following:
// 1.php
ob_start();
session_start();
$cache = Zend_Cache::factory( ...
if (!($obj = $cache->load('OBJ')))
{
$obj = ...
$cache->save($obj);
}
Zend_Translate::setCache($cache);
$translate = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => 'languages',
null,
array('scan' => Zend_Translate::LOCALE_FILENAME)
)
);
echo $translate->_("Hello, I'm the first script");
// end 1.php
and
// 2.php
ob_start();
session_start();
$cache = Zend_Cache::factory( ...
if (!($obj = $cache->load('OBJ')))
{
$obj = ...
$cache->save($obj);
}
Zend_Translate::setCache($cache);
$translate = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => 'languages',
null,
array('scan' => Zend_Translate::LOCALE_FILENAME)
)
);
echo $translate->_("Hello, I'm the second script");
// end 2.php
This approach doesn’t work as I see that the cache files are created every time that I load the page.
I am wondering:
- Am I correct to assume that I need to call
Zend_Cache::factoryin every page? - How can I get my translate to work with cache in this standalone situation?
- Question about
Zend_Translate: doesaddTranslationadd anything to the picture or can I just load all my translations like I do?
Thank you!
Question #1 Yes. You need to call Zend_Cache::factory on every page load. But if you already use Zend_Cache you can re-use it for your needs instead of creating a new instance for Zend_Translate.
Question #2. There is no difference in usage of Zend Translate + Zend Cache as standalone components. Here is how I use them as standalone components in such small scripts:
OK, you are ready to use Zend_Cache. All caching will be covered by Zend_Translate instance itself internally.
Question #3. addTranslation() just adds new translation sources, you can pass them to constructor instead (your case).
Try to set “cache_id_prefix” for cache before passing it to Zend_Translate, that will help you to determine if problem occurs somewhere in your code “…” or new cache that is created on each page load is created by Zend Translate.