Skip to content

Commit

Permalink
Merge pull request #4 from zagovorichev/release-1.0.0
Browse files Browse the repository at this point in the history
Language manager v 1.0.0
  • Loading branch information
zagovorichev authored Oct 28, 2016
2 parents b62b35c + 3f8e35b commit 2e5ed1f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 70 deletions.
11 changes: 9 additions & 2 deletions config/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
| # example: http://www.example.com/en/post/234
|
*/
'pathRegExp' => '{\w+}\/.*',
'pathRegExp' => [
'reg' => '|([a-z]{2})(/.*)|ui',
'langPart' => 1,
],

/*
|-----------------------------------------------------------------
Expand All @@ -76,6 +79,10 @@
| # example: http://www.example.en/post/234 => http://en.example.com/post/234
|
*/
'domainRegExp' => 'http://{\w+}.example.com',
'domainRegExp' => [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'langPart' => 2,
'domainSeparator' => '.',
]

];
39 changes: 11 additions & 28 deletions src/Http/Middleware/LanguagesMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,33 @@
* @author A.Zagovorichev <zagovorichev@gmail.com>
*/

namespace zagovorichev\laravel\languages\Http\Middleware;
namespace Zagovorichev\Laravel\Languages\Http\Middleware;


use Closure;
use Illuminate\Config\Repository;
use Zagovorichev\Laravel\Languages\LanguageManager;

class LanguagesMiddleware
{
/**
* Handle an incoming request.
*
* @param Request $request+
* @param \Closure $next
* @param Request $request +
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{

config('languages');

/*$currentLang == $this->getLanguage();
session()->put('lang', $lang);
cookie('lang', $lang);
\App::setLocale($lang);
$path = $request->path();
$params = $request->except(['lang']);
if (count($params)) {
$path .= '?' . http_build_query($params);
}
return redirect($path);*/

/* protected function redirect($path='')
{
if (!function_exists('redirect')) {
throw new LanguageManagerException('Function redirect() does not exists, can\'t go to the path ' . $path);
$languageManager = new LanguageManager(new Repository(config('languages')));
if ($languageManager->isOtherLanguage()) {
return redirect($languageManager->getRedirectPath());
}

if (!empty($path) && function_exists('redirect')) {
redirect($path);
if ($languageManager->has()) {
\App::setLocale($languageManager->get());
}
}*/

return $next($request);
}
}
29 changes: 28 additions & 1 deletion src/LanguageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ public function __construct(Repository $config)
}
}

public function isOtherLanguage()
{
$isOther = false;

$lang = $this->get();
//in request input
if ($this->getManager('request')->has()
&& $this->filterLang($this->getManager('request')->get())) {

$lang = $this->filterLang($this->getManager('request')->get());
$isOther = true;
}

// if user go to the different domain we should use that language
if (in_array('domain', $this->modes) && $this->getManager('domain')->get() !== $this->get()) {

$lang = $this->getManager('domain')->get();
$isOther = true;
}

if ($isOther) {
$this->set($lang);
}

return $isOther;
}

private function sortModes($modes)
{
$sorted = [];
Expand Down Expand Up @@ -132,7 +159,7 @@ public function get()
return $lang;
}

protected function filterLang($lang)
public function filterLang($lang)
{
$lang = parent::filterLang($lang);
return $lang ? $lang : $this->getDefaultLanguage();
Expand Down
54 changes: 29 additions & 25 deletions src/LanguageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
namespace Zagovorichev\Laravel\Languages;


use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use zagovorichev\laravel\languages\Http\Middleware\LanguagesMiddleware;

class LanguageServiceProvider extends ServiceProvider
{
const CONFIG_NAME = 'languages';

protected $defer = false;

/**
* @var string
*/
private $defaultConfigPath;

public function __construct($app)
public function __construct(Application $app)
{
parent::__construct($app);

Expand All @@ -39,55 +38,60 @@ public function register()
{
$this->mergeConfigFrom($this->defaultConfigPath, self::CONFIG_NAME);

$this->app->singleton('languages', function ($app) {

$manager = new LanguageManager(config(self::CONFIG_NAME));

$this->app->singleton(LanguageManager::class, function ($app) {
$conf = new Repository(config(self::CONFIG_NAME));
$manager = new LanguageManager($conf);
return $manager;
});
}

public function boot()
/*
* TODO I must understand how I can place midleware into another group from the provider
*
* todo (then I can delete initialization from the app/Http/Kernel and I can use only provider initialization)
*
*
*
* public function boot()
{
$this->publishes([$this->defaultConfigPath => $this->basePath()], 'config');
$languages = $this->app['languages'];
$languages->enable();
$languages->boot();

$this->registerMiddleware(LanguagesMiddleware::class);
}
}*/

/**
* Register the Debugbar Middleware
* Register the Languages Middleware should be in the web group
* in other case we can't write cookie, sessions etc.
*
* @param string $middleware
*/
protected function registerMiddleware($middleware)
/* protected function registerMiddleware($middleware)
{
$kernel = $this->app['Illuminate\Contracts\Http\Kernel'];
$kernel->pushMiddleware($middleware);
}
if (!$kernel->hasMiddleware()){
$kernel->pushMiddleware($middleware);
}
}*/

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
/* public function provides()
{
return ['languages'];
}
return [self::CONFIG_NAME];
}*/

private function basePath()
/* private function basePath()
{
$pubPath = '';
if (function_exists('config_path')) {
$pubPath = config_path(self::CONFIG_FILENAME);
$pubPath = config_path(self::CONFIG_NAME);
} elseif (function_exists('base_path')) {
$pubPath = base_path('config/' . self::CONFIG_FILENAME);
$pubPath = base_path('config/' . self::CONFIG_NAME);
}
return $pubPath;
}
}*/
}
2 changes: 1 addition & 1 deletion src/Manager/CookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct($config, $cookie = null)
} elseif ($this->getConfig()->has('cookie')) {
$this->cookie = $this->getConfig()->get('cookie');
} elseif(function_exists('cookie')) {
$this->cookie = cookie();
$this->cookie = Cookie::class;
} else {
throw new LanguageManagerException('Cookie did not specified [specify it through config or use cookie() method]');
}
Expand Down
5 changes: 5 additions & 0 deletions src/Manager/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected function getRegExp()
{
return $this->getConfig()->get('domainRegExp', '');
}

protected function separator()
{
return $this->getConfig()->get('domainSeparator', '');
}
}
2 changes: 1 addition & 1 deletion src/Manager/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getLanguages()
return $this->languages;
}

protected function filterLang($lang)
public function filterLang($lang)
{
if (!in_array($lang, $this->getLanguages())) {
$lang = false;
Expand Down
13 changes: 9 additions & 4 deletions src/Manager/PathManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class PathManager extends RequestManager
{
private $path = '';
protected $path = '';

protected function getResource()
{
Expand All @@ -26,13 +26,13 @@ protected function getResource()

protected function getRegExp()
{
return $this->getConfig()->get('pathRegExp', '');
return $this->getConfig()->get('pathRegExp', false);
}

public function get()
{
$lang = false;
if (preg_match($this->getRegExp()['reg'], $this->getResource(), $match) !== false) {
if ($this->getRegExp() && !empty($this->getRegExp()['reg']) && preg_match($this->getRegExp()['reg'], $this->getResource(), $match) !== false) {
if (isset($match[$this->getRegExp()['langPart']])) {
$lang = $this->filterLang($match[$this->getRegExp()['langPart']]);
}
Expand All @@ -54,13 +54,18 @@ public function set($lang = '')
if (!$key) {
continue;
}
$path .= ($key == $this->getRegExp()['langPart']) ? $lang : $match;
$path .= ($key == $this->getRegExp()['langPart']) ? $lang . $this->separator() : $match;
}
}

$this->path = $path;
}

protected function separator()
{
return '';
}

public function getRedirectPath()
{
return $this->path;
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public function set($lang = '')
public function getRedirectPath()
{
$params = $this->request->except(['lang']);
return '?' . http_build_query($params);
return count($params) ? '?' . http_build_query($params) : '';
}
}
10 changes: 5 additions & 5 deletions tests/LanguageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testDomainLanguage()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://)([a-z]{2})([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand All @@ -95,7 +95,7 @@ public function testGetLanguageFromPath()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://)([a-z]{2})([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand Down Expand Up @@ -125,7 +125,7 @@ public function testGetLanguageFromCookie()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://)([a-z]{2})([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand Down Expand Up @@ -157,7 +157,7 @@ public function testGetLanguageFromSession()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://)([a-z]{2})([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand Down Expand Up @@ -192,7 +192,7 @@ public function testSetNewLanguage()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://)([a-z]{2})([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/ManagersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testDomainManager()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://)([a-z]{2})([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand All @@ -136,7 +136,7 @@ public function testDomainManager2()

// RegEx from configuration file
$this->config->set('domainRegExp', [
'reg' => '|^(http://province\.)([a-z]{2})(\.example\.com.*)$|ui',
'reg' => '|^(http://province\.)([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2
]);

Expand Down

0 comments on commit 2e5ed1f

Please sign in to comment.