https://github.com/lase-peco/localization
A simple localization library for Laravel
https://github.com/lase-peco/localization
flags internationalization language laravel localization php
Last synced: 3 months ago
JSON representation
A simple localization library for Laravel
- Host: GitHub
- URL: https://github.com/lase-peco/localization
- Owner: lase-peco
- License: mit
- Created: 2021-03-30T11:32:21.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-12-15T16:22:32.000Z (over 1 year ago)
- Last Synced: 2025-12-14T22:57:41.922Z (6 months ago)
- Topics: flags, internationalization, language, laravel, localization, php
- Language: PHP
- Homepage: https://lase-peco.com
- Size: 207 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# A simple localization library
[](https://packagist.org/packages/lase-peco/localization)
[](https://packagist.org/packages/lase-peco/localization)
[comment]: <> ([](https://travis-ci.org/lase-peco/localization))
[comment]: <> ([](https://scrutinizer-ci.com/g/lase-peco/localization))
A simple localization library.
## Notes
This whole package is hugely inspired by [mcamara/laravel-localization](https://github.com/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:
```bash
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:
```php
'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
php artisan vendor:publish --provider="LasePeco\Localization\LocalizationServiceProvider"
```
## Usage
### Get current language
Return a string with the current language `"en"`.
``` php
Localization::getCurrentLocale()
```
### Get current language name
Return a string with the name of the current language `"English"`.
``` php
Localization::getCurrentLocaleName()
```
### Get current language native name
Return a string with the native name of the current language `"Deutsch"`.
``` php
Localization::getCurrentLocaleNativeName()
```
### Get current regional language
Return a string with the current regional language `"en_GB"`.
``` php
Localization::getCurrentLocaleRegional()
```
### Get keys of supported languages
Return an array of supported languages in your application:
``` php
Localization::getSupportedLanguagesKeys()
```
``` php
//return
[
0 => "ar"
1 => "en"
2 => "de"
]
```
### Get supported languages
Return an associative array with the supported languages for your application:
``` php
Localization::getSupportedLocales()
```
``` php
//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:
``` php
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
``` php
$model->created_at->intlDateFormat();
// or
Localization::formatDate($model->created_at);
```
``` php
//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
``` php
$model->created_at->intlTimeFormat();
// or
Localization::formatTime($model->created_at);
```
``` php
//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
``` php
$model->created_at->intlDateTimeFormat();
// or
Localization::formatDateTime($model->created_at);
```
``` php
// 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!
``` html
{!! Localization::getCurrentLocaleFlag() !!}{{Localization::getCurrentLocaleNativeName()}}
```
``` html
@foreach(Localization::getSupportedLocales() as $key => $locale)
{!! Localization::getSupportedLocalesFlags()[$key] !!}{{$locale['native']}}
@endforeach
```
### Testing
``` bash
composer test
```
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
### Security
If you discover any security related issues, please email a.dabak@lase-peco.com instead of using the issue tracker.
## Credits
- [Ahmed Dabak](https://github.com/lase-peco)
- [Abdulsalam Emesh](https://github.com/lase-peco)
- [All Contributors](CONTRIBUTING.md)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.