Caching Translations

1.1k views Asked by At

I think I've hit a mind-road-block and I just can't get out of my own way to figure this situation out.

Before I go any further, one of my goals (if possible) is to set 'config_cache_enabled' => true inside of my application.config.php file so that my configurations are all merged together while being able to cache translations.

In my module.config.php I have the following:

'translator'  => array(
    'locale'                    => 'en',
    'translation_file_patterns' => array(
        array(
            'type'        => 'gettext',
            'base_dir'    => __DIR__ . '/../language',
            'pattern'     => '%s.mo',
            'text_domain' => 'mytextdomain'
        )
    )
)

Translations are working as expected. Now I'd like to add cache support so I change the previous example to this:

'translator'  => array(
    'locale'                    => 'en',
    'translation_file_patterns' => array(
        array(
            'type'        => 'gettext',
            'base_dir'    => __DIR__ . '/../language',
            'pattern'     => '%s.mo',
            'text_domain' => 'mytextdomain'
        )
    ),
    'cache' => \Zend\Cache\StorageFactory::factory(array(
        'adapter' => array(
            'name'    => 'Filesystem',
            'options' => array(
                'cache_dir' => __DIR__ . '/../../../data/cache',
                'ttl'       => '3600'
            )
        ),
        'plugins' => array(
            array(
                'name'    => 'serializer',
                'options' => array()
            ),
            'exception_handler' => array(
                'throw_exceptions' => true
            )
        )
    ))
)

This results in a circular reference problem because of the closure(). This will work if I set 'config_cache_enabled' => false but that isn't the goal which I am after.

Is it even possible to do what I am attempting? If so, any examples you could share would be very helpful.

1

There are 1 answers

1
jchampion On BEST ANSWER

You should only write raw data in your configuration files (the ones that will be cached). No closure, nothing that can not be safely serialized.

This should do exactly the same thing :

'translator'  => array(
    ...
    'cache' => array(
        'adapter' => array(
            'name'    => 'Filesystem',
            'options' => array(
                'cache_dir' => __DIR__ . '/../../../data/cache',
                'ttl'       => '3600'
            )
        ),
        'plugins' => array(
            array(
                'name'    => 'serializer',
                'options' => array()
            ),
            'exception_handler' => array(
                'throw_exceptions' => true
            )
        )
    )
)