sajjadhossainshohag / laravel-lang-cache
Compile Laravel translation files into a single cached PHP file with php artisan lang:cache.
Package info
github.com/sajjadhossainshohag/laravel-lang-cache
pkg:composer/sajjadhossainshohag/laravel-lang-cache
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0|^12.0|^13.0
- illuminate/filesystem: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- illuminate/translation: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.5|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Cache your Laravel translation files, just like config:cache and route:cache.
Laravel reads and merges your language files on every request. This package compiles them once at deploy time, so loading translations takes about 10 µs, no matter how many you have.
Installation
composer require sajjadhossainshohag/laravel-lang-cache
Requires PHP 8.1+ and Laravel 10–13.
Usage
php artisan lang:cache # build the cache php artisan lang:clear # remove it
On Laravel 11.27.1 and newer, php artisan optimize and optimize:clear run these for you. php artisan about shows whether translations are cached.
Benchmark
Time to load translations per request, for an app with seven locales (PHP 8.4, Laravel 13):
| Keys per locale | Without cache | With cache | Speedup (OPcache) | Speedup (no OPcache) |
|---|---|---|---|---|
| 2,000 | ~545 µs | ~9 µs | ~62× | ~2.6× |
| 10,000 | ~1.55 ms | ~9 µs | ~170× | ~2× |
| 25,000 | ~3.5 ms | ~9 µs | ~380× | ~1.5× |
| 50,000 | ~7.9 ms | ~10 µs | ~830× | ~1.9× |
| 75,000 | ~11.6 ms | ~11 µs | ~1,100× | ~1.5× |
| 100,000 | ~17 ms | ~11 µs | ~1,600× | ~1.6× |
Times are with OPcache enabled. They cover translation loading only, not a full request. Run it yourself:
php -d opcache.enable_cli=1 -d opcache.file_update_protection=0 benchmark/run.php 10000
Good to know
- Re-run
lang:cacheafter editing language files, just likeconfig:cache. - The cache is built with Laravel's own loader, so it returns exactly the same translations, including package and
lang/vendoroverrides. - Each locale gets its own file and is only loaded when used. Without OPcache, a faster serialized copy is used automatically.
- Register translation paths in a service provider, not at runtime, so they're included in the cache.
- Translation files must return plain values (strings and arrays), not closures.
License
MIT. See LICENSE.md.