Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9, add a test for failure. The expected exception need to be fixed #10

Merged
merged 1 commit into from Apr 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 40 additions & 33 deletions README.md
Expand Up @@ -24,7 +24,7 @@ instantiate a translator locator object.

```php
<?php
$translators = include '/path/to/Aura.Http/scripts/instance.php';
$translators = include '/path/to/Aura.Intl/scripts/instance.php';
```

Alternatively, we can add the Aura.Intl package `/path/to/Aura.Intl/src` to
Expand All @@ -40,8 +40,8 @@ use Aura\Intl\TranslatorLocator;
return new TranslatorLocator(
new PackageLocator,
new FormatterLocator([
'basic' => function() { return new Aura\Intl\Formatter\BasicFormatter; },
'intl' => function() { return new Aura\Intl\Formatter\IntlFormatter; },
'basic' => function() { return new Aura\Intl\BasicFormatter; },
'intl' => function() { return new Aura\Intl\IntlFormatter; },
]),
new TranslatorFactory,
'en_US'
Expand All @@ -53,7 +53,7 @@ Setting Localized Messages For A Package

We can set localized messages for a package through the `PackageLocator` object
from the translator locator. We create a new `Package` with messages and place
it into the locator. The messages take the form of a message key and
it into the locator as a callable. The messages take the form of a message key and
and message string.

```php
Expand All @@ -63,25 +63,27 @@ use Aura\Intl\Package;
// get the package locator
$packages = $translators->getPackages();

// create a US English message set
$package = new Package;
$package->setMessages([
'FOO' => 'The text for "foo."';
'BAR' => 'The text for "bar."';
]);

// place into the locator for Vendor.Package
$packages->set('Vendor.Package', 'en_US', $package);

// a Brazilian Portuguese message set
$package = new Package;
$package->setMessages([
'FOO' => 'O texto de "foo".';
'BAR' => 'O texto de "bar".';
]);
$packages->set('Vendor.Package', 'en_US', function() {
// create a US English message set
$package = new Package;
$package->setMessages([
'FOO' => 'The text for "foo."';
'BAR' => 'The text for "bar."';
]);
return $package;
});

// place into the locator for a Vendor.Package
$packages->set('Vendor.Package', 'pt_BR', $package);
$packages->set('Vendor.Package', 'pt_BR', function() {
// a Brazilian Portuguese message set
$package = new Package;
$package->setMessages([
'FOO' => 'O texto de "foo".';
'BAR' => 'O texto de "bar".';
]);
return $package;
});
```


Expand Down Expand Up @@ -129,19 +131,24 @@ message string needs to have a token placeholder for the dynamic value:
// get the packages out of the translator locator
$packages = $translators->getPackages();

// US English messages
$package = new Package;
$package->setMessages([
'PAGE' => 'Page {page} of {pages} pages.';
]);
$packages->set('Vendor.Dynamic', 'en_US', $package);

// Brazilian Portuguese messages
$package = new Package;
$package->setMessages([
'PAGE' => 'Página {page} de {pages} páginas.';
]);
$packages->set('Vendor.Dynamic', 'pt_BR', $package);
$packages->set('Vendor.Dynamic', 'en_US', function() {

// US English messages
$package = new Package;
$package->setMessages([
'PAGE' => 'Page {page} of {pages} pages.';
]);
return $package;
});

$packages->set('Vendor.Dynamic', 'pt_BR', function() {
// Brazilian Portuguese messages
$package = new Package;
$package->setMessages([
'PAGE' => 'Página {page} de {pages} páginas.';
]);
return $package;
});
```

Then, when we translate the message, we provide an array of tokens and
Expand Down
28 changes: 28 additions & 0 deletions tests/Aura/Intl/TranslatorLocatorTest.php
Expand Up @@ -80,4 +80,32 @@ public function testGetFormatterLocator()
$actual = $this->translators->getFormatters();
$this->assertSame($this->formatters, $actual);
}

public function testIssue9()
{
$this->packages->set('Vendor.Package', 'en_UK', function() {
$package = new Package('mock');
$package->setMessages([
'FOO' => 'The text for "foo."',
'BAR' => 'The text for "bar."',
]);
return $package;
});

$translator = $this->translators->get('Vendor.Package', 'en_UK');
$expect = 'The text for "foo."';
$this->assertSame($translator->translate('FOO'), $expect);
}

public function testIssue9Failure()
{
$package = new Package;
$package->setMessages([
'FOO' => 'The text for "foo."',
'BAR' => 'The text for "bar."',
]);
// $this->packages->set('Vendor.Package', 'en_UK', $package);
// $this->setExpectedException('Exception');
// $translator = $this->translators->get('Vendor.Package', 'en_UK');
}
}