<commit_before># locale

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

A Laravel package for lightweight route localization. 
This package will set the app locale based on the request. 
E.g. `/nl/foo` will set locale to `nl`. 

## Install

Via Composer

``` bash
$ composer require thinktomorrow/locale
```

Next add the provider to the providers array in the `/config/app.php` file:

``` php
    Thinktomorrow\Locale\LocaleServiceProvider::class,
```

Finally create a configuration file to `/config/thinktomorrow/locale.php`

``` bash
    php artisan vendor:publish --provider="Thinktomorrow\Locale\LocaleServiceProvider"
```

Not required, but if you want to use a facade you can add in the `config/app.php` file as well:

``` php
'aliases' => [
    ...
    'Locale' => 'Thinktomorrow\Locale\LocaleFacade',
    'LocaleUrl' => 'Thinktomorrow\Locale\LocaleUrlFacade',
];
```

## Usage

To make your routes localized, place them inside a Route::group() with a prefix value that is determined by the Locale class itself. 
To avoid possible conflicts with your deployments, you should call the `Thinktomorrow\Locale\Locale` class via the `app()` container instead of the facade inside the `routes.php` file.

``` php
    
    Route::group(['prefix' => app(Thinktomorrow\Locale\Locale::class)->set()],function(){
        
        // Routes registered within this group will be localized
        
    });
    
```
**Note**: *Subdomain- and tld-based localization should be possible as well but this is currently not fully supported yet.*

## Generating a localized url

Localisation of your routes is done automatically when <a href="https://laravel.com/docs/5.2/routing#named-routes" target="_blank">named routes</a> are being used. 
Creation of all named routes will be localized based on current locale. Quick non-obtrusive integration. 

``` php
    route('pages.about'); // prints out http://example.com/en/about (if en is the active locale)
```

To create an url with a specific locale other than the active one, you can use the `Thinktomorrow\Locale\LocaleUrl` class.

``` php
    // Generate localized url from uri (resolves as laravel url() function)
    Thinktomorrow\Locale\LocaleUrl::to('about','en'); // prints out http://example.com/en/about
    
    // Generate localized url from named route (resolves as laravel route() function)
    Thinktomorrow\Locale\LocaleUrl::route('pages.about','en'); // prints out http://example.com/en/about  
```

**Note:** Passing the locale as 'lang' query parameter will force the locale 
*example.com/en/about?lang=nl* makes sure the request will deal with a 'nl' locale.

## Configuration
- **available_locales**: Whitelist of locales available for usage inside your application. 
- **hidden_locale**: You can set one of the available locales as 'hidden' which means any request without a locale in its uri, should be localized as this hidden locale.
For example if the hidden locale is 'nl' and the request uri is /foo/bar, this request is interpreted with the 'nl' locale. 
Note that this is best used for your main / default locale.

## Locale API

#### Set a new locale for current request
``` php
    app('Thinktomorrow\Locale\Locale')->set('en');
```

#### Get the current locale
``` php
    app('Thinktomorrow\Locale\Locale')->get(); // returns 'en' and is basically an alias for app()->getLocale();
```

## Changing locale
This is an example on how you allow an user to change the locale. In this case the route `/lang?locale=en` will
set the new locale to `en` and returns to the user's current page in the new locale.

``` php

// /app/Http/routes.php:
Route::get('lang',['as' => 'lang.switch','uses' => LanguageSwitcher::class.'@store']);

// /app/Http/Controllers/LanguageSwitcher.php:
namespace App\Http\Controllers;

use URL, Illuminate\Http\Request;
use Locale, LocaleUrl;

class LanguageSwitcher extends Controller
{
    public function store(Request $request)
    {
        $locale = $request->get('locale');
        
        // Set new locale
        Locale::set($locale);
        
        // Get current visited page and return to it in the new locale
        $previous = LocaleUrl::to(URL::previous(),Locale::get());
        
        return redirect()->to($previous);
    }
}
``

## Testing`

``` bash
$ vendor/bin/phpunit
```

## Security

If you discover any security related issues, please email <EMAIL> instead of using the issue tracker.

## Credits

- <NAME> <<EMAIL>>

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/thinktomorrow/locale.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/thinktomorrow/locale/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/thinktomorrow/locale.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/thinktomorrow/locale.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/thinktomorrow/locale.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/thinktomorrow/locale
[link-travis]: https://travis-ci.org/thinktomorrow/locale
[link-scrutinizer]: https://scrutinizer-ci.com/g/thinktomorrow/locale/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/thinktomorrow/locale
[link-downloads]: https://packagist.org/packages/thinktomorrow/locale
[link-author]: https://github.com/bencavens<commit_msg>typo in readme<commit_after># locale

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

A Laravel package for lightweight route localization. 
This package will set the app locale based on the request. 
E.g. `/nl/foo` will set locale to `nl`. 

## Install

Via Composer

``` bash
$ composer require thinktomorrow/locale
```

Next add the provider to the providers array in the `/config/app.php` file:

``` php
    Thinktomorrow\Locale\LocaleServiceProvider::class,
```

Finally create a configuration file to `/config/thinktomorrow/locale.php`

``` bash
    php artisan vendor:publish --provider="Thinktomorrow\Locale\LocaleServiceProvider"
```

Not required, but if you want to use a facade you can add in the `config/app.php` file as well:

``` php
'aliases' => [
    ...
    'Locale' => 'Thinktomorrow\Locale\LocaleFacade',
    'LocaleUrl' => 'Thinktomorrow\Locale\LocaleUrlFacade',
];
```

## Usage

To make your routes localized, place them inside a Route::group() with a prefix value that is determined by the Locale class itself. 
To avoid possible conflicts with your deployments, you should call the `Thinktomorrow\Locale\Locale` class via the `app()` container instead of the facade inside the `routes.php` file.

``` php
    
    Route::group(['prefix' => app(Thinktomorrow\Locale\Locale::class)->set()],function(){
        
        // Routes registered within this group will be localized
        
    });
    
```
**Note**: *Subdomain- and tld-based localization should be possible as well but this is currently not fully supported yet.*

## Generating a localized url

Localisation of your routes is done automatically when <a href="https://laravel.com/docs/5.2/routing#named-routes" target="_blank">named routes</a> are being used. 
Creation of all named routes will be localized based on current locale. Quick non-obtrusive integration. 

``` php
    route('pages.about'); // prints out http://example.com/en/about (if en is the active locale)
```

To create an url with a specific locale other than the active one, you can use the `Thinktomorrow\Locale\LocaleUrl` class.

``` php
    // Generate localized url from uri (resolves as laravel url() function)
    Thinktomorrow\Locale\LocaleUrl::to('about','en'); // prints out http://example.com/en/about
    
    // Generate localized url from named route (resolves as laravel route() function)
    Thinktomorrow\Locale\LocaleUrl::route('pages.about','en'); // prints out http://example.com/en/about  
```

**Note:** Passing the locale as 'lang' query parameter will force the locale 
*example.com/en/about?lang=nl* makes sure the request will deal with a 'nl' locale.

## Configuration
- **available_locales**: Whitelist of locales available for usage inside your application. 
- **hidden_locale**: You can set one of the available locales as 'hidden' which means any request without a locale in its uri, should be localized as this hidden locale.
For example if the hidden locale is 'nl' and the request uri is /foo/bar, this request is interpreted with the 'nl' locale. 
Note that this is best used for your main / default locale.

## Locale API

#### Set a new locale for current request
``` php
    app('Thinktomorrow\Locale\Locale')->set('en');
```

#### Get the current locale
``` php
    app('Thinktomorrow\Locale\Locale')->get(); // returns 'en' and is basically an alias for app()->getLocale();
```

## Changing locale
This is an example on how you allow an user to change the locale. In this case the route `/lang?locale=en` will
set the new locale to `en` and returns to the user's current page in the new locale.

``` php

// /app/Http/routes.php:
Route::get('lang',['as' => 'lang.switch','uses' => LanguageSwitcher::class.'@store']);

// /app/Http/Controllers/LanguageSwitcher.php:
namespace App\Http\Controllers;

use URL, Illuminate\Http\Request;
use Locale, LocaleUrl;

class LanguageSwitcher extends Controller
{
    public function store(Request $request)
    {
        $locale = $request->get('locale');
        
        // Set new locale
        Locale::set($locale);
        
        // Get current visited page and return to it in the new locale
        $previous = LocaleUrl::to(URL::previous(),Locale::get());
        
        return redirect()->to($previous);
    }
}
```

## Testing

``` bash
$ vendor/bin/phpunit
```

## Security

If you discover any security related issues, please email <EMAIL> instead of using the issue tracker.

## Credits

- <NAME> <<EMAIL>>

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/thinktomorrow/locale.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/thinktomorrow/locale/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/thinktomorrow/locale.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/thinktomorrow/locale.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/thinktomorrow/locale.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/thinktomorrow/locale
[link-travis]: https://travis-ci.org/thinktomorrow/locale
[link-scrutinizer]: https://scrutinizer-ci.com/g/thinktomorrow/locale/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/thinktomorrow/locale
[link-downloads]: https://packagist.org/packages/thinktomorrow/locale
[link-author]: https://github.com/bencavens
{'fraction_non_alphanumeric': 0.13498836307214895, 'fraction_numerical': 0.000344797862253254, 'mean_word_length': 4.223773075191355, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 39, 'https://': 26, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1902966', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3453, 'n_words': 1069}