Skip to content

Commit

Permalink
Merge pull request #7 from zagovorichev/release-v1.1.0
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
zagovorichev authored Oct 29, 2016
2 parents 2e5ed1f + 8e939bc commit 7fbe50f
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 19 deletions.
122 changes: 120 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,120 @@
# laravel-languages
For using different languages in the Laravel framework
# Multi language Laravel Plugin

This package pulls in the framework agnostic Language Manager and provides seamless integration with **Laravel 5**.

- [What is it?](#what-is-it?)
- [Installation](#installation)
- [Usage](#usage)
- [Requirements](#requirements)
- [License](#license)

## What is it?

Language manager: you can choose new language and it will be placed as `App::setLocale($lang)`

Language will be saved in SESSION and/or COOKIE

Also you can use URL
```
http://www.example.com/article/1?lang=en => http://en.example.com/en/article/1
```

## Installation

`composer require zagovorichev/laravel-languages`

Add to the **app.php** (by default in */laravel/config/app.php*) lines:

```php
'providers' => [
// ...
Zagovorichev\Laravel\Languages\LanguageServiceProvider::class,
]
```

And new middleware into the **Kernel.php** (look in */laravel/app/Http/Kernel.php*)

```php
protected $middlewareGroups = [
'web' => [
// ...
\Zagovorichev\Laravel\Languages\Http\Middleware\LanguagesMiddleware\LanguagesMiddleware::class,
],
];
```

## Usage

Language manager provides working with sessions, cookies, path and domain.

For changing system language send request with input **lang**. For example: `?lang=es`
and in application you will have Spain locale.

All configurations stored in the **languages.php** file.
For making your own configuration copy file */laravel/vendor/zagovorichev/laravel-languages/config/languages.php*
to the folder */laravel/config/*.

#### Simple usage (SESSION + COOKIE)
If you need simple language manager you can use only 'session' and 'cookie' modes. Then you don't need to
configure anything more.

#### Domain Map

For using DomainMapManger in your configuration file you should matched languages and domains

```
'domainMap' => [
'en' => 'www.example.com',
'es' => 'es.example.com',
'ua' => 'www.example.ua',
],
```

> :warning: **Notice**: Please, use only DomainMap or Domain modes, otherwise the behaviour will be unpredictable.
#### Domain

Also you can provide regular expression in the configuration file.

```php
'domainRegExp' => [
'reg' => '|^(http://)([a-z]{2})[\.]{0,1}(example\.com.*)$|ui',
'langPart' => 2,
]
```

And as a result you will have
`http://www.example.en/post/234 => http://en.example.com/post/234 => http://es.example.com/post/234`

> :warning: **Notice**: Please, use only DomainMap or Domain modes, otherwise the behaviour will be unpredictable.
#### Path
Similar to domains, you can provide regular expression:

```php
'pathRegExp' => [
'reg' => '|([a-z]{2})(/.*)|ui',
'langPart' => 1, // lang part will be replaced with new lang
],
```


### Modes of the languages manager

Each store has its own manager.

- **session** - store in the $_SESSION
- **cookie** - store in the browser $_COOKIES
- **domainMap** - you can define map if you have different domain which can't be described with simple regular expression
- **domain** - use domain name for determining current language (www.example.com, en.example.com, es.example.com...)
- **path** - use uri for language example.com/en/address


## Requirements

* PHP 5.6
* Laravel 5.3

## License

This package is licensed under the [MIT license](https://github.com/backup-manager/laravel/blob/master/LICENSE)
18 changes: 16 additions & 2 deletions config/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,21 @@
'domainRegExp' => [
'reg' => '|^(http://)([a-z]{2})(\.example\.com.*)$|ui',
'langPart' => 2,
'domainSeparator' => '.',
]
'separator' => '.',
],

/*
|-----------------------------------------------------------------
| DomainMap manager configuration
|-----------------------------------------------------------------
|
| You can identify which language will use which domain
|
*/
'domainMap' => [
'en' => 'www.example.com',
'es' => 'es.example.com',
'ua' => 'www.example.ua',
],

];
32 changes: 27 additions & 5 deletions src/LanguageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Config\Repository;
use Zagovorichev\Laravel\Languages\Manager\CookieManager;
use Zagovorichev\Laravel\Languages\Manager\DomainManager;
use Zagovorichev\Laravel\Languages\Manager\DomainMapManager;
use Zagovorichev\Laravel\Languages\Manager\Manager;
use Zagovorichev\Laravel\Languages\Manager\PathManager;
use Zagovorichev\Laravel\Languages\Manager\RequestManager;
Expand Down Expand Up @@ -44,8 +45,8 @@ class LanguageManager extends Manager
'session',
'cookie',
'path',
'domainMap',
'domain', // low priority

];

/**
Expand All @@ -58,6 +59,7 @@ class LanguageManager extends Manager
'cookie' => CookieManager::class,
'path' => PathManager::class,
'domain' => DomainManager::class,
'domainMap' => DomainMapManager::class,
];

/**
Expand Down Expand Up @@ -85,6 +87,11 @@ public function __construct(Repository $config)
}
}

protected function getModeName()
{
return 'languageManager';
}

public function isOtherLanguage()
{
$isOther = false;
Expand All @@ -105,6 +112,13 @@ public function isOtherLanguage()
$isOther = true;
}

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

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

if ($isOther) {
$this->set($lang);
}
Expand Down Expand Up @@ -181,8 +195,7 @@ public function has()
{
$lang = false;
foreach ($this->modes as $mode) {
$lang = $this->getManager($mode)->has();
if ($lang) {
if ( $lang = $this->getManager($mode)->has() ) {
break;
}
}
Expand All @@ -192,8 +205,17 @@ public function has()

public function getRedirectPath()
{
$domain = $this->getManager('domain')->getRedirectPath();
$path = $this->getManager('path')->getRedirectPath();
$domain = $path = '';
if ($this->getManager('domainMap')->active()) {
$domain = $this->getManager('domainMap')->getRedirectPath();
}
if (!$domain && $this->getManager('domain')->active()) {
$domain = $this->getManager('domain')->getRedirectPath();
}
if ($this->getManager('path')->active()) {
$path = $this->getManager('path')->getRedirectPath();
}

$request = $this->getManager('request')->getRedirectPath();

return $domain . $path . $request;
Expand Down
5 changes: 5 additions & 0 deletions src/Manager/CookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function __construct($config, $cookie = null)
}
}

protected function getModeName()
{
return 'cookie';
}

public function get()
{
return call_user_func([$this->cookie, 'get'], self::LANG, false);
Expand Down
7 changes: 6 additions & 1 deletion src/Manager/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

class DomainManager extends PathManager
{
protected function getModeName()
{
return 'domain';
}

protected function getResource()
{
return $this->request->url();
Expand All @@ -27,6 +32,6 @@ protected function getRegExp()

protected function separator()
{
return $this->getConfig()->get('domainSeparator', '');
return $this->getConfig()->get('domainRegExp.separator', '');
}
}
64 changes: 64 additions & 0 deletions src/Manager/DomainMapManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MIT Public License for more details.
*
* Copyright (c) 2016. (original work) Blog-Tree.com;
*
* @author A.Zagovorichev <zagovorichev@gmail.com>
*/

namespace Zagovorichev\Laravel\Languages\Manager;


use Zagovorichev\Laravel\Languages\LanguageManagerException;

class DomainMapManager extends RequestManager
{

private $redirectPath = false;

protected function getModeName()
{
return 'domainMap';
}

public function has()
{
return !!$this->get();
}

public function get()
{
$lang = false;
$url = $this->request->url();
foreach ($this->getConfig()->get('domainMap') as $_lang => $domain) {
if (mb_strpos($url, $domain) !== false) {
$lang = $_lang;
break;
}
}
return $lang;
}

public function set($lang = '')
{
$map = $this->getConfig()->get('domainMap');
// don't do anything leave it for another storages
if (!isset($map[$lang]) || !isset($map[$this->get()])) {
return false;
}

$oldDomain = $map[$this->get()];
$newDomain = $map[$lang];

$this->redirectPath = str_replace($oldDomain, $newDomain, $this->request->url());
}

public function getRedirectPath()
{
return $this->redirectPath;
}
}
10 changes: 10 additions & 0 deletions src/Manager/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public function __construct(Repository $config)
}
}

/**
* @return string
*/
abstract protected function getModeName();

public function active()
{
return in_array($this->getModeName(), $this->getConfig()->get('modes'));
}

private function setConfig(Repository $config)
{
$this->config = $config;
Expand Down
8 changes: 7 additions & 1 deletion src/Manager/PathManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class PathManager extends RequestManager
{
protected $path = '';

protected function getModeName()
{
return 'path';
}

protected function getResource()
{
return $this->request->path();
Expand All @@ -32,7 +37,8 @@ protected function getRegExp()
public function get()
{
$lang = false;
if ($this->getRegExp() && !empty($this->getRegExp()['reg']) && 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 Down
5 changes: 5 additions & 0 deletions src/Manager/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class RequestManager extends Manager

protected $request;

protected function getModeName()
{
return 'request';
}

public function __construct(Repository $config, $request = null)
{
parent::__construct($config);
Expand Down
5 changes: 5 additions & 0 deletions src/Manager/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class SessionManager extends Manager
*/
private $session;

protected function getModeName()
{
return 'session';
}

public function __construct(Repository $config, $session = null)
{
parent::__construct($config);
Expand Down
Loading

0 comments on commit 7fbe50f

Please sign in to comment.