Skip to content

Commit 37f6c03

Browse files
committed
feature #9859 [Translation] added method to expose collected message (Grygir)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Translation] added method to expose collected message | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | #2435 | License | MIT | Doc PR | symfony/symfony-docs#3386 Commits ------- ef5d7c5 [Translation] added method to expose collected messages
2 parents 13c75f5 + c9c597a commit 37f6c03

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added relative file path template to the file dumpers
88
* added optional backup to the file dumpers
99
* changed IcuResFileDumper to extend FileDumper
10+
* added Translator::getMessages() for retrieving the message catalogue as an array
1011

1112
2.3.0
1213
-----

Tests/TranslatorTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,120 @@ public function testTransChoiceFallbackWithNoTranslation()
462462
// unchanged if it can't be found
463463
$this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
464464
}
465+
466+
/**
467+
* @dataProvider dataProviderGetMessages
468+
*/
469+
public function testGetMessages($resources, $locale, $expected)
470+
{
471+
$locales = array_keys($resources);
472+
$_locale = !is_null($locale) ? $locale : reset($locales);
473+
$locales = array_slice($locales, 0, array_search($_locale, $locales));
474+
475+
$translator = new Translator($_locale, new MessageSelector());
476+
$translator->setFallbackLocales(array_reverse($locales));
477+
$translator->addLoader('array', new ArrayLoader());
478+
foreach ($resources as $_locale => $domainMessages) {
479+
foreach ($domainMessages as $domain => $messages) {
480+
$translator->addResource('array', $messages, $_locale, $domain);
481+
}
482+
}
483+
$result = $translator->getMessages($locale);
484+
485+
$this->assertEquals($expected, $result);
486+
}
487+
488+
public function dataProviderGetMessages()
489+
{
490+
$resources = array(
491+
'en' => array(
492+
'jsmessages' => array(
493+
'foo' => 'foo (EN)',
494+
'bar' => 'bar (EN)',
495+
),
496+
'messages' => array(
497+
'foo' => 'foo messages (EN)',
498+
),
499+
'validators' => array(
500+
'int' => 'integer (EN)',
501+
),
502+
),
503+
'pt-PT' => array(
504+
'messages' => array(
505+
'foo' => 'foo messages (PT)',
506+
),
507+
'validators' => array(
508+
'str' => 'integer (PT)',
509+
),
510+
),
511+
'pt_BR' => array(
512+
'validators' => array(
513+
'int' => 'integer (BR)',
514+
),
515+
),
516+
);
517+
518+
return array(
519+
array($resources, null,
520+
array(
521+
'jsmessages' => array(
522+
'foo' => 'foo (EN)',
523+
'bar' => 'bar (EN)',
524+
),
525+
'messages' => array(
526+
'foo' => 'foo messages (EN)',
527+
),
528+
'validators' => array(
529+
'int' => 'integer (EN)',
530+
),
531+
),
532+
),
533+
array($resources, 'en',
534+
array(
535+
'jsmessages' => array(
536+
'foo' => 'foo (EN)',
537+
'bar' => 'bar (EN)',
538+
),
539+
'messages' => array(
540+
'foo' => 'foo messages (EN)',
541+
),
542+
'validators' => array(
543+
'int' => 'integer (EN)',
544+
),
545+
),
546+
),
547+
array($resources, 'pt-PT',
548+
array(
549+
'jsmessages' => array(
550+
'foo' => 'foo (EN)',
551+
'bar' => 'bar (EN)',
552+
),
553+
'messages' => array(
554+
'foo' => 'foo messages (PT)',
555+
),
556+
'validators' => array(
557+
'int' => 'integer (EN)',
558+
'str' => 'integer (PT)',
559+
),
560+
),
561+
),
562+
array($resources, 'pt_BR',
563+
array(
564+
'jsmessages' => array(
565+
'foo' => 'foo (EN)',
566+
'bar' => 'bar (EN)',
567+
),
568+
'messages' => array(
569+
'foo' => 'foo messages (PT)',
570+
),
571+
'validators' => array(
572+
'int' => 'integer (BR)',
573+
'str' => 'integer (PT)',
574+
),
575+
),
576+
),
577+
);
578+
}
465579
}
466580

467581
class String

Translator.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,39 @@ protected function getLoaders()
251251
return $this->loaders;
252252
}
253253

254+
/**
255+
* Collects all messages.
256+
*
257+
* Collects all messages for the given locale.
258+
*
259+
* @param string|null $locale Locale of translations, by default is current locale
260+
*
261+
* @return array[array] indexed by catalog.
262+
*/
263+
public function getMessages($locale = null)
264+
{
265+
if (null === $locale) {
266+
$locale = $this->getLocale();
267+
}
268+
269+
if (!isset($this->catalogues[$locale])) {
270+
$this->loadCatalogue($locale);
271+
}
272+
273+
$catalogues = array();
274+
$catalogues[] = $catalogue = $this->catalogues[$locale];
275+
while ($catalogue = $catalogue->getFallbackCatalogue()) {
276+
$catalogues[] = $catalogue;
277+
}
278+
$messages = array();
279+
for ($i = count($catalogues) - 1; $i >= 0; $i--) {
280+
$localeMessages = $catalogues[$i]->all();
281+
$messages = array_replace_recursive($messages, $localeMessages);
282+
}
283+
284+
return $messages;
285+
}
286+
254287
protected function loadCatalogue($locale)
255288
{
256289
try {

0 commit comments

Comments
 (0)