Last active
April 8, 2018 02:28
-
-
Save calebporzio/639bf4fbc6e9934694b5a3847bc21905 to your computer and use it in GitHub Desktop.
Throw an error in a testing environment when trans('some-key') doesn't exist
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 Tests; | |
use Illuminate\Translation\Translator; | |
use Laravel\BrowserKitTesting\TestCase as BaseTestCase; | |
abstract class TestCase extends BaseTestCase | |
{ | |
use CreatesApplication; | |
protected function setUp() | |
{ | |
parent::setUp(); | |
$this->strictLocalizationKeys(); | |
} | |
protected function strictLocalizationKeys() | |
{ | |
// if 'some-key' doesn't exist, trans('some-key') will now | |
// throw an error, instead of returning 'some-key'. | |
$this->app->instance('translator', new class( | |
$this->app['translation.loader'], | |
$this->app['config']['app.locale'] | |
) extends Translator { | |
public function get($key, array $replace = [], $locale = null, $fallback = true) | |
{ | |
$output = parent::get($key, $replace, $locale, $fallback); | |
list($namespace, $group, $item) = $this->parseKey($key); | |
// "validation.php" should be exempt from strictness. Otherwise, | |
// validation breaks when you don't define "custom" | |
// validation messaging. | |
if ($key === $output && ! preg_match('/validation/', $group)) { | |
throw new \Exception(sprintf('Unable to find localization key [%s] in language files.', $key)); | |
} | |
return $output; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment