Translator

Caching

In production, it makes sense to cache your translations. This not only saves you from loading and parsing the individual formats each time, but also guarantees an optimized loading procedure.

Installation requirements

The cache support of zend-i18n depends on the zend-cache component, so be sure to have it installed before getting started:

bash $ composer require zendframework/zend-cache

Enable Caching

To enable caching, pass a Zend\Cache\Storage\Adapter to the setCache() method.

$translator = new Zend\I18n\Translator\Translator();
$cache      = Zend\Cache\StorageFactory::factory([
    'adapter' => [
        'name'    => Zend\Cache\Storage\Adapter\Filesystem::class,
        'options' => [
            'cache_dir' => __DIR__ . '/cache',
        ],
    ],
]);
$translator->setCache($cache);

The explanation of creating a cache and and using different adapters for caching can be found in documentation of zend-cache.

Disable Caching

To disable the cache, pass a null value to the setCache() method.

$translator->setCache(null);

Clear Cache

To clear the cache for a specific text domain and locale, use the clearCache method.

$translator->clearCache('default', 'en_US');

Get Cache Identifier

To get the cache identifier for a specific text domain and locale, use the getCacheId method:

$translator->getCacheId('default', 'en_US');

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!