Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
* Add .php_cs

* Add Drivers

* Implement new drivers

* Add tests

* Use --prefer-dist

* Disable XDebug on travis

* Bugfix

* Update .travis.yml

* Fix tests

* Fix tests

* Fix tests

* Fix tests

* Travis add cache

* Optimize Travis cache

* Removed constant from drivers

* Travis prefer-source

* Disable XDebug

* Fix tests

* Fix Travis

* Travis re-enable XDebug

* Travis fix

* Added composer.lock and .gitattributes

* Improved tests

* Improved CookieDriver

* Removed automatic middleware registration

* Improved tests

* Delete LocaleSwitcherTest.php

* Delete composer.lock

* Update .travis.yml

* Update .travis.yml

* Travis min version Laravel 5.2

* Test with Laravel 5.0

* 5.0 not working, using 5.1

* Fixed middleware under 5.1

* Try Laravel 5.0 and PHP 5.5.9

* PHPunit min version is 4.8

* Min version will be Laravel 5.1

* Fix Travis

* Added RouteParameterDriver

* Added storage tests

* Fix tests

* trying out Coveralls

* Add vendor to travis cache

* Fixed tests

* Cleaned up travis

* remove codeclimate

* code style

* Update .travis.yml

* Update .travis.yml

* bugfix

* composer self-update

* Finished the BrowserDriver and its tests

* Fix tests

* Improve Code coverage

* Changed default settings

* Add helpers

* add more tests

* cleanup and update readme

* Add more tests covering locale switching

* Add switch_locale() helper

* update Readme

* request() doesnt exist in Laravel 4.2

* fix tests

* cleanup

* Update README.md
  • Loading branch information
patricksamson committed Apr 4, 2016
1 parent 8749195 commit bc57af9
Show file tree
Hide file tree
Showing 28 changed files with 1,033 additions and 413 deletions.
17 changes: 0 additions & 17 deletions .codeclimate.yml

This file was deleted.

8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
* text=auto

/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpunit.xml export-ignore
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
composer.lock
/vendor
composer.lock
10 changes: 0 additions & 10 deletions .styleci.yml

This file was deleted.

34 changes: 26 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,39 @@ php:
- 7.0
- hhvm

addons:
code_climate:
repo_token: e9d8026b4a1ca0f1c3577c3772bbe9d8d337f0cb2ee50642d2047efa8a148327
cache:
directories:
- vendor
- $HOME/.composer/cache

env:
global:
- setup=basic
- COMPOSER_DISCARD_CHANGES=true
- COMPOSER_NO_INTERACTION=1
- COMPOSER_DISABLE_XDEBUG_WARN=1

matrix:
include:
- php: 5.6
env: setup=lowest
- php: 5.6
env: setup=stable

before_install:
- travis_retry composer self-update
- if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi;
- composer self-update && composer -V

install:
- travis_retry composer install --prefer-source --no-interaction
- if [[ $setup = 'basic' ]]; then travis_retry composer update --prefer-dist; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --prefer-stable --prefer-lowest; fi

script:
- phpunit --coverage-clover build/logs/clover.xml
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- vendor/bin/test-reporter
after_success:
- travis_retry php vendor/bin/coveralls

notifications:
email:
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

A Simple Laravel middleware to easily load and switch the user's locale.
Expand All @@ -21,11 +20,18 @@ Then, add this to your Service Providers :
Lykegenes\LocaleSwitcher\ServiceProvider::class,
```

and add this Alias
and add this Alias [Optional]
``` php
'LocaleSwitcher' => Lykegenes\LocaleSwitcher\Facades\LocaleSwitcher::class,
```

Finally, you will need to register this Middleware either in your `Kernel.php` or directly in your routes.
This way, you can control which routes will have automatic locale detection (Web pages) or not (your API).
Make sure this middleware is placed **after** any Session or Cookie related middlewares from the framework.
``` php
\Lykegenes\LocaleSwitcher\Middleware\SwitchLocaleMiddleware::class,
```

Optionally, you can publish and edit the configuration file :
``` bash
php artisan vendor:publish --provider="Lykegenes\LocaleSwitcher\ServiceProvider" --tag=config
Expand All @@ -43,22 +49,21 @@ http://my-app.com/some/sub/route?locale=es&otherParam=value
```
This will store the locale in the user's session, and set it as the current locale everytime the user requests a page.

You can build the routes like so :
You can build the routes with the included helpers. The URLs will be generated using the current locale.
```php
$url = action('SomeController@someFunction', ['locale' => 'en']);
$url = route('someNamedRoute', ['locale' => 'en']);
$url = url('/some/url', ['locale' => 'en']);
$url = action_localized('SomeController@someFunction', ['your' => 'parameters']);
$url = route_localized('someNamedRoute', ['your' => 'parameters']);
```

You can easily generate a dropdown using the [laravelcollective/html](https://github.com/LaravelCollective/html) package :
To build a URL to the same page, but with a different locale, use the `switch_locale()` helper.
```php
HTML::ul(LocaleSwitcher::getEnabledLocales());
$url = switch_locale('fr'); // URL of the French version of the current page.
```

## Testing

``` bash
composer test
You can easily generate a dropdown using the [laravelcollective/html](https://github.com/LaravelCollective/html) package :
```php
HTML::ul(LocaleSwitcher::getEnabledLocales());
```

## Credits
Expand All @@ -73,12 +78,10 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-version]: https://img.shields.io/packagist/v/lykegenes/laravel-locale-switcher.svg?style=flat-square
[ico-license]: https://img.shields.io/packagist/l/lykegenes/laravel-locale-switcher.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/Lykegenes/laravel-locale-switcher/master.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/lykegenes/laravel-locale-switcher.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/lykegenes/laravel-locale-switcher.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/lykegenes/laravel-locale-switcher
[link-travis]: https://travis-ci.org/Lykegenes/laravel-locale-switcher
[link-code-quality]: https://scrutinizer-ci.com/g/lykegenes/laravel-locale-switcher
[link-downloads]: https://packagist.org/packages/lykegenes/laravel-locale-switcher
[link-author]: https://github.com/lykegenes
[link-contributors]: ../../contributors
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
"require-dev": {
"phpunit/phpunit": "~5.1",
"mockery/mockery": "0.9.*",
"codeclimate/php-test-reporter": "^0.2.0"
"satooshi/php-coveralls": "~1.0",
"orchestra/testbench": "~3.1",
"illuminate/foundation": "^5.1.3"
},
"autoload": {
"psr-4": {
"Lykegenes\\LocaleSwitcher\\": "src/"
}
},
"files": ["src/helpers.php"]
},
"scripts": {
"test": "phpunit"
Expand Down
56 changes: 44 additions & 12 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return array(
/**
return [
/*
*--------------------------------------------------------------------------
* LocaleSwitcher Settings
*--------------------------------------------------------------------------
Expand All @@ -11,21 +11,53 @@
*/
'enabled' => true,

/**
/*
*--------------------------------------------------------------------------
* LocaleSwitcher URL parameter key prefix
* LocaleSwitcher Source Drivers
*--------------------------------------------------------------------------
*
* Sometimes you want to set the URL parameter key to be used by LocaleSwitcher
* to use to detect locale switching requests.
*
* By default, it is "locale", so the URL will be :
* http://my-app.com/some/page/?locale=en
* These are the drivers that will be used to determine the current Locale.
*
* The Drivers will be used in this order, and if no locale is found,
* the Application default locale will be used.
*
*/
'source_drivers' => [
Lykegenes\LocaleSwitcher\Drivers\RouteParameterDriver::class, // Laravel Route parameter
// Lykegenes\LocaleSwitcher\Drivers\RequestDriver::class, // URL query string
// Lykegenes\LocaleSwitcher\Drivers\CookieDriver::class, // Cookie
// Lykegenes\LocaleSwitcher\Drivers\SessionDriver::class, // Laravel Session
// Lykegenes\LocaleSwitcher\Drivers\BrowserDriver::class, // Browser Accept-Language header
],

/*
*--------------------------------------------------------------------------
* LocaleSwitcher Store Driver
*--------------------------------------------------------------------------
*
* This is the driver that will be used to store the locale for future requests.
* It is set to null by default in order to detect the locale on every request.
* Only one can be used!
*
* The included drivers are :
* Lykegenes\LocaleSwitcher\Drivers\SessionDriver::class,
* Lykegenes\LocaleSwitcher\Drivers\CookieDriver::class,
*
*/
'store_driver' => null,

/*
*--------------------------------------------------------------------------
* LocaleSwitcher Driver Key
*--------------------------------------------------------------------------
*
* This key will be used by all the included drivers to detect and store
* the locale across requests.
*
*/
'URL_param_key' => 'locale',
'default_key' => 'locale',

/**
/*
*--------------------------------------------------------------------------
* Enabled Locales
*--------------------------------------------------------------------------
Expand All @@ -39,4 +71,4 @@
'fr' => 'Français',
],

);
];
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<directory>./tests/</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="Integration">
<directory>./tests/Integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">./src/</directory>
Expand Down
32 changes: 17 additions & 15 deletions src/CurrentConfig.php → src/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lykegenes\LocaleSwitcher;

class CurrentConfig
class ConfigManager
{
/**
* Check if this package is enabled or not.
Expand All @@ -14,37 +14,39 @@ public static function isPackageEnabled()
return config('locale-switcher.enabled');
}

/**
* Get the Http Get parameter used to switch locales.
*
* @return string
*/
public static function getUrlParamKey()
public static function getSourceDrivers()
{
return config('locale-switcher.URL_param_key');
return config('locale-switcher.source_drivers', []);
}

public static function getStoreDriver()
{
return config('locale-switcher.store_driver');
}

public static function getDefaultKey()
{
return config('locale-switcher.default_key');
}

/**
* Determine if this locale is enabled or not.
* Depends on the current settings used.
*
* @param string $locale The locale shorthand to verify
* @param string $locale The locale shorthand to verify
*
* @return bool
*/
public static function isEnabledLocale($locale)
{
if (config('locale-switcher.enabled_locales') !== null) {
return true;
}

return array_key_exists($locale, config('locale-switcher.enabled_locales'));
return config('locale-switcher.enabled_locales') !== null
&& array_key_exists($locale, config('locale-switcher.enabled_locales'));
}

/**
* Get the localized name of the specified locale.
*
* @param string $locale The locale shorthand to verify
* @param string $locale The locale shorthand to verify
*
* @return string
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/DriverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Lykegenes\LocaleSwitcher\Contracts;

interface DriverInterface
{
public function has($key);

public function get($key, $default);

public function store($key, $default);
}
26 changes: 26 additions & 0 deletions src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Lykegenes\LocaleSwitcher\Drivers;

use Lykegenes\LocaleSwitcher\Contracts\DriverInterface;

/**
* @codeCoverageIgnore
*/
abstract class BaseDriver implements DriverInterface
{
public function has($key)
{
return false;
}

public function get($key, $default)
{
return false;
}

public function store($key, $default)
{
return false;
}
}
Loading

0 comments on commit bc57af9

Please sign in to comment.