This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New class Translator\Loader\PhpMemoryArray #5825
Merged
weierophinney
merged 5 commits into
zendframework:develop
from
poisa:add/phpMemoryArray
Mar 10, 2014
Merged
New class Translator\Loader\PhpMemoryArray #5825
weierophinney
merged 5 commits into
zendframework:develop
from
poisa:add/phpMemoryArray
Mar 10, 2014
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now 2014
@DASPRiD ping |
Can you show an example how to use this as an end-user? |
@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();
}
} |
@DASPRiD $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
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.