Skip to content

Commit

Permalink
Add additional tests for context messages
Browse files Browse the repository at this point in the history
Check that context + context-free messages can be reached in an isolated
example. I wasn't able to reproduce the issue reported in #10774
  • Loading branch information
markstory committed Jul 21, 2017
1 parent f36e1ee commit 24fc1d7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
55 changes: 53 additions & 2 deletions tests/TestCase/I18n/Parser/PoFileParserTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\I18n\Parser;

use Aura\Intl\Package;
use Cake\Cache\Cache;
use Cake\I18n\I18n;
use Cake\I18n\Parser\PoFileParser;
use Cake\TestSuite\TestCase;
Expand All @@ -24,6 +25,31 @@
*/
class PoFileParserTest extends TestCase
{
protected $locale;

/**
* Set Up
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->locale = I18n::locale();
}

/**
* Tear down method
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
I18n::clear();
I18n::locale($this->locale);
Cache::clear(false, '_cake_core_');
}

/**
* Tests parsing a file with plurals and message context
Expand Down Expand Up @@ -119,7 +145,7 @@ public function testParseContextOnSomeMessages()
$file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
$messages = $parser->parse($file);

I18n::translator('default', 'de_DE', function () use ($messages) {
I18n::translator('default', 'en_CA', function () use ($messages) {
$package = new Package('default');
$package->setMessages($messages);

Expand All @@ -131,10 +157,35 @@ public function testParseContextOnSomeMessages()
$this->assertSame('En resolved - context', $messages['Resolved']['_context']['Pay status']);

// Confirm actual behavior
I18n::locale('de_DE');
I18n::locale('en_CA');
$this->assertSame('En cours', __('Pending'));
$this->assertSame('En cours - context', __x('Pay status', 'Pending'));
$this->assertSame('En resolved', __('Resolved'));
$this->assertSame('En resolved - context', __x('Pay status', 'Resolved'));
}

/**
* Test parsing context based messages
*
* @return void
*/
public function testParseContextMessages()
{
$parser = new PoFileParser();
$file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
$messages = $parser->parse($file);

I18n::translator('default', 'en_US', function () use ($messages) {
$package = new Package('default');
$package->setMessages($messages);

return $package;
});

// Check translated messages
I18n::locale('en_US');
$this->assertSame('Titel mit Kontext', __x('context', 'title'));
$this->assertSame('Titel mit anderem Kontext', __x('another_context', 'title'));
$this->assertSame('Titel ohne Kontext', __('title'));
}
}
14 changes: 14 additions & 0 deletions tests/test_app/TestApp/Locale/en/context.po
Expand Up @@ -13,3 +13,17 @@ msgstr "En resolved - context"

msgid "Resolved"
msgstr "En resolved"

#: No context
msgid "title"
msgstr "Titel ohne Kontext"

#: Has context
msgctxt "context"
msgid "title"
msgstr "Titel mit Kontext"

#: Another context
msgctxt "another_context"
msgid "title"
msgstr "Titel mit anderem Kontext"

0 comments on commit 24fc1d7

Please sign in to comment.