Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

New class Translator\Loader\PhpMemoryArray #5825

Merged
merged 5 commits into from
Mar 10, 2014

Conversation

poisa
Copy link
Contributor

@poisa poisa commented Feb 13, 2014

This translator loader takes a php array that already exists instead of loading it from a file like Translator\Loader\PhpArray does. This is beneficial when you already have the translations in memory or when using a service like Google App Engine where disk I/O is an expensive API call. It is also useful when translations come from a centralized place like memcached which is what prompted me to write this loader.

…irectly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now 2014

@Maks3w
Copy link
Member

Maks3w commented Feb 15, 2014

@DASPRiD ping

@DASPRiD
Copy link
Member

DASPRiD commented Feb 15, 2014

Can you show an example how to use this as an end-user?

…ations directly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does"

This reverts commit 04e28ef.
…irectly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does
@poisa
Copy link
Contributor Author

poisa commented Feb 16, 2014

@DASPRiD As I was writing the use case example I realized I had pushed from the wrong local branch (oops). I reverted both commits and recommited the clean versions.

Here's one way of using it. I don't know how to "automate" getting messages off the config though.

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\I18n\Translator\Translator;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $sm = $this->getServiceLocator();

        $translator = Translator::factory(array(
            'event_manager_enabled' => true,
            'locale'                => 'en_US',
            'remote_translation'    => array(
                array(
                    'type'        => 'Zend\I18n\Translator\Loader\PhpMemoryArray',
                ),
            ),
        ));

        $plugins = $translator->getPluginManager();
        $plugins->setServiceLocator($sm);

        $config = new \Zend\ServiceManager\Config(array(
            'factories' => array(
                'Zend\I18n\Translator\Loader\PhpMemoryArray' => function($sm) {
                        $messages = array(
                            'default' => array(
                                'en_US' => array(
                                    '' => array(
                                        'plural_forms' => 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);'
                                    ),
                                    'Message 1' => 'Message 1 (en)',
                                    'Message 2' => 'Message 2 (en)',
                                    'Message 3' => 'Message 3 (en)',
                                    'Message 4' => 'Message 4 (en)',
                                    'Message 5' => array(
                                        0 => 'Message 5 (en) Plural 0',
                                        1 => 'Message 5 (en) Plural 1',
                                        2 => 'Message 5 (en) Plural 2'
                                    ),
                                    'Cooking furniture' => 'Küchen Möbel (en)',
                                    'Küchen Möbel' => 'Cooking furniture (en)',
                                ),
                            ),
                        );
                        $instance = new \Zend\I18n\Translator\Loader\PhpMemoryArray($messages);
                        return $instance;
                    }
            ),
        ));
        $config->configureServiceManager($plugins);

        var_dump($translator->translate('Message 1'));
        var_dump($translator->translate('Cooking furniture'));
        die();
    }
}

@poisa
Copy link
Contributor Author

poisa commented Feb 17, 2014

@DASPRiD
Just thought I'd clean up the use case a little to make it lees convoluted. It would be really cool if I could pass the $messages directly to the factory but I don't know how to do that since the RemoteLoaderInterface only gets the $locale and $textDomain.

$translator = Translator::factory(array(
    'event_manager_enabled' => true,
    'locale'                => 'en_US',
    'remote_translation'    => array(
        array(
            'type'        => 'phpmemoryarray',
        ),
    ),
));

$messages = array(
    'default' => array(
        'en_US' => array(
            '' => array(
                'plural_forms' => 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);'
            ),
            'Message 1' => 'Message 1 (en)',
            'Message 2' => 'Message 2 (en)',
            'Message 3' => 'Message 3 (en)',
            'Message 4' => 'Message 4 (en)',
            'Message 5' => array(
                0 => 'Message 5 (en) Plural 0',
                1 => 'Message 5 (en) Plural 1',
                2 => 'Message 5 (en) Plural 2'
            ),
            'Cooking furniture' => 'Küchen Möbel (en)',
            'Küchen Möbel' => 'Cooking furniture (en)',
        ),
    ),
);
$loader = new \Zend\I18n\Translator\Loader\PhpMemoryArray($messages);

$translator->getPluginManager()->setService('phpmemoryarray', $loader);

var_dump($translator->translate('Message 1'));
var_dump($translator->translate('Cooking furniture'));

@weierophinney weierophinney added this to the 2.3.0 milestone Mar 3, 2014
weierophinney added a commit that referenced this pull request Mar 10, 2014
New class Translator\Loader\PhpMemoryArray
weierophinney added a commit that referenced this pull request Mar 10, 2014
- Bad casing meant class could not load, and tests would not run.
weierophinney added a commit that referenced this pull request Mar 10, 2014
weierophinney added a commit that referenced this pull request Mar 10, 2014
@weierophinney weierophinney merged commit 716eb98 into zendframework:develop Mar 10, 2014
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
…pMemoryArray

New class Translator\Loader\PhpMemoryArray
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
- Bad casing meant class could not load, and tests would not run.
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants