Navigation Menu

Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Added translator instance from 'handler' config option
Browse files Browse the repository at this point in the history
  • Loading branch information
shaggyz committed May 9, 2016
1 parent b747568 commit d27b36e
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 18 deletions.
6 changes: 6 additions & 0 deletions phpunit.xml
Expand Up @@ -15,4 +15,10 @@
<directory suffix=".php">./tests/unit/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
30 changes: 15 additions & 15 deletions src/Xinax/LaravelGettext/LaravelGettextServiceProvider.php
Expand Up @@ -10,7 +10,6 @@
* Class LaravelGettextServiceProvider
* @package Xinax\LaravelGettext
*
* TODO: Use providers
*/
class LaravelGettextServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -53,20 +52,21 @@ public function register()

$fileSystem = new FileSystem($configuration->get(), app_path(), storage_path());

// php-gettext mod implementation
/*$translator = new Translators\Gettext(
$configuration->get(),
new Session\SessionHandler($configuration->get()->getSessionIdentifier()),
new Adapters\LaravelAdapter,
$fileSystem
);*/

// symfony translator implementation
$translator = new Translators\Symfony(
$configuration->get(),
new Adapters\LaravelAdapter,
$fileSystem
);
if ('symfony' == $configuration->get()->getHandler()) {
// symfony translator implementation
$translator = new Translators\Symfony(
$configuration->get(),
new Adapters\LaravelAdapter,
$fileSystem
);
} else {
// GNU/Gettext php extension
$translator = new Translators\Gettext(
$configuration->get(),
new Adapters\LaravelAdapter,
$fileSystem
);
}

return new LaravelGettext($translator);
});
Expand Down
6 changes: 3 additions & 3 deletions src/Xinax/LaravelGettext/Translators/Symfony.php
Expand Up @@ -97,10 +97,10 @@ protected function createTranslator()
public function translatePlural($singular, $plural, $amount)
{
return $this->symfonyTranslator->transChoice(
// Symfony translator looks for 'singular|plural' message, and obviously doesn't exists in catalog
// @see https://github.com/symfony/symfony/issues/10152
// Symfony translator looks for 'singular|plural' message id in catalog,
// and obviously doesn't exists, so always the fallback string will be returned.
// $singular . '|' . $plural, //<-- this just doesn't works, idk wtf is wrong.
$amount >1 ? $plural : $singular,
// $singular . '|' . $plural, <-- this just doesn't works, idk wtf is wrong.
$amount,
['%count%' => $amount],
$this->getDomain(),
Expand Down
92 changes: 92 additions & 0 deletions tests/unit/LaravelGettextTest.php
@@ -0,0 +1,92 @@
<?php

namespace Xinax\LaravelGettext\Test;

use \Mockery as m;
use \Xinax\LaravelGettext\LaravelGettext;
use \Xinax\LaravelGettext\Config\ConfigManager;
use \Xinax\LaravelGettext\Adapters\LaravelAdapter;
use \Xinax\LaravelGettext\FileSystem;
use Xinax\LaravelGettext\Translators\Symfony;

class LaravelGettextTest extends BaseTestCase
{
/**
* Gettext wrapper
* @var \Xinax\LaravelGettext\Gettext
*/
protected $gettext;

public function setUp()
{
parent::setUp();

$config = ConfigManager::create();
$adapter = new LaravelAdapter;

$fileSystem = new FileSystem($config->get(), app_path(), storage_path());

$translator = new Symfony(
$config->get(),
$adapter,
$fileSystem
);

$this->gettext = $translator;
}

/**
* Test setting locale.
*/
public function testSetLocale()
{
$response = $this->gettext->setLocale('en_US');

$this->assertEquals('en_US', $response);
}

/**
* Test getting locale.
* It should receive locale from mocked config.
*/
public function testGetLocale()
{
$response = $this->gettext->getLocale();

$this->assertEquals('en_US', $response);
}

public function testIsLocaleSupported()
{
$this->assertTrue($this->gettext->isLocaleSupported('en_US'));
}

/**
* Test dumping locale to string
*/
public function testToString()
{
$response = $this->gettext->__toString();

$this->assertEquals('en_US', $response);
}

public function testGetEncoding()
{
$response = $this->gettext->getEncoding();
$this->assertNotEmpty($response);
$this->assertEquals('UTF-8', $response);
}

public function testSetEncoding()
{
$response = $this->gettext->setEncoding('UTF-8');
$this->assertNotEmpty($response);
$this->assertInstanceOf('Xinax\LaravelGettext\Gettext', $response);
}

public function tearDown()
{
m::close();
}
}

0 comments on commit d27b36e

Please sign in to comment.