Skip to content

Commit

Permalink
Merge pull request #60 from ARCANEDEV/patch-1
Browse files Browse the repository at this point in the history
Fixing the translation issue
  • Loading branch information
arcanedev-maroc committed Apr 19, 2016
2 parents 78e1de8 + 75e142c commit 257cf89
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 22 deletions.
11 changes: 11 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ function log_viewer() {
}
}

if ( ! function_exists('log_levels')) {
/**
* Get the LogLevels instance.
*
* @return \Arcanedev\LogViewer\Contracts\LogLevelsInterface
*/
function log_levels() {
return app('arcanedev.log-viewer.levels');
}
}

if ( ! function_exists('log_menu')) {
/**
* Get the LogMenu instance.
Expand Down
2 changes: 1 addition & 1 deletion src/Bases/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class Table implements TableInterface
public function __construct(array $data, LogLevelsInterface $levels, $locale = null)
{
$this->setLevels($levels);
$this->setLocale($locale);
$this->setLocale(is_null($locale) ? config('log-viewer.locale') : $locale);
$this->setData($data);
$this->init();
}
Expand Down
26 changes: 26 additions & 0 deletions src/Contracts/LogLevelsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ interface LogLevelsInterface
*/
public function setTranslator(Translator $translator);

/**
* Get the selected locale.
*
* @return string
*/
public function getLocale();

/**
* Set the selected locale.
*
* @param string $locale
*
* @return self
*/
public function setLocale($locale);

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -53,4 +69,14 @@ public function names($locale = null);
* @return array
*/
public static function all($flip = false);

/**
* Get the translated level.
*
* @param string $key
* @param string|null $locale
*
* @return string
*/
public function get($key, $locale = null);
}
2 changes: 1 addition & 1 deletion src/Entities/LogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function level()
*/
public function name()
{
return trans('log-viewer::levels.' . $this->level);
return log_levels()->get($this->level);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/LogEntryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function tree($trans = false)

array_walk($tree, function(&$count, $level) use ($trans) {
$count = [
'name' => $trans ? trans("log-viewer::levels.$level") : $level,
'name' => $trans ? log_levels()->get($level) : $level,
'count' => $count,
];
});
Expand Down
19 changes: 13 additions & 6 deletions src/Providers/UtilitiesServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Arcanedev\LogViewer\Providers;

use Arcanedev\LogViewer\Utilities\Filesystem;
use Arcanedev\LogViewer\Utilities\LogLevels;
use Arcanedev\Support\ServiceProvider;

/**
Expand Down Expand Up @@ -60,10 +61,16 @@ public function provides()
*/
private function registerLogLevels()
{
$this->singleton(
'arcanedev.log-viewer.levels',
'Arcanedev\LogViewer\Utilities\LogLevels'
);
$this->singleton('arcanedev.log-viewer.levels', function ($app) {
/**
* @var \Illuminate\Config\Repository $config
* @var \Illuminate\Translation\Translator $translator
*/
$config = $app['config'];
$translator = $app['translator'];

return new LogLevels($translator, $config->get('log-viewer.locale'));
});

$this->bind(
'Arcanedev\\LogViewer\\Contracts\\LogLevelsInterface',
Expand Down Expand Up @@ -110,11 +117,11 @@ private function registerFilesystem()
{
$this->singleton('arcanedev.log-viewer.filesystem', function ($app) {
/**
* @var \Illuminate\Filesystem\Filesystem $files
* @var \Illuminate\Config\Repository $config
* @var \Illuminate\Filesystem\Filesystem $files
*/
$files = $app['files'];
$config = $app['config'];
$files = $app['files'];

return new Filesystem($files, $config->get('log-viewer.storage-path'));
});
Expand Down
55 changes: 43 additions & 12 deletions src/Utilities/LogLevels.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class LogLevels implements LogLevelsInterface
*/
private $translator;

/**
* The selected locale.
*
* @var string
*/
private $locale;

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
Expand All @@ -39,10 +46,12 @@ class LogLevels implements LogLevelsInterface
* Create LogLevels instance.
*
* @param \Illuminate\Translation\Translator $translator
* @param string $locale
*/
public function __construct(Translator $translator)
public function __construct(Translator $translator, $locale)
{
$this->setTranslator($translator);
$this->setLocale($locale);
}

/* ------------------------------------------------------------------------------------------------
Expand All @@ -63,6 +72,32 @@ public function setTranslator(Translator $translator)
return $this;
}

/**
* Get the selected locale.
*
* @return string
*/
public function getLocale()
{
return $this->locale === 'auto'
? $this->translator->getLocale()
: $this->locale;
}

/**
* Set the selected locale.
*
* @param string $locale
*
* @return self
*/
public function setLocale($locale)
{
$this->locale = is_null($locale) ? 'auto' : $locale;

return $this;
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -91,7 +126,7 @@ public function names($locale = null)
$levels = self::all(true);

array_walk($levels, function (&$name, $level) use ($locale) {
$name = $this->getTranslatedName($level, $locale);
$name = $this->get($level, $locale);
});

return $levels;
Expand All @@ -114,24 +149,20 @@ public static function all($flip = false)
return $flip ? array_flip(self::$levels) : self::$levels;
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Translate a level.
* Get the translated level.
*
* @param string $level
* @param string $key
* @param string|null $locale
*
* @return string
*/
private function getTranslatedName($level, $locale)
public function get($key, $locale = null)
{
if ($locale === 'auto') {
$locale = null;
if (is_null($locale) || $locale === 'auto') {
$locale = $this->getLocale();
}

return $this->translator->get('log-viewer::levels.' . $level, [], $locale);
return $this->translator->get("log-viewer::levels.$key", [], $locale);
}
}
18 changes: 17 additions & 1 deletion tests/Utilities/LogLevelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LogLevelsTest extends TestCase
| Properties
| ------------------------------------------------------------------------------------------------
*/
/** @var LogLevels */
/** @var \Arcanedev\LogViewer\Utilities\LogLevels */
private $levels;

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -73,6 +73,18 @@ public function it_can_get_all_translated_levels()
}
}

/** @test */
public function it_must_choose_the_log_viewer_locale_instead_of_app_locale()
{
$this->assertNotEquals('auto', $this->levels->getLocale());
$this->assertEquals($this->app->getLocale(), $this->levels->getLocale());

$this->levels->setLocale('fr');

$this->assertEquals('fr', $this->levels->getLocale());
$this->assertNotEquals($this->app->getLocale(), $this->levels->getLocale());
}

/** @test */
public function it_can_translate_levels_automatically()
{
Expand All @@ -88,6 +100,10 @@ public function it_can_translate_levels_automatically()
$this->app->getLocale(),
$this->levels->names('auto')
);

$this->assertTranslatedLevels(
$locale, $this->levels->names($locale)
);
}
}
}

0 comments on commit 257cf89

Please sign in to comment.