Skip to content

Commit

Permalink
Merge pull request #145 from ARCANEDEV/update-package
Browse files Browse the repository at this point in the history
Updating the package
  • Loading branch information
arcanedev-maroc authored Oct 3, 2019
2 parents ff8fddd + efe7176 commit 4c5588b
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 186 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php

sudo: false

php:
- 7.2
- 7.3
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Localization [![Packagist License][badge_license]](LICENSE.md) [![For Laravel 5/6][badge_laravel]][link-github-repo]
# Localization [![Packagist License][badge_license]](LICENSE.md) [![For Laravel][badge_laravel]][link-github-repo]

[![Travis Status][badge_build]][link-travis]
[![Coverage Status][badge_coverage]][link-scrutinizer]
Expand All @@ -21,7 +21,7 @@ Feel free to check out the [releases](https://github.com/ARCANEDEV/Localization/
### Features

* Easy setup & configuration.
* Laravel `5.x` & `6.0` are supported.
* Laravel `5.x | 6.x` are supported.
* SEO-Friendly (Search engine optimization).
* New extended Router to manage your localized routes.
* Translated Eloquent Models.
Expand Down Expand Up @@ -52,7 +52,7 @@ If you discover any security related issues, please email arcanedev.maroc@gmail.
- [All Contributors][link-contributors]

[badge_license]: http://img.shields.io/packagist/l/arcanedev/localization.svg?style=flat-square
[badge_laravel]: https://img.shields.io/badge/For-Laravel%205.x%20to%206.0-orange.svg?style=flat-square
[badge_laravel]: https://img.shields.io/badge/Laravel-5.x%20|%206.x-orange.svg?style=flat-square
[badge_build]: http://img.shields.io/travis/ARCANEDEV/Localization.svg?style=flat-square
[badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/Localization.svg?style=flat-square
[badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/Localization.svg?style=flat-square
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"require": {
"php": ">=7.2.0",
"ext-json": "*",
"arcanedev/support": "~5.0.0"
"arcanedev/support": "^5.0"
},
"require-dev": {
"orchestra/testbench": "~4.0.0",
"mockery/mockery": "~1.0",
"phpunit/phpcov": "~6.0",
"phpunit/phpunit": "~8.0"
"orchestra/testbench": "^4.0",
"mockery/mockery": "^1.0",
"phpunit/phpcov": "^6.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -38,7 +38,8 @@
"extra": {
"laravel": {
"providers": [
"Arcanedev\\Localization\\LocalizationServiceProvider"
"Arcanedev\\Localization\\LocalizationServiceProvider",
"Arcanedev\\Localization\\Providers\\DeferredServicesProvider"
]
}
},
Expand Down
2 changes: 0 additions & 2 deletions config/localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

'redirection-code' => 302,

'facade' => 'Localization',

'utf-8-suffix' => '.UTF-8',

/* -----------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions src/Contracts/RouteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public function findTranslatedRouteByPath($path, $locale);
/**
* Get URL from route name.
*
* @param string $locale
* @param string $defaultLocale
* @param string $transKey
* @param array $attributes
* @param bool|false $defaultHidden
* @param bool|false $showHiddenLocale
* @param string|bool $locale
* @param string $defaultLocale
* @param string $transKey
* @param array $attributes
* @param bool|false $defaultHidden
* @param bool|false $showHiddenLocale
*
* @return string
*/
Expand Down
6 changes: 1 addition & 5 deletions src/Entities/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,8 @@ private function setScript($script)
public function direction()
{
if (empty($this->direction)) {
$rtlScripts = ['Arab', 'Hebr', 'Mong', 'Tfng', 'Thaa'];

$this->setDirection(
in_array($this->script, $rtlScripts)
? self::DIRECTION_RIGHT_TO_LEFT
: self::DIRECTION_LEFT_TO_RIGHT
in_array($this->script, ['Arab', 'Hebr', 'Mong', 'Tfng', 'Thaa']) ? self::DIRECTION_RIGHT_TO_LEFT : self::DIRECTION_LEFT_TO_RIGHT
);
}

Expand Down
20 changes: 0 additions & 20 deletions src/Facades/Localization.php

This file was deleted.

7 changes: 3 additions & 4 deletions src/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ public function getNonLocalizedURL($url = null)
/**
* Returns an URL adapted to $locale or current locale.
*
* @todo: Refactor this beast
*
* @param string|null $locale
* @param string|null $url
* @param array $attributes
Expand Down Expand Up @@ -404,7 +402,8 @@ public function localesNavbar()
*/
private function findTranslatedRouteByUrl($url, $attributes, $locale)
{
if (empty($url)) return false;
if (empty($url))
return false;

// check if this url is a translated url
foreach ($this->routeTranslator->getTranslatedRoutes() as $translatedRoute) {
Expand Down Expand Up @@ -494,7 +493,7 @@ public function isLocaleSupported($locale)
*
* @throws \Arcanedev\Localization\Exceptions\UnsupportedLocaleException
*/
private function isLocaleSupportedOrFail($locale)
private function isLocaleSupportedOrFail($locale): void
{
if ( ! $this->isLocaleSupported($locale))
throw new UnsupportedLocaleException(
Expand Down
46 changes: 5 additions & 41 deletions src/LocalizationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Arcanedev\Localization;

use Arcanedev\Support\PackageServiceProvider;
use Arcanedev\Support\Providers\PackageServiceProvider;

/**
* Class LocalizationServiceProvider
Expand Down Expand Up @@ -30,57 +30,21 @@ class LocalizationServiceProvider extends PackageServiceProvider
/**
* Register the service provider.
*/
public function register()
public function register(): void
{
parent::register();

$this->registerConfig();
$this->registerProviders([
Providers\RoutingServiceProvider::class,
Providers\UtilitiesServiceProvider::class,
]);
$this->registerLocalization();

$this->registerProvider(Providers\RoutingServiceProvider::class);
}

/**
* Boot the package.
*/
public function boot()
public function boot(): void
{
parent::boot();

$this->publishConfig();
$this->publishViews();
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
Contracts\Localization::class,
];
}

/* -----------------------------------------------------------------
| Services Methods
| -----------------------------------------------------------------
*/

/**
* Register Localization.
*/
private function registerLocalization()
{
$this->singleton(Contracts\Localization::class, Localization::class);

if ($alias = $this->config()->get('localization.facade')) {
$this->alias($alias, Facades\Localization::class);

$this->registerAliases();
}
}
}
2 changes: 0 additions & 2 deletions src/Middleware/LocaleSessionRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*
* @package Arcanedev\Localization\Middleware
* @author ARCANEDEV <[email protected]>
*
* @todo: Refactoring
*/
class LocaleSessionRedirect extends Middleware
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?php namespace Arcanedev\Localization\Providers;

use Arcanedev\Localization\Contracts\LocalesManager as LocalesManagerContract;
use Arcanedev\Localization\Contracts\Negotiator as NegotiatorContract;
use Arcanedev\Localization\Contracts\RouteTranslator as RouteTranslatorContract;
use Arcanedev\Localization\Utilities\LocalesManager;
use Arcanedev\Localization\Utilities\Negotiator;
use Arcanedev\Localization\Utilities\RouteTranslator;
use Arcanedev\Support\ServiceProvider;
use Arcanedev\Localization\Contracts\{
LocalesManager as LocalesManagerContract,
Localization as LocalizationContract,
Negotiator as NegotiatorContract,
RouteTranslator as RouteTranslatorContract
};
use Arcanedev\Localization\Localization;
use Arcanedev\Localization\Utilities\{LocalesManager, Negotiator, RouteTranslator};
use Arcanedev\Support\Providers\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;

/**
* Class UtilitiesServiceProvider
* Class DeferredServicesProvider
*
* @package Arcanedev\Localization\Providers
* @author ARCANEDEV <[email protected]>
*/
class UtilitiesServiceProvider extends ServiceProvider
class DeferredServicesProvider extends ServiceProvider implements DeferrableProvider
{
/* -----------------------------------------------------------------
| Main Methods
Expand All @@ -24,10 +27,9 @@ class UtilitiesServiceProvider extends ServiceProvider
/**
* Register the service provider.
*/
public function register()
public function register(): void
{
parent::register();

$this->registerLocalization();
$this->registerRouteTranslator();
$this->registerLocalesManager();
$this->registerLocaleNegotiator();
Expand All @@ -41,25 +43,32 @@ public function register()
public function provides()
{
return [
LocalizationContract::class,
RouteTranslatorContract::class,
LocalesManagerContract::class,
NegotiatorContract::class,
];
}

/* -----------------------------------------------------------------
| Utilities
| Services
| -----------------------------------------------------------------
*/

/**
* Register Localization.
*/
private function registerLocalization()
{
$this->singleton(LocalizationContract::class, Localization::class);
}

/**
* Register RouteTranslator utility.
*/
private function registerRouteTranslator()
{
$this->singleton(RouteTranslatorContract::class, function ($app) {
return new RouteTranslator($app['translator']);
});
$this->singleton(RouteTranslatorContract::class, RouteTranslator::class);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Traits/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ public function setAttribute($key, $value)
*
* @param string $key
* @param string $locale
* @param bool $useFallbackLocale
*
* @return mixed
*/
public function trans($key, $locale = '')
public function trans($key, $locale = '', bool $useFallbackLocale = true)
{
return $this->getTranslation($key, $locale);
return $this->getTranslation($key, $locale, $useFallbackLocale);
}

/***
Expand Down Expand Up @@ -121,7 +122,7 @@ public function getTranslations($key = null)
$this->guardAgainstNonTranslatableAttribute($key);

return array_filter(json_decode($this->getAttributeFromArray($key) ?? '' ?: '{}', true) ?: [], function ($value) {
return $value !== null && $value !== false && $value !== '';
return $value !== null && $value !== '';
});
}

Expand Down
1 change: 0 additions & 1 deletion src/Utilities/LocalesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ public function setDefaultLocale($defaultLocale = null)

$this->isDefaultLocaleSupported($defaultLocale);
$this->defaultLocale = $defaultLocale;
// TODO: Refresh locales & supportedLocales collection [Locale entity => default property]

return $this;
}
Expand Down
14 changes: 5 additions & 9 deletions src/Utilities/Negotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Arcanedev\Localization\Contracts\Negotiator as NegotiatorContract;
use Arcanedev\Localization\Entities\LocaleCollection;
use Illuminate\Http\Request;
use Locale;

/**
* Class Negotiator
Expand Down Expand Up @@ -105,8 +104,6 @@ public function negotiate(Request $request)
if ( ! is_null($locale = $this->getFromRemoteHostServer()))
return $locale;

// TODO: Adding negotiate form IP Address ??

return $this->defaultLocale;
}

Expand Down Expand Up @@ -140,14 +137,13 @@ private function getFromHttpAcceptedLanguagesServer()
{
$httpAcceptLanguage = $this->request->server('HTTP_ACCEPT_LANGUAGE');

// @codeCoverageIgnoreStart
if ( ! class_exists('Locale') || empty($httpAcceptLanguage))
return null;
// @codeCoverageIgnoreEnd
$locale = null;

$locale = Locale::acceptFromHttp($httpAcceptLanguage);
if (class_exists('Locale') && ! empty($httpAcceptLanguage))
$locale = \Locale::acceptFromHttp($httpAcceptLanguage);

if ($this->isSupported($locale)) return $locale;
if ($this->isSupported($locale))
return $locale;

return null;
}
Expand Down
Loading

0 comments on commit 4c5588b

Please sign in to comment.