New class Translator\Loader\PhpMemoryArray #5825

Merged
merged 5 commits into from Mar 10, 2014

Projects

None yet

5 participants

@poisa

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.

@poisa poisa New class Translator\Loader\PhpMemoryArray which loads translations d…
…irectly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does
04e28ef
@samsonasik samsonasik commented on an outdated diff Feb 13, 2014
library/Zend/I18n/Translator/Loader/PhpMemoryArray.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * 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)
@samsonasik samsonasik commented on an outdated diff Feb 13, 2014
...endTest/I18n/Translator/Loader/PhpMemoryArrayTest.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * 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)
@DASPRiD DASPRiD was assigned by Maks3w Feb 15, 2014
@Maks3w
Zend Framework member

@DASPRiD ping

@DASPRiD
Zend Framework member

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

poisa added some commits Feb 16, 2014
@poisa poisa Revert "Updated copyright year to 2005-2014"
This reverts commit b428385.
6a052c6
@poisa poisa Revert "New class Translator\Loader\PhpMemoryArray which loads transl…
…ations directly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does"

This reverts commit 04e28ef.
322b9e0
@poisa poisa New class Translator\Loader\PhpMemoryArray which loads translations d…
…irectly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does
716eb98
@poisa

@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

@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 weierophinney added a commit that referenced this pull request Mar 10, 2014
@weierophinney weierophinney [#5825] Fix import statement
- Bad casing meant class could not load, and tests would not run.
6ad63b2
@weierophinney weierophinney added a commit that referenced this pull request Mar 10, 2014
@weierophinney weierophinney [#5825] Added note to README 5c0d650
@weierophinney weierophinney added a commit that referenced this pull request Mar 10, 2014
@weierophinney weierophinney Merge branch 'feature/5825' into develop
Close #5825
d8e50a8
@weierophinney weierophinney merged commit 716eb98 into zendframework:develop Mar 10, 2014

1 check failed

Details default The Travis CI build could not complete due to an error
@weierophinney weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge pull request zendframework/zendframework#5825 from poisa/add/ph…
…pMemoryArray

New class Translator\Loader\PhpMemoryArray
64d144d
@weierophinney weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
@weierophinney weierophinney [zendframework/zendframework#5825] Fix import statement
- Bad casing meant class could not load, and tests would not run.
47de189
@weierophinney weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge branch 'feature/5825' into develop f8c7771
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment