litepie / trans
A modern, production-ready, and well-documented PHP translation package with language negotiation and Laravel integration.
Requires
- php: >=8.1
- illuminate/config: ^9.0|^10.0|^11.0|^12.0
- illuminate/http: ^9.0|^10.0|^11.0|^12.0
- illuminate/routing: ^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
- illuminate/translation: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.5
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0|^10.0|^11.0
This package is auto-updated.
Last update: 2025-08-29 10:37:13 UTC
README
A modern, production-ready, and well-documented PHP translation package with advanced language negotiation, URL localization, route translation, and comprehensive Laravel integration. Now fully compatible with Laravel 12!
β¨ Features
- π Automatic Language Detection - Intelligent locale detection from Accept-Language headers
- π URL-based Locale Switching - Clean, SEO-friendly localized URLs
- π£οΈ Route Translation - Translate route patterns and parameters
- β‘ Performance Optimized - Built-in caching and optimization for Laravel 12
- π§ Middleware Integration - Seamless Laravel 12 middleware support
- π― Type Safe - Full PHP 8.1+ type declarations and strict types
- π± Multi-directional Support - LTR and RTL language support
- πͺ Session & Cookie Support - Remember user language preferences
- π§ͺ Comprehensive Testing - Full test suite included
- π Rich Documentation - Extensive documentation and examples
- π Laravel 12 Ready - Fully compatible with the latest Laravel version
π Installation
Install the package via Composer:
composer require litepie/trans
Laravel Auto-Discovery
The package will automatically register its service provider and aliases.
Manual Registration (if needed)
Add the service provider to your config/app.php
:
'providers' => [ // Other providers... Litepie\Trans\TransServiceProvider::class, ],
Publish Configuration
Publish the configuration file:
php artisan vendor:publish --provider="Litepie\Trans\TransServiceProvider" --tag="trans-config"
βοΈ Configuration
The configuration file config/trans.php
provides extensive customization options:
<?php return [ // Supported locales with metadata 'supportedLocales' => [ 'en' => [ 'name' => 'English', 'script' => 'Latn', 'native' => 'English', 'regional' => 'en_US', 'dir' => 'ltr', ], 'es' => [ 'name' => 'Spanish', 'script' => 'Latn', 'native' => 'EspaΓ±ol', 'regional' => 'es_ES', 'dir' => 'ltr', ], // More locales... ], // Use Accept-Language header for negotiation 'useAcceptLanguageHeader' => true, // Hide default locale in URLs (/en/page becomes /page) 'hideDefaultLocaleInURL' => false, // Auto-detect and redirect to user's preferred locale 'autoDetectLocale' => true, // Additional configuration options... ];
π― Basic Usage
Setting Up Middleware
Add the localization middleware to your routes:
// In routes/web.php Route::group(['middleware' => 'localization'], function () { Route::get('/', 'HomeController@index'); Route::get('/about', 'AboutController@index'); // Your localized routes... });
Using the Trans Service
<?php use Litepie\Trans\Trans; class HomeController extends Controller { protected Trans $trans; public function __construct(Trans $trans) { $this->trans = $trans; } public function index() { // Get current locale $currentLocale = $this->trans->getCurrentLocale(); // 'en' // Get localized URL for different locale $spanishUrl = $this->trans->getLocalizedURL('es'); // Check if multilingual $isMultilingual = $this->trans->isMultilingual(); // true/false // Get all supported locales $locales = $this->trans->getSupportedLocales(); return view('home', compact('currentLocale', 'spanishUrl', 'locales')); } }
Language Switcher in Blade Templates
Create a language switcher component:
{{-- resources/views/components/language-switcher.blade.php --}} @inject('trans', 'Litepie\Trans\Trans') <div class="language-switcher"> <div class="dropdown"> <button class="dropdown-toggle"> {{ $trans->getCurrentLocaleNative() }} <span class="flag flag-{{ $trans->getCurrentLocale() }}"></span> </button> <div class="dropdown-menu"> @foreach($trans->getSupportedLocales() as $code => $locale) @if($code !== $trans->getCurrentLocale()) <a href="{{ $trans->getLocalizedURL($code) }}" class="dropdown-item"> <span class="flag flag-{{ $code }}"></span> {{ $locale['native'] }} </a> @endif @endforeach </div> </div> </div>
π Advanced Features
Route Translation
Define translated routes in your language files:
// resources/lang/en/routes.php return [ 'about' => 'about', 'contact' => 'contact', 'products' => 'products', ]; // resources/lang/es/routes.php return [ 'about' => 'acerca-de', 'contact' => 'contacto', 'products' => 'productos', ];
Register translated routes:
Route::group(['middleware' => 'localization'], function () { Route::get(trans('routes.about'), 'AboutController@index')->name('about'); Route::get(trans('routes.contact'), 'ContactController@index')->name('contact'); });
Custom Language Negotiation
use Litepie\Trans\LanguageNegotiator; $negotiator = new LanguageNegotiator( 'en', // default locale ['en' => [...], 'es' => [...]], // supported locales $request ); $bestLocale = $negotiator->negotiateLanguage();
Locale Detection Priority
The package detects locale in this order:
- URL segment -
/es/page
(highest priority) - Session - Stored user preference
- Cookie - Persistent preference
- Accept-Language header - Browser preference
- Default locale - Fallback (lowest priority)
π§ Middleware Options
LocalizationMiddleware
Automatically handles locale detection and URL redirection:
Route::group(['middleware' => 'localization'], function () { // Your routes });
Custom Middleware Usage
public function handle($request, Closure $next) { // Custom logic before localization $response = $next($request); // Custom logic after localization return $response; }
π API Reference
Trans Class Methods
Locale Management
// Set current locale $trans->setLocale('es'); // Returns: 'es' // Get current locale $trans->getCurrentLocale(); // Returns: 'en' // Get default locale $trans->getDefaultLocale(); // Returns: 'en' // Check if locale is supported $trans->checkLocaleInSupportedLocales('fr'); // Returns: true/false
Locale Information
// Get locale display name $trans->getCurrentLocaleName(); // Returns: 'English' // Get locale native name $trans->getCurrentLocaleNative(); // Returns: 'English' // Get text direction $trans->getCurrentLocaleDirection(); // Returns: 'ltr' or 'rtl' // Get locale script $trans->getCurrentLocaleScript(); // Returns: 'Latn' // Get regional locale $trans->getCurrentLocaleRegional(); // Returns: 'en_US'
URL Generation
// Generate localized URL $trans->getLocalizedURL('es', '/products'); // Returns: 'https://site.com/es/productos' // Generate non-localized URL $trans->getNonLocalizedURL('/es/productos'); // Returns: 'https://site.com/productos' // Get URL from route name $trans->getURLFromRouteNameTranslated('es', 'products');
Utility Methods
// Check if application is multilingual $trans->isMultilingual(); // Returns: true/false // Get all supported locales $trans->getSupportedLocales(); // Returns: array // Get supported locale keys $trans->getSupportedLanguagesKeys(); // Returns: ['en', 'es', 'fr']
LanguageNegotiator Class
// Negotiate best language $negotiator->negotiateLanguage(); // Returns: 'es' // Get supported languages $negotiator->getSupportedLanguages(); // Returns: array // Check if language is supported $negotiator->isLanguageSupported('fr'); // Returns: true/false // Get best match for language $negotiator->getBestMatch('en-US'); // Returns: 'en'
π§ͺ Testing
Run the test suite:
# Run all tests composer test # Run with coverage composer test-coverage # Run specific test ./vendor/bin/phpunit tests/Unit/TransTest.php
Example Test
<?php use Litepie\Trans\Trans; use Litepie\Trans\LanguageNegotiator; class TransTest extends TestCase { public function test_can_set_and_get_locale() { $trans = $this->app->make(Trans::class); $result = $trans->setLocale('es'); $this->assertEquals('es', $result); $this->assertEquals('es', $trans->getCurrentLocale()); } public function test_can_generate_localized_url() { $trans = $this->app->make(Trans::class); $url = $trans->getLocalizedURL('es', '/products'); $this->assertStringContains('/es/', $url); } }
π Security Considerations
- β Input Validation - All locale inputs are validated against supported locales
- β XSS Protection - Output is properly escaped in Blade components
- β CSRF Protection - Compatible with Laravel's CSRF middleware
- β SQL Injection - No direct database queries, uses Laravel's query builder
π Performance Tips
-
Enable Route Caching:
'cacheRouteTranslations' => true, 'routeTranslationCacheTTL' => 60, // minutes
-
Use Laravel's Route Caching:
php artisan route:cache
-
Optimize Config Loading:
php artisan config:cache
π€ Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
Development Setup
# Clone the repository git clone https://github.com/litepie/trans.git # Install dependencies composer install # Run tests composer test # Check code style composer cs-check # Fix code style composer cs-fix
π Changelog
Please see CHANGELOG.md for recent changes.
π‘οΈ Security
If you discover any security-related issues, please email security@litepie.com instead of using the issue tracker.
π License
This package is open-sourced software licensed under the MIT license.
π Credits
- Original Lavalite Team - For the foundation
- Laravel Community - For the amazing framework
- Contributors - All the people who have contributed to this project
π Links
Made with β€οΈ by the Lavalite Team