Skip to content

Commit

Permalink
Merge pull request #36 from mputkowski/v5
Browse files Browse the repository at this point in the history
  • Loading branch information
mputkowski authored Mar 11, 2023
2 parents dbaab43 + b7be186 commit c5873af
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 136 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [7.2, 7.4, 8.0]
laravel: [^6.0, ^7.0, ^8.0, ^9.0]
php: [8.0, 8.1, 8.2]
laravel: [^9.0, ^10.0]
stability: [prefer-stable]
exclude:
- laravel: ^9.0
php: 7.2
- laravel: ^9.0
php: 7.4
- laravel: ^8.0
php: 7.2
- laravel: ^10.0
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
.phpunit.result.cache
.phpunit.cache
.phpunit.result.cache
50 changes: 0 additions & 50 deletions .travis.yml

This file was deleted.

21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,42 @@
Powerful Localization for Laravel

## Installation
||L6|L7|L8|L9|L10|
|---|---|---|---|---|---|
|v4|✓|✓|✓|✓|✗|
|v5|✗|✗|✗|✓|✓|

Add package to composer.json
```
composer require mputkowski/laravel-localization
```
**Laravel 5 (5.5 or later)**
```
composer require mputkowski/laravel-localization:^3.2
```
**Laravel 6-9**
```
composer require mputkowski/laravel-localization:^4.0
```

Publish package's config file
```
php artisan vendor:publish --provider="mputkowski\Localization\ServiceProvider"
```

Include middleware within the `web` group in `middlewareGroups` array (`app/Http/Kernel.php`):
```php
'web' => [
\mputkowski\Localization\Middleware\VerifyLangCookie::class,
],
```

**You don't have to register service provider and alias, this package uses Package Auto-Discovery.**

### Manual installation
```
composer require mputkowski/laravel-localization
```

In `config/app.php`, add the following to `providers` array:
```php
'providers' => [
mputkowski\Localization\ServiceProvider::class,
],
```

And register alias in `aliases` array:
```php
'aliases' => [
Expand All @@ -52,6 +54,7 @@ And register alias in `aliases` array:
```
php artisan vendor:publish --provider="mputkowski\Localization\ServiceProvider"
```

Include middleware within the `web` group in `middlewareGroups` array (`app/Http/Kernel.php`):
```php
'web' => [
Expand All @@ -72,7 +75,7 @@ Configuration is stored in `config/localization.php` file.
|pattern|string|`/lang/{lang}`|

### Auto-detection
If `auto` is set to `true`, app will automatically detect client's language. Directories in `resources/lang` will be compared with client's `Accept-Language` header. If header doesn't match with app's locales, language will be set to default.
If `auto` is set to `true`, the app will automatically detect client's language. The lang directory will be compared with the client's `Accept-Language` header. If header doesn't match with the app's locales, language will be set to default.

Auto-detected language could be changed by accessing a special route designed for that, or by calling the `setLocale` method:

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
}
],
"require": {
"php": "^7.2.5|^8.0",
"illuminate/cookie": "^6.0|^7.0|^8.0|^9.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0"
"php": "^8.0.2",
"illuminate/cookie": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5.8|^9.3.3",
"graham-campbell/testbench": "^5.7"
"phpunit/phpunit": "^9.5.8|^10.0",
"graham-campbell/testbench": "^5.7|^6.0"
},
"autoload": {
"psr-4": {
Expand Down
43 changes: 43 additions & 0 deletions config/localization.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Automatically detect client's language
|--------------------------------------------------------------------------
|
| If true, the app will automatically detect client's language.
| The lang directory will be compared with the client's Accept-Language header.
| If the header doesn't match with the app's locales, language will be set to default.
|
*/

'auto' => true,

/*
|--------------------------------------------------------------------------
| Cookie name
|--------------------------------------------------------------------------
|
| Name of the cookie which stores the current language setting.
|
*/

'cookie_name' => 'lang',

/*
|--------------------------------------------------------------------------
| Default locale
|--------------------------------------------------------------------------
|
| Fallback language for the "auto" feature.
|
*/

'default_locale' => 'en',

/*
|--------------------------------------------------------------------------
| Route for changing locale
|--------------------------------------------------------------------------
|
| enabled - boolean
| pattern - string
|
*/

'route' => [
'enabled' => true,
'pattern' => '/lang/{lang}',
Expand Down
41 changes: 19 additions & 22 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
timeoutForLargeTests="60"
timeoutForMediumTests="10"
timeoutForSmallTests="1"
verbose="false"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
timeoutForLargeTests="60"
timeoutForMediumTests="10"
timeoutForSmallTests="1"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Test">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>
32 changes: 19 additions & 13 deletions src/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public function __construct(Config $config, Request $request)
$this->config = $config;
$this->request = $request;

$cookie_name = $this->config->get('localization.cookie_name');
$cookie_value = $request->cookie($cookie_name);
$cookieName = $this->config->get('localization.cookie_name');
$cookieValue = $request->cookie($cookieName);

if (is_string($cookie_value)) {
$this->cookie = $this->makeCookie($request->cookie($cookie_name));
if (is_string($cookieValue)) {
$this->cookie = $this->makeCookie($request->cookie($cookieName));
}
}

Expand All @@ -60,7 +60,7 @@ public function __construct(Config $config, Request $request)
*
* @return null|\Symfony\Component\HttpFoundation\Cookie
*/
public function getCookie()
public function getCookie(): ?Cookie
{
return $this->cookie;
}
Expand All @@ -70,7 +70,7 @@ public function getCookie()
*
* @return \Illuminate\Http\Request
*/
public function getRequest()
public function getRequest(): Request
{
return $this->request;
}
Expand All @@ -80,7 +80,7 @@ public function getRequest()
*
* @return string
*/
public function getLocale()
public function getLocale(): string
{
return app()->getLocale();
}
Expand All @@ -92,7 +92,7 @@ public function getLocale()
*
* @return void
*/
public function setLocale($locale = null)
public function setLocale(?string $locale = null): void
{
$locale = $locale ?? $this->config->get('localization.default_locale');

Expand All @@ -105,7 +105,7 @@ public function setLocale($locale = null)
*
* @return void
*/
public function validate()
public function validate(): void
{
if ($this->cookie instanceof Cookie && $this->cookie->getValue() !== $this->getLocale()) {
$this->setLocale($this->cookie->getValue());
Expand All @@ -119,7 +119,7 @@ public function validate()
*
* @return string
*/
public function getPreferredLanguage()
public function getPreferredLanguage(): string
{
if ($this->request->headers->has('Accept-Language') === false) {
return $this->config->get('localization.default_locale');
Expand All @@ -137,7 +137,7 @@ public function getPreferredLanguage()
*
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function makeCookie($value)
public function makeCookie(string $value): Cookie
{
return cookie()->forever($this->config->get('localization.cookie_name'), $value);
}
Expand All @@ -147,8 +147,14 @@ public function makeCookie($value)
*
* @return array
*/
private function getAvailableLocales()
private function getAvailableLocales(): array
{
return array_values(array_diff(scandir(app()->langPath()), ['..', '.']));
$locales = array_values(array_diff(scandir(app()->langPath()), ['..', '.']));

foreach ($locales as &$locale) {
$locale = basename($locale, '.json');
}

return $locales;
}
}
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function provides()
*
* @return callable
*/
private function getRouteCallback()
private function getRouteCallback(): callable
{
return function ($lang) {
$localization = app('Localization');
Expand All @@ -74,7 +74,7 @@ private function getRouteCallback()
*
* @return void
*/
private function registerRoute()
private function registerRoute(): void
{
$route = config('localization.route.pattern', '/lang/{lang}');

Expand Down
Loading

0 comments on commit c5873af

Please sign in to comment.