Skip to content

Commit

Permalink
Default locale mandatory, moved the fallback configuration to the con…
Browse files Browse the repository at this point in the history
…structor
  • Loading branch information
mnapoli committed May 13, 2014
1 parent 57c69aa commit 64ffab5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ A `TranslationHelper` is provided, and integrations with Twig or other systems a
Example:

```php
$translationManager = TranslationManager();
// The current locale is "en"
$translationManager->setCurrentContext('en');
// The default locale is "en"
$translationManager = TranslationManager('en');

// If a user is logged in, we can set the locale to the user's one
$translationManager->setCurrentContext('fr');

$helper = new TranslationHelper($translationManager);

Expand Down Expand Up @@ -204,16 +206,21 @@ However, be aware there are cons:
Everything in this library works around the `TranslationContext`.
It is really simple: **it just contains the current locale**.

For example, if you handle a HTTP request with a 'en_US' locale, then
For example, if you handle a HTTP request with a 'fr_FR' locale, then
you will create a translation context with that locale.

You can then use this context to create the helpers.

You can create a new context and set it as the current context:
You can create a new context (with a default locale):

```php
$manager = new TranslationManager('en');
```

and set the current user's locale:

```php
$manager = new TranslationManager();
$context = $manager->setCurrentContext('en');
$context = $manager->setCurrentContext('fr');
```

A good place to do this would be at the beginning of a HTTP request, so that the current
Expand Down Expand Up @@ -335,11 +342,9 @@ return new UntranslatedString('-');
You can define fallbacks on the `TranslationManager`:

```php
$manager = new TranslationManager();

$manager->setFallbacks([
'fr' => ['en'],
'es' => ['fr', 'en'],
$manager = new TranslationManager('en', [
'fr' => ['en'], // french fallbacks to english if not found
'es' => ['fr', 'en'], // spanish fallbacks to french, then english if not found
]);
```

Expand All @@ -348,7 +353,7 @@ As you can see, fallbacks are optional, and can be multiple.
Once fallbacks are configured, they will be embedded in the `TranslationContext`:

```php
$context = $manager->createContext('es');
$context = $manager->setCurrentContext('es');

var_dump($context->getFallback()); // [ 'fr', 'en' ]
```
Expand Down
34 changes: 24 additions & 10 deletions src/TranslationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,30 @@ public function __construct(TranslationManager $translationManager)
}

/**
* Returns the translation of a TranslatedString using the current locale and the fallback locales.
* Returns the translation of a TranslatedString using the current language and the fallback languages.
*
* @param TranslatedStringInterface $string
*
* @return null|string Returns the string, or null if no translation was found.
*/
public function toString(TranslatedStringInterface $string)
{
$context = $this->translationManager->getCurrentContext();
$context = $this->getCurrentContext();

return $string->get($context->getLocale(), $context->getFallback());
}

/**
* Set the translation for the current locale in a TranslatedString.
* Set the translation for the current language in a TranslatedString.
*
* @param TranslatedStringInterface $string
* @param string $translation
* @param string $translation
*
* @return TranslatedStringInterface Returns $string
*/
public function set(TranslatedStringInterface $string, $translation)
{
$context = $this->translationManager->getCurrentContext();

$locale = $context->getLocale();
$locale = $this->getCurrentContext()->getLocale();

$string->set($translation, $locale);

Expand All @@ -56,16 +54,32 @@ public function set(TranslatedStringInterface $string, $translation)
* Set many translations at once in a TranslatedString.
*
* @param TranslatedStringInterface $string
* @param string[] $translations Must be an array of translations, indexed by the locale.
* @param string[] $translations Must be an array of translations, indexed by the language.
*
* @return TranslatedStringInterface Returns $string
*/
public function setMany(TranslatedStringInterface $string, array $translations)
{
foreach ($translations as $locale => $translation) {
$string->set($translation, $locale);
foreach ($translations as $language => $translation) {
$string->set($translation, $language);
}

return $string;
}

/**
* @return TranslationManager
*/
public function getTranslationManager()
{
return $this->translationManager;
}

/**
* @return TranslationContext
*/
public function getCurrentContext()
{
return $this->translationManager->getCurrentContext();
}
}
15 changes: 9 additions & 6 deletions src/TranslationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ class TranslationManager
private $currentContext;

/**
* Defines a list of fallback locales for each locale.
* Example of fallbacks:
*
* ->setFallbacks([
* [
* 'en' => ['de', 'fr'],
* 'fr' => ['en'],
* 'de' => ['en'],
* ])
* ]
*
* @param array $fallbacks
* @param string $defaultLocale The default locale, to create the default context.
* @param array $fallbacks
*/
public function setFallbacks(array $fallbacks)
public function __construct($defaultLocale, array $fallbacks = [])
{
$this->fallbacks = $fallbacks;

$this->setCurrentContext($defaultLocale);
}

/**
Expand All @@ -53,7 +56,7 @@ public function setCurrentContext($locale)
}

/**
* @return TranslationContext|null
* @return TranslationContext
*/
public function getCurrentContext()
{
Expand Down
24 changes: 9 additions & 15 deletions tests/TranslationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,38 @@ public function testToString()
$str->set('foo', 'en');
$str->set('fou', 'fr');

$manager = new TranslationManager();
$manager->setCurrentContext('en');
$manager = new TranslationManager('en');
$helper = new TranslationHelper($manager);

$this->assertEquals('foo', $helper->toString($str));
}

public function testToStringWithFallback()
{
$manager = new TranslationManager();
$helper = new TranslationHelper($manager);

$str = new TranslatedString();
$str->set('fou', 'fr');

// No fallback
$manager->setCurrentContext('en');
$manager = new TranslationManager('en');
$helper = new TranslationHelper($manager);
$this->assertNull($helper->toString($str));

// One fallback
$manager->setFallbacks(['en' => ['fr']]);
$manager->setCurrentContext('en');
$manager = new TranslationManager('en', ['en' => ['fr']]);
$helper = new TranslationHelper($manager);
$this->assertEquals('fou', $helper->toString($str));

// Two fallbacks
$manager->setFallbacks(['en' => ['de', 'fr']]);
$manager->setCurrentContext('en');
$manager = new TranslationManager('en', ['en' => ['de', 'fr']]);
$helper = new TranslationHelper($manager);
$this->assertEquals('fou', $helper->toString($str));
}

public function testSet()
{
$str = new TranslatedString();

$manager = new TranslationManager();
$manager->setCurrentContext('en');
$manager = new TranslationManager('en');
$helper = new TranslationHelper($manager);

$returned = $helper->set($str, 'foo');
Expand All @@ -66,9 +62,7 @@ public function testSetMany()
{
$str = new TranslatedString();


$manager = new TranslationManager();
$manager->setCurrentContext('en');
$manager = new TranslationManager('en');
$helper = new TranslationHelper($manager);

$returned = $helper->setMany($str, [
Expand Down
4 changes: 1 addition & 3 deletions tests/TranslationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class TranslationManagerTest extends \PHPUnit_Framework_TestCase
{
public function testSetCurrentContext()
{
$manager = new TranslationManager();

$manager->setFallbacks([
$manager = new TranslationManager('en', [
'fr' => ['en'],
]);

Expand Down

0 comments on commit 64ffab5

Please sign in to comment.