https://github.com/michaelnabil230/laravel-multi-language
Laravel Multi Language
https://github.com/michaelnabil230/laravel-multi-language
language laravel localization multi-language php
Last synced: about 1 month ago
JSON representation
Laravel Multi Language
- Host: GitHub
- URL: https://github.com/michaelnabil230/laravel-multi-language
- Owner: michaelnabil230
- License: mit
- Created: 2025-02-25T14:16:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-01-12T01:46:26.000Z (5 months ago)
- Last Synced: 2026-01-12T05:59:46.911Z (5 months ago)
- Topics: language, laravel, localization, multi-language, php
- Language: PHP
- Homepage:
- Size: 238 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Multi Language
[](https://packagist.org/packages/michaelnabil230/laravel-multi-language)
[](https://github.com/michaelnabil230/laravel-multi-language/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/michaelnabil230/laravel-multi-language/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/michaelnabil230/laravel-multi-language)
A Laravel package for seamless multi-language support, enabling localized routing and language management.
## Installation
You can install the package via composer:
```bash
composer require michaelnabil230/laravel-multi-language
```
You can publish the config file with:
```bash
php artisan vendor:publish --tag="multi-language-config"
```
### Middleware Setup
Register the middleware in `bootstrap/app.php`:
```php
return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'localize' => \MichaelNabil230\LaravelMultiLanguage\Http\Middleware\SetLocale::class,
]);
});
```
## Usage
### Routing Setup
```php
// routes/web.php
Route::prefix(LaravelMultiLanguage::prefix())
->middleware('localize')
->group(function () {
// Your routes here
});
```
Example URLs:
- English: `http://yoursite.com/en`, `http://yoursite.com/en/test`
- Arabic: `http://yoursite.com/ar`, `http://yoursite.com/ar/test`
- Auto-detect: `http://yoursite.com` (redirects to default locale)
(Due to technical issues, the search service is temporarily unavailable.)
Here's the updated Helpers section converted to bullet points:
### Helper Methods
- **Get Localized URL**
Generate URL for a specific locale:
```php
{{ LaravelMultiLanguage::getLocalizedURL('en') }}
// Output: http://yoursite.com/en/current-route
```
- **Get Supported Locales**
Retrieve all supported locales with their properties:
```php
{{ LaravelMultiLanguage::getSupportedLocales() }}
/* Output: [
'en' => ['name' => 'English', 'native' => 'English', ...],
'ar' => ['name' => 'Arabic', 'native' => 'عربى', ...]
] */
```
- **Get Supported Locale Keys**
Get array of supported language codes:
```php
{{ LaravelMultiLanguage::getSupportedLanguagesKeys() }}
// Output: ['en', 'ar']
```
- **Get Current Locale**
Retrieve current locale code:
```php
{{ LaravelMultiLanguage::getCurrentLocale() }}
// Output: en
```
- **Get Current Locale Name**
Get English name of current locale:
```php
{{ LaravelMultiLanguage::getCurrentLocaleName() }}
// Output: English
```
- **Get Current Locale Native Name**
Get native language name:
```php
{{ LaravelMultiLanguage::getCurrentLocaleNative() }}
// Output: English (for EN) or عربى (for AR)
```
- **Get Current Locale Direction**
Get text direction (ltr/rtl):
```php
{{ LaravelMultiLanguage::getCurrentLocaleDirection() }}
// Output: ltr or rtl
```
- **Get Current Locale Script**
Get ISO 15924 script code:
```php
{{ LaravelMultiLanguage::getCurrentLocaleScript() }}
// Output: Latn (for Latin script)
```
*For ISO 15924 details, see [Unicode's documentation](http://www.unicode.org/iso15924).*
### Language Selector Example
```blade
-
{{ $properties['native'] }}
@foreach(LaravelMultiLanguage::getSupportedLocales() as $localeCode => $properties)
@endforeach
```
### Listeners Event
If you want to listen to the `SetLocaleEvent` event, you can use the following code:
```php
use MichaelNabil230\LaravelMultiLanguage\Events\SetLocaleEvent;
use Illuminate\Support\Facades\Event;
Event::listen(SetLocaleEvent::class, function (SetLocaleEvent $event) {
$locale = $event->locale;
});
```
## Advanced Configuration
### Route Caching
Cache routes using:
```bash
php artisan route:trans:cache
```
Update `AppServiceProvider.php`:
```php
use MichaelNabil230\LaravelMultiLanguage\Traits\LoadsTranslatedCachedRoutes;
class AppServiceProvider extends ServiceProvider
{
use LoadsTranslatedCachedRoutes;
public function boot(): void
{
RouteServiceProvider::loadCachedRoutesUsing(fn () => $this->loadCachedRoutes());
}
}
```
### Testing Setup
Add to your `TestCase.php`:
```php
use MichaelNabil230\LaravelMultiLanguage\LaravelMultiLanguage;
protected function setUp(): void
{
$this->setApplicationLocale('en');
parent::setUp();
}
protected function tearDown(): void
{
putenv(Localization::ENV_ROUTE_KEY);
parent::tearDown();
}
protected function setApplicationLocale(string $locale): void
{
putenv(Localization::ENV_ROUTE_KEY . '=' . $locale);
}
```
## Support
[](https://github.com/sponsors/michaelnabil230)
[](https://ko-fi.com/michaelnabil230)[](https://www.buymeacoffee.com/michaelnabil230)[](https://www.paypal.com/paypalme/MichaelNabil23)
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Michael Nabil](https://github.com/michaelnabil230)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.