Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Last active April 8, 2018 02:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save calebporzio/639bf4fbc6e9934694b5a3847bc21905 to your computer and use it in GitHub Desktop.
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
<?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