Download the PHP package alexjoffroy/laravel-localization without Composer
On this page you can find all versions of the php package alexjoffroy/laravel-localization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexjoffroy/laravel-localization
More information about alexjoffroy/laravel-localization
Files in alexjoffroy/laravel-localization
Package laravel-localization
Short Description A Laravel package to handle localization with ease
License MIT
Homepage https://github.com/alexjoffroy/laravel-localization
Informations about the package laravel-localization
A Laravel package to handle localization from your routes
This Laravel package gives you the ability to simply handle localization in your application. It provides a set of helpers so you can basically do stuff like:
You can still continue to use Laravel core features such as testing, route caching, lang files, ...
Table of content
- Installation
- Configuration
- Default locale
- Supported locales
- Hide default locale in url
- Usage
- Register the middleware
- Add your routes
- The Localization instance
- Testing
- Changelog
- Contributing
- Security
- Credits
- License
Installation
You can install the package via composer:
This package will automatically register itself.
Optionnaly, you can publish the config file config/localization.php
:
If you want to customize the default switch view:
Configuration
Default locale
The default locale can be changed in the config file. By default, this value is the same as the Laravel fallback_locale
(set in config/app.php
).
Supported locales
You can list all locales you want to support here.
Each key should be a locale code (en, fr, ...).
The native
field is the label which will be rendered in the switch view.
regional_code
, charset
, and constants
fields are required to work with PHP's setlocale, which is called in Localization::setLocale
. This is useful for stuff like date formatting with Carbon.
Hide default locale in url
By default, all URLs will be prefixed with the locale.
When setting hide_default_locale_in_url
to true, this prefix will be removed for the default locale.
Usage
Register the middleware
The first step is to register the SetLocaleFromCurrentRoute
middleware into your app. This middleware will guess and set the current locale from the URL.
The easiest way to do that is to register it globally:
Add your routes
You are now able to register your locales routes, with a convenient helper:
Warning: all routes defined inside the locales
group should be named.
According you are supporting en
and fr
locales and you defined translations for routes.about
, the above code will register these routes:
Verb | URL | Name | Action |
---|---|---|---|
GET HEAD | en/about | en.about | App\Http\Controllers\AboutController@index |
GET HEAD | fr/a-propos | fr.about | App\Http\Controllers\AboutController@index |
The Localization instance
The \AlexJoffroy\Localization\Localization
class provides a set of methods which could be helpful to use in your app. The object is registered as a singleton and can be accessed from the app container, the L10n
facade or the l10n()
helper.
Get/Set locale
Localization::getLocale
and Localization::setLocale
are aliases for App::getLocale
and App::setLocale
Checks
Get config values
Generate routes
Render switch
This should be called in a Blade view like {{ l10n()->renderSwitch() }}
. When using a custom view, you can directly access the Localization instance from $l10n
variable.
Custom view example:
Facade
The Localization methods are also available from the L10n
facade.
Helpers
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Alex Joffroy
- All Contributors
This package was originally build on top of the package skeleton provided by Spatie at spatie/skeleton-php
License
The MIT License (MIT). Please see License File for more information.