Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef5d7c5

Browse files
committed
[Translation] added method to expose collected messages
1 parentb3b41d5 commitef5d7c5

File tree

3 files changed

+148
-2
lines changed

3 files changed

+148
-2
lines changed

‎src/Symfony/Component/Translation/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
-----

‎src/Symfony/Component/Translation/Tests/TranslatorTest.php‎

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public function testSetFallbackLocalesMultiple()
103103
$this->assertEquals('bar (fr)',$translator->trans('bar'));
104104
}
105105

106-
107106
/**
108107
* @dataProvider getInvalidLocalesTests
109108
* @expectedException \InvalidArgumentException
@@ -329,7 +328,6 @@ public function testTransChoiceValidLocale($locale)
329328
// no assertion. this method just asserts that no exception is thrown
330329
}
331330

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

468580
class String

‎src/Symfony/Component/Translation/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+
publicfunctiongetMessages($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
protectedfunctionloadCatalogue($locale)
255288
{
256289
try {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp