0

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:

  1. Am I correct to assume that I need to call Zend_Cache::factory in every page?
  2. How can I get my translate to work with cache in this standalone situation?
  3. Question about Zend_Translate: does addTranslation add anything to the picture or can I just load all my translations like I do?

Thank you!

JoeSlav
  • 4,479
  • 4
  • 31
  • 50

2 Answers2

1

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:

// Init application
define('APP_PATH',      realpath(dirname(__FILE__)));
define('APP_DATA',  APP_PATH . '/lang');
define('APP_CACHE',     APP_PATH . '/tmp');

require_once 'Zend/Translate.php';
require_once 'Zend/Cache.php';

// initialize cache
$cache = Zend_Cache::factory(
      'Core'
        , 'File'
        , array(
              'caching'     => true
            , 'lifetime'    => 900
            , 'automatic_serialization' => true
            , 'automatic_cleaning_factor'   => 20
            , 'cache_id_prefix'             => 'Translate'
            )
        , array(
              'hashed_directory_level' => 0
            , 'cache_dir'   => APP_CACHE
        )
    );

// Setup translation object
Zend_Translate::setCache($cache);
$translate = new Zend_Translate('tmx', APP_DATA . '/general.tmx');

echo $translate->_("Hello, I'm the second script");

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.

webdevbyjoss
  • 504
  • 7
  • 20
-1

If I recall correctly, Zend_Cache usage in Zend_Translate is a bit of a misnomer. I think what it does is reduce directory scanning and locale setup by setting the options for the Translator, not the results of the translation strings themselves.

shrikeh
  • 661
  • 9
  • 12