Last active
February 13, 2020 17:09
-
-
Save RobinRadic/062994797177590663e65aa48a75429a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Anomaly\Streams\Platform\Support; | |
/** | |
* Class Translator | |
* | |
* @link http://pyrocms.com/ | |
* @author PyroCMS, Inc. <[email protected]> | |
* @author Ryan Thompson <[email protected]> | |
*/ | |
class Translator | |
{ | |
/** @var \Illuminate\Contracts\Translation\Translator */ | |
protected $translator; | |
public function __construct(\Illuminate\Contracts\Translation\Translator $translator) | |
{ | |
$this->translator = $translator; | |
} | |
/** | |
* Translate a target array. | |
* | |
* @param array $target | |
* @return array | |
*/ | |
public function translate($target) | |
{ | |
if (is_string($target)) { | |
return $this->translator->get($target); | |
} | |
if (is_array($target)) { | |
foreach ($target as &$value) { | |
if (is_string($value) && $this->translator->has($value)) { | |
if (is_string($translated = $this->translator->get($value))) { | |
$value = $translated; | |
} | |
} elseif (is_array($value)) { | |
$value = $this->translate($value); | |
} | |
} | |
} | |
return $target; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Mockery as m; | |
class TranslatorTest extends \PHPUnit\Framework\TestCase | |
{ | |
public function testTranslateString() | |
{ | |
$translator = new \Anomaly\Streams\Platform\Support\Translator($mock = m::mock(\Illuminate\Contracts\Translation\Translator::class)); | |
$mock->shouldReceive('get')->withArgs(['asdfasdf'])->andReturn('foo'); | |
$translated = $translator->translate('asdfasdf'); | |
$this->assertEquals('foo', $translated); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment