https://github.com/smknstd/laravel-missing-translation
https://github.com/smknstd/laravel-missing-translation
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/smknstd/laravel-missing-translation
- Owner: smknstd
- License: mit
- Created: 2022-03-18T09:09:16.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-09T12:41:37.000Z (over 2 years ago)
- Last Synced: 2025-01-16T11:32:51.302Z (over 1 year ago)
- Language: PHP
- Size: 68.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README

# Laravel missing translations
[](https://packagist.org/packages/smknstd/laravel-missing-translation)
[](https://github.com/smknstd/laravel-missing-translation/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/smknstd/laravel-missing-translation/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/smknstd/laravel-missing-translation)
Laravel comes with a built-in [localization feature](https://laravel.com/docs/9.x/localization).
But sometimes your translation files doesn't have a requested translation, and even if fallback locale is used
you generally doesn't notice those missing translations and they can be hard to detect.
Using this package and its fallback functionality, you can decide what should happen. The idea of this package is
to setting up a callback function with your custom code that is called everytime a translation key is not found.
This package could be used in development, but also in a production environment.
## Installation
You can install the package via composer:
```bash
composer require smknstd/laravel-missing-translation
```
## Usage
To detect missing translations, you have to swap the Laravel TranslationServiceProvider with a custom provider.
In your `config/app.php`, comment out the original TranslationServiceProvider and add the one from this package:
```php
'providers' => [
...
//'Illuminate\Translation\TranslationServiceProvider',
'Smknstd\LaravelMissingTranslation\TranslationServiceProvider',
]
```
Then to set up the fallback system you need to call static method on the facade `Smknstd\LaravelMissingTranslation\Facades\MissingTranslation`.
Typically, you would put this in a service provider of your own.
You have to register some code you want to run, by passing a closure. It will be used as a callback function and will be
executed everytime a translation key is not found. It lets you execute some custom code like logging something or contact
a remote service for example:
```php
// typically, in a service provider
use Smknstd\LaravelMissingTranslation\Facades\MissingTranslation;
MissingTranslation::fallback(function (
string $translationKey,
string $locale,
?string $fallbackLocale = null,
?string $fallbackTranslation = null,
) {
// do something (ex: logging, alerting, etc)
Log::warning('Some translation key is missing', [
'key' => $translationKey,
'locale' => $locale,
'fallback_locale' => $fallbackLocale,
'fallback_translation' => $fallbackTranslation,
]);
});
```
If the closure returns a string, it will be used as the fallback translation:
```php
// typically, in a service provider
use Smknstd\LaravelMissingTranslation\Facades\MissingTranslation;
use App\Service\MyRemoteTranslationService;
MissingTranslation::fallback(function (
string $translationKey,
string $locale,
string $fallbackLocale,
string $fallbackTranslation,
) {
return MyRemoteTranslationService::getAutomaticTranslation(
$fallbackTranslation,
$fallbackLocale,
$locale
);
});
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Arnaud Becher](https://github.com/smknstd)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.