Issues

ZF-3930: When using caching with zend_tanslate it appears that no translation at all is performed

Issue Type: Bug Created: 2008-08-12T08:35:12.000+0000 Last Updated: 2008-09-02T10:39:45.000+0000 Status: Resolved Fix version(s): - 1.6.0 (02/Sep/08)

Reporter: Marcin Lulek (ergo14) Assignee: Thomas Weidner (thomas) Tags: - Zend_Translate

Related issues: Attachments: - default.mo

Description

When using caching with zend_tanslate it appears that no translation at all is performed

<pre class="highlight">
    function indexAction(){
        require_once 'Zend/Locale.php';
        require_once 'Zend/Translate.php';
        $config = Zend_Registry::get('config');
        $this->locale = new Zend_Locale($config->setup->defaultLocale);
        Zend_Registry::set('locale', $this->locale);
        Zend_Translate::setCache(Zend_Registry::get('cache'));
        Zend_Registry::set('Zend_Translate', new Zend_Translate('gettext', './Application/Locale/pl/default.mo', 'pl'));
    }

removing Zend_Translate::setCache(Zend_Registry::get('cache')); - makes app work fine

Comments

Posted by Marcin Lulek (ergo14) on 2008-08-12T08:35:35.000+0000

test case

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-12T10:15:52.000+0000

I tried to work this into a more succinct use case:

<pre class="highlight">
<?php
$translations = array(
    'foo' => 'Foo Bar!',
    'boo' => 'Boo Hoo!',
);

$cacheOptions = array(
    'frontend' => array(
        'automatic_serialization' => true,
        'lifetime' => 3,
    ),
    'backend' => array(
        'cache_db_complete_path' => dirname(__FILE__) . '/translate.db',
    ),
);
$cache = Zend_Cache::factory('Core', 'Sqlite', $cacheOptions['frontend'], $cacheOptions['backend']);

$local = new Zend_Locale('en_US');
Zend_Translate::setCache($cache);
$translate = new Zend_Translate('array', $translations, 'en_US');

echo $translate->translate('foo');
echo $translate->translate('foo');
sleep(3);
echo $translate->translate('foo');
sleep(3);
echo $translate->translate('foo');

However, this worked fine. It may be an issue with the gettext adapter specifically, and I'll try that next.

Posted by Thomas Weidner (thomas) on 2008-08-12T10:23:56.000+0000

The adapter classes themself have no access to the cache. They only read the data and connect them to the abstract class.

The behaviour of the cache is completly independent of the used adapter.

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-12T10:24:57.000+0000

Okay, I tried the following now:

<pre class="highlight">
<?php
$cacheOptions = array(
    'frontend' => array(
        'automatic_serialization' => true,
        'lifetime' => 3,
    ),
    'backend' => array(
        'cache_db_complete_path' => dirname(__FILE__) . '/translate.db',
    ),
);
$cache = Zend_Cache::factory('Core', 'Sqlite', $cacheOptions['frontend'], $cacheOptions['backend']);

$locale = new Zend_Locale('en');
Zend_Translate::setCache($cache);
$translate = new Zend_Translate('gettext', dirname(__FILE__) . '/testcase/translate/Application/Locale/pl.mo', 'pl');

echo $translate->translate('o_object_added');
sleep(3);
echo $translate->translate('o_object_added');
sleep(3);
echo $translate->translate('o_object_added');
sleep(3);
echo $translate->translate('o_object_added');

I now get the following exception:

<pre class="highlight">
PHP Fatal error:  Uncaught exception 'Zend_Translate_Exception' with message 'Error opening translation file '/home/matthew/tmp/testcase/translate/Application/Locale/pl.mo'.' in /home/matthew/lib/framework-full/trunk/library/Zend/Translate/Adapter/Gettext.php:88

I get this error with or without caching. At this point, I cannot reproduce unless we get a valid translation file to test against.

Posted by Marcin Lulek (ergo14) on 2008-08-12T10:31:46.000+0000

default pl po file

Posted by Thomas Weidner (thomas) on 2008-08-12T10:32:32.000+0000

Question:

Are you changing the locale when using the same cache ? Because actually caching is only done for the complete adapter...

When you set the cache and want to load another language afterwards it will not be recognised. Caching is only done for the complete adapter at initiation.

You should add all languages/files once.

There exists already an issue (ZF-3071) which will add this behaviour (caching on file-basis/adding translations afterwards to the cache). But it is not implemented for now.

The actual solution is to use a locale-based cache.

Posted by Marcin Lulek (ergo14) on 2008-08-12T10:34:45.000+0000

polish mo file

Posted by Thomas Weidner (thomas) on 2008-08-12T10:35:11.000+0000

A po file will not be recognised by Zend_Translate... you should add the related binary gettext file (*.mo)

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-12T10:38:36.000+0000

I've run the reproduce case I posted in an earlier comment against the .mo file linked in the issue, and it works both with and without caching.

Posted by Thomas Weidner (thomas) on 2008-08-12T10:45:12.000+0000

Is the string you wanted to translate available in the mo file ? Try to use isTranslated() to verify.

Is the right data loaded ? Try to use getList() and getMessages() to verify.

Try the translation itself without application as showed by Matthew to verify if it works or not.

Posted by Marcin Lulek (ergo14) on 2008-08-12T10:48:54.000+0000

<pre class="highlight">
<?php
set_include_path('.' . PATH_SEPARATOR . './Library/'
. PATH_SEPARATOR . './Application/'
. PATH_SEPARATOR . get_include_path());
require 'Zend/Registry.php';
require 'Zend/Cache.php';
$cache = Zend_Cache::factory('Core', 'File' , array('automatic_serialization' => true, 'lifetime' => 3 ), array('cache_dir' =>'./Cache'));
Zend_Registry::set('cache',$cache);
require 'Zend/Controller/Front.php';
$frontController = Zend_Controller_Front::getInstance();
require_once 'Zend/Locale.php';
require_once 'Zend/Translate.php';
$locale = new Zend_Locale('pl');
Zend_Registry::set('locale', $locale);
Zend_Translate::setCache(Zend_Registry::get('cache'));
Zend_Registry::set('Zend_Translate', new Zend_Translate('gettext', './Application/Locale/pl/default.mo', 'pl'));
echo Zend_Registry::get('Zend_Translate')->translate('mainOptions');
?>

simplified test case

Posted by Thomas Weidner (thomas) on 2008-08-12T11:08:11.000+0000

Running your testcode I get returned:

Ustawienia główne

Which seems to me like all is translated as required.

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-12T11:16:32.000+0000

As noted on IRC, I've tried the above simplified test case, varying the cache lifetime as well as looping and setting random wait durations before translating -- in each case, I get the translated strings. This is looking like a caching issue.

Posted by Marcin Lulek (ergo14) on 2008-08-12T13:02:37.000+0000

the fix for the issue

on translate/adapter.php line 98:

add:

if($locale){$this->setLocale($locale); }

fixes the issue for me.

Posted by Thomas Weidner (thomas) on 2008-08-18T08:09:40.000+0000

Fixed with r10915

Posted by Wil Sinclair (wil) on 2008-09-02T10:39:45.000+0000

Updating for the 1.6.0 release.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts