PHPackages                             arielenter/laravel-test-translations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. arielenter/laravel-test-translations

ActiveLibrary

arielenter/laravel-test-translations
====================================

Easy way to test the existence of translations, placeholders and replace keys.

1.5.4(8mo ago)072GPL-3.0PHP

Since Jul 20Pushed 8mo agoCompare

[ Source](https://github.com/arielenter/laravel-test-translations)[ Packagist](https://packagist.org/packages/arielenter/laravel-test-translations)[ RSS](/packages/arielenter-laravel-test-translations/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (8)Used By (2)

**Laravel’s phpunit intended package to test translations.**
============================================================

[](#laravels-phpunit-intended-package-to-test-translations)

Description
-----------

[](#description)

Easy way to test translation existence along side placeholders and replace keys.

How it works
------------

[](#how-it-works)

Say it is desired to test if a given pice of code (be it a view or a function for example) returns an expected translation back. For instance, let’s say it is expected that a given abstract class’s static method, throws an exception containing an specific translation. We could use the third argument ‘message’ of Laravel’s TestCase ‘assertThrows’ assertion to check if the translation returned matches what we expect as follow:

```
$this->assertThrows(
    fn () => ExampleClass::notTrue(true),
	Exception::class,
    __('error.wrong', [ 'value' => 'true' ])
);
```

This could work well enough. But, what would happen if the key ‘error.wrong’ doesn’t have a translation (be it because it was miss spelled or forgotten)? The test would still pass, but the result is probably not what is desired. Same thing if not all placeholders are satisfied by the given replace argument, or the other way around, if not all the provided replace keys exist as placeholders in the translation due to some oversight.

With this in mind, the perfect solution is to use the following trait method ‘tryGetTrans’ instead of translation function ‘\_\_’ as follow:

```
$this->assertThrows(
    fn () => ExampleClass::notTrue(true),
	Exception::class,
    $this->tryGetTranslation('error.wrong', [ 'value' => 'true' ])
);
```

The trait method ‘tryGetTrans’ will (as I hope is implied) try to get the translation for the given key, failing if translation doesn’t indeed exist for the given key returning an assertion fail. It also assert that that all replace keys are present as placeholders in the translation, and checks if no other placeholders persist after all replace key’s placeholders have been discarted.

Installation
------------

[](#installation)

```
composer require --dev arielenter/laravel-test-translations
```

Usage
-----

[](#usage)

As a trait, and continuing with the example given in the first section ‘How it works’, such scenario would look as follow:

```
