Translator

Factory

Setting Locale

$translator = Zend\I18n\Translator\Translator::factory(
    [
        'locale' => 'de_DE',
    ]
);

Setting Fallback Locale

$translator = Zend\I18n\Translator\Translator::factory(
    [
        'locale' => [
            'de_DE', // Default locale
            'en_EN', // Fallback locale
        ],
    ]
);

Setting Translation File Patterns

$translator = Zend\I18n\Translator\Translator::factory(
    [
        'translation_file_patterns' => [
            [
                'type'     => Zend\I18n\Translator\Loader\PhpArray::class,
                'base_dir' => __DIR__ . '/languages',
                'pattern'  => '%s.php',
            ],
            [
                'type'        => Zend\I18n\Translator\Loader\PhpArray::class,
                'base_dir'    => __DIR__ . '/languages',
                'pattern'     => 'album-%s.php',
                'text_domain' => 'album',
            ],
        ],
    ]
);

Each file pattern option array must contain type, base_dir and pattern. The option for text_domain is optional. The default value for text_domain is default.

Setting Translation Files

$translator = Zend\I18n\Translator\Translator::factory(
    [
        'translation_files' => [
            [
                'type'     => Zend\I18n\Translator\Loader\PhpArray::class,
                'filename' => __DIR__ . '/languages/en_EN.php',
            ],
            [
                'type'     => Zend\I18n\Translator\Loader\PhpArray::class,
                'filename' => __DIR__ . '/languages/de_DE.php',
                'locale'   => 'de_DE',
            ],
            [
                'type'        => Zend\I18n\Translator\Loader\PhpArray::class,
                'filename'    => __DIR__ . '/languages/album-de_DE.php',
                'locale'      => 'de_DE',
                'text_domain' => 'album',
            ],
        ],
    ]
);

Each file option array must contain type and filename. The options for locale and the text_domain are optional. The default value for locale is null and for text_domain it is default.

Setting Remote Translations

$translator = Zend\I18n\Translator\Translator::factory(
    [
        'remote_translation' => [
            [
                'type' => 'translation-de_DE', // Custom name
            ],
            [
                'type'        => 'translation-de_DE', // Custom name
                'text_domain' => 'album',
            ],
        ],
    ]
);

Each remote option array must contain type. The option for text_domain is optional. The default value for text_domain is default.

Adding Translations

$translator->getPluginManager()->setService(
    'translation-de_DE', // Custom name
    new \Zend\I18n\Translator\Loader\PhpMemoryArray(
        [
            'default' => [
                'de_DE' => [
                    'car'   => 'Auto',
                    'train' => 'Zug',
                ],
            ],
            'album'   => [
                'de_DE' => [
                    'music' => 'Musik',
                ],
            ],
        ]
    )
);

Setting Cache

Using a Cache Instance

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

Using Cache Configuration

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

Enable EventManager

$translator = Zend\I18n\Translator\Translator::factory(
    [
        'event_manager_enabled' => true,
    ]
);

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