A simple localization library.
This whole package is hugely inspired by mcamara/laravel-localization, we wanted something simpler, with support for PHP IntlDateFormatter, so we made our package.
You can install the package via composer:
composer require lase-peco/localizationThen in your Web kernel file app/Http/Kernel.php in the web array in $middlewareGroups, add this line to the end of the array:
'web' => [
// Other middlewares
//
\LasePeco\Localization\Http\Middleware\Localization::class,
],Then publish the config file localization.php with the following command to define your application languages:
php artisan vendor:publish --provider="LasePeco\Localization\LocalizationServiceProvider"Return a string with the current language "en".
Localization::getCurrentLocale() Return a string with the name of the current language "English".
Localization::getCurrentLocaleName() Return a string with the native name of the current language "Deutsch".
Localization::getCurrentLocaleNativeName() Return a string with the current regional language "en_GB".
Localization::getCurrentLocaleRegional()Return an array of supported languages in your application:
Localization::getSupportedLanguagesKeys() //return
[
0 => "ar"
1 => "en"
2 => "de"
]Return an associative array with the supported languages for your application:
Localization::getSupportedLocales()//return
[
"en" => [
"direction" => "ltr"
"regional" => "en_GB"
"name" => "English"
"native" => "English"
]
"de" => [
"direction" => "ltr"
"regional" => "de_DE"
"name" => "German"
"native" => "Deutsch"
]
]To set the language of your application use the provided route 'locale' with the selected language as a parameter:
route('locale', [$key]) // $key = "en" or "de" or ...Or make a get request to /local/{$local}, this will set the application language to the selected language.
Localization::formatDate($date) return a string of the date formatted in native Language:
Example
$model->created_at->intlDateFormat();
// or
Localization::formatDate($model->created_at);//return
'Sep 14, 2021' // 'en'
'14.09.2021' // 'de'
'14 sept. 2021' // 'fn'
'١٤/٠٩/٢٠٢١' // 'ar'Localization::formatTime($time) return a string of the time formatted in native Language:
Example
$model->created_at->intlTimeFormat();
// or
Localization::formatTime($model->created_at);//return
'1:27 PM' // 'en'
'13:27' // 'de'
'١:٢٧ م' // 'ar'Localization::formatDateTime($date_time) return a string of the date and time formatted in native Language:
Example
$model->created_at->intlDateTimeFormat();
// or
Localization::formatDateTime($model->created_at);// return
'Sep 14, 2021, 1:27 PM' // 'en'
'14.09.2021, 13:27' // 'de'
'14 sept. 2021, 13:27' // 'fr'
'١٤/٠٩/٢٠٢١, ١:٢٧ م' // 'ar'Localization::getCurrentLocaleFlag() return html string that represents the svg flag.
Localization::getSupportedLocalesFlags() return an array, each item contains a html string that represents the svg flag.
The flags are set to take the full height and width of their parent tag.
Example using tailwindcss!
<div class="inline-block h-4 w-auto mr-2">{!! Localization::getCurrentLocaleFlag() !!}</div>{{Localization::getCurrentLocaleNativeName()}}@foreach(Localization::getSupportedLocales() as $key => $locale)
<a href="{{ route('locale', [$key]) }}"
class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
role="menuitem"><span class="flex items-center"><span class="inline-block h-4 w-auto mr-2">{!! Localization::getSupportedLocalesFlags()[$key] !!}</span>{{$locale['native']}}</span></a>
@endforeachcomposer testPlease see CONTRIBUTING for details.
If you discover any security related issues, please email a.dabak@lase-peco.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.