Skip to content

Latest commit

 

History

History
96 lines (77 loc) · 2.58 KB

README.md

File metadata and controls

96 lines (77 loc) · 2.58 KB

zend-i18n-resources

This "component" provides translation resources, specifically for zendframework/zend-validate and zendframework/zend-captcha, for use with zendframework/zend-i18n's Translator subcomponent.

Installation

$ composer require zendframework/zend-i18n-resources

Usage

To use the resources, you need to use the provided Zend\I18n\Translator\Resources class to retrieve the path and pattern to provide to Zend\I18n\Translator\Translator::addTranslationFilePattern():

use Zend\I18n\Translator\Resources;
use Zend\I18n\Translator\Translator;

$translator = new Translator();
$translator->addTranslationFilePattern(
    'phpArray',
    Resources::getBasePath(),
    Resources::getPatternForValidator()
);

echo $translator->translate('Invalid type given. String expected', 'default', 'es');

You can also use the getPatternForCaptcha() method to setup translation messages for zend-captcha:

$translator->addTranslationFilePattern(
    'phpArray',
    Resources::getBasePath(),
    Resources::getPatternForCaptcha()
);

Automating resource injection

If you are using Zend\I18n\Translator\Translator via the zend-servicemanager, you may want to automate injecting the translation messages. This can be done using zend-servicemanager's delegator factories.

As an example, consider the following delegator factory:

use Zend\I18n\Translator\Resources;
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class TranslatorDelegator implements DelegatorFactoryInterface
{
    public function createDelegatorWithName(
        ServiceLocatorInterface $services,
        $name,
        $requestedName,
        $callback
    ) {
        $translator = $callback();
        $translator->addTranslationFilePattern(
            'phpArray',
            Resources::getBasePath(),
            Resources::getPatternForValidator()
        );
        $translator->addTranslationFilePattern(
            'phpArray',
            Resources::getBasePath(),
            Resources::getPatternForCaptcha()
        );
        
        return $translator;
    }
}

You would then register this in your configuration:

return [
    'service_manager' => [
        'delegators' => [
            'MvcTranslator' => [
                'TranslatorDelegator',
            ],
        ],
    ],
];