lase-peco/localization

A simple localization library

1.2 2022-06-29 13:36 UTC

This package is auto-updated.

Last update: 2024-05-29 05:02:24 UTC


README

Latest Version on Packagist Total Downloads

A simple localization library.

Notes

This whole package is hugely inspired by mcamara/laravel-localization, we wanted something simpler, with support for PHP IntlDateFormatter, so we made our package.

Installation

You can install the package via composer:

composer require lase-peco/localization

Then 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"

Usage

Get current language

Return a string with the current language "en".

Localization::getCurrentLocale() 

Get current language name

Return a string with the name of the current language "English".

Localization::getCurrentLocaleName() 

Get current language native name

Return a string with the native name of the current language "Deutsch".

Localization::getCurrentLocaleNativeName() 

Get current regional language

Return a string with the current regional language "en_GB".

Localization::getCurrentLocaleRegional()

Get keys of supported languages

Return an array of supported languages in your application:

Localization::getSupportedLanguagesKeys() 
//return
[
  0 => "ar"
  1 => "en"
  2 => "de"
]

Get supported languages

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"
  ]
]

Set application language

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.

Time format

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'

Date format

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'

Date-Time format

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'

Flags

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>
@endforeach

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email a.dabak@lase-peco.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.