Skip to content
Permalink
Browse files

url segment 를 이용한 locale 처리

  • Loading branch information...
jhyeon1010 committed Jun 26, 2019
1 parent 950c474 commit a1f2013a4d66233c1893697d1901d890b7f80da8
@@ -14,11 +14,8 @@
namespace App\Http\Middleware;
use Auth;
use Illuminate\Contracts\Cookie\Factory as CookieFactory;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Response;
use XeLang;
use Closure;
use Xpressengine\Http\Request;
use Xpressengine\User\Rating;
@@ -82,16 +79,6 @@ public function __construct(Application $app)
*/
public function handle(Request $request, Closure $next)
{
// check locale at request & set locale
$locale = $request->get('_l');
if(!$locale) {
$locale = $request->cookie('locale') ?: $this->app['xe.translator']->getLocale();
}
$this->app->setLocale($locale);
$this->app['cookie']->queue(
$this->app[CookieFactory::class]->forever('locale', $locale, null, null, false, false)
);
if ($request->has('xe_use_request_preprocessor') && $this->available()) {
$this->prepare($request);
}
@@ -13,6 +13,7 @@
*/
namespace App\Providers;
use Illuminate\Contracts\Cookie\Factory as CookieFactory;
use Illuminate\Support\ServiceProvider;
/**
@@ -34,7 +35,7 @@ class HttpServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->app['request']->setConfig($this->app['config']);
//
}
/**
@@ -44,15 +45,39 @@ public function boot()
*/
public function register()
{
// bind Request
$this->app->bind(\Xpressengine\Http\Request::class, 'request');
// request 는 web 요청에서만 사용됨. index.php 에서 생성된 후 router 에 의해
// dispatch 되기전에 rebound 됨. 이 시점에 request 에서 처리하기 위한 요소를 정의 함.
$this->app->rebinding('request', function ($app, $request) {
$config = $app['config'];
$request->setConfig($config);
// set config to request when rebind request
$this->app->rebinding(
'request',
function ($app, $request) {
return $request->setConfig($app['config']);
if ($config['xe.lang.locale_route'] === true) {
if (in_array($locale = $request->rawSegment(1), $config['xe.lang.locales'])) {
$app['url']->formatHostUsing(function ($root) use ($locale) {
return rtrim($root, '/') . '/' . $locale;
});
$request->enableLocaleSegment();
} else {
$locale = $this->getFallbackLocale();
}
} else {
$locale = $request->get('_l') ?: $request->cookie('locale');
if (!in_array($locale, $config['xe.lang.locales'])) {
$locale = $this->getFallbackLocale();
}
$app['cookie']->queue(
$app[CookieFactory::class]->forever('locale', $locale, null, null, false, false)
);
}
);
$app->setLocale($locale);
});
}
protected function getFallbackLocale()
{
// return $this->app['config']['app.fallback_locale'];
return $this->app['xe.translator']->getLocale();
}
}
@@ -29,6 +29,7 @@
'en' => 'English',
],
'autocomplete' => false,
'locale_route' => true,
],
'settings' => [
@@ -17,6 +17,7 @@
use Closure;
use Illuminate\Config\Repository;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
@@ -52,6 +53,11 @@ class Request extends \Illuminate\Http\Request
*/
protected $config;
/**
* @var bool
*/
protected $localeSegment = false;
/**
* 현재 요청이 모바일 페이지 요청인지 조회한다.
* $fromUserAgent가 true일 경우 사용자가 강제로 지정한 모드
@@ -193,4 +199,88 @@ public function originExcept($keys)
return $results;
}
/**
* Get the current path info for the request.
*
* @return string
*/
public function path()
{
$path = $this->rawPath();
if ($this->enabledLocaleSegment()) {
$locale = $this->getLocale();
$path = trim(Str::startsWith($path, $locale) ? substr($path, strlen($locale)) : $path, '/');
}
return $path;
}
/**
* Get the current raw path info for the request.
*
* @return string
*/
public function rawPath()
{
return parent::path();
}
/**
* Get a raw segment from the URI (1 based index).
*
* @param int $index segment index
* @param string|null $default default value
* @return string|null
*/
public function rawSegment($index, $default = null)
{
return Arr::get($this->rawSegments(), $index - 1, $default);
}
/**
* Get all of the raw segments for the request path.
*
* @return array
*/
public function rawSegments()
{
$segments = explode('/', rawurldecode($this->rawPath()));
return array_values(array_filter($segments, function ($value) {
return $value !== '';
}));
}
/**
* Get the locale.
*
* @return string
*/
public function getLocale()
{
return $this->enabledLocaleSegment() ? $this->rawSegment(1) : parent::getLocale();
}
/**
* Enables locale segment
*
* @return void
*/
public function enableLocaleSegment()
{
$this->localeSegment = true;
}
/**
* Determine if locale segment is enabled
*
* @return bool
*/
public function enabledLocaleSegment()
{
return $this->localeSegment === true;
}
}
@@ -0,0 +1,41 @@
<?php
/**
* InstanceRouteChecker.php
*
* PHP version 7
*
* @category Routing
* @package Xpressengine\Routing
* @author XE Developers <developers@xpressengine.com>
* @copyright 2015 Copyright (C) NAVER Corp. <http://www.navercorp.com>
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPL-2.1
* @link https://xpressengine.io
*/
namespace Xpressengine\Routing;
use Illuminate\Routing\Route;
/**
* Trait InstanceRouteChecker
*
* @category Routing
* @package Xpressengine\Routing
* @author XE Developers <developers@xpressengine.com>
* @copyright 2015 Copyright (C) NAVER Corp. <http://www.navercorp.com>
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPL-2.1
* @link https://xpressengine.io
*/
trait InstanceRouteChecker
{
/**
* Determine the given route is for the instance.
*
* @param Route $route illuminate route
* @return bool
*/
public function isInstance(Route $route)
{
return strpos($route->uri(), '{instanceGroup') === 0;
}
}
@@ -31,6 +31,7 @@
*/
class ModuleValidator implements ValidatorInterface
{
use InstanceRouteChecker;
/**
* @var null
*/
@@ -114,16 +115,15 @@ private function canReview(Route $route)
return false;
}
$uri = $route->uri();
if (strpos($uri, '{instanceGroup') !== 0) {
if (!$this->isInstance($route)) {
return false;
}
$actions = $route->getAction();
if (isset($actions['module'])) {
return true;
}
return false;
}
@@ -30,6 +30,7 @@
*/
class UriValidator implements ValidatorInterface
{
use InstanceRouteChecker;
/**
* Validate a given rule against a route and request.
@@ -41,13 +42,12 @@ class UriValidator implements ValidatorInterface
*/
public function matches(Route $route, Request $request)
{
$path = $request->path() == '/' ? '/' : '/' . $request->path();
$firstSegment = $request->segment(1);
$uri = $route->uri();
if (strpos($uri, '{instanceGroup') === 0 && $firstSegment === null) {
if ($request->segment(1) === null && $this->isInstance($route)) {
return true;
} else {
return preg_match($route->getCompiled()->getRegex(), rawurldecode($path));
$path = $request->decodedPath() == '/' ? '/' : '/' . $request->decodedPath();
return preg_match($route->getCompiled()->getRegex(), $path);
}
}
}
@@ -283,9 +283,16 @@ function xe_trans_choice($id, $number, array $parameters = array(), $locale = nu
*/
function locale_url($locale)
{
$queries = app('request')->query->all();
array_set($queries, '_l', $locale);
return app('request')->url() . '?' . http_build_query($queries);
$request = app('request');
$queries = $request->query->all();
if (config('xe.lang.locale_route') === true) {
$url = $request->root().'/'.$locale.'/'.$request->path();
} else {
$url = $request->url();
array_set($queries, '_l', $locale);
}
return $url.'?'.http_build_query($queries);
}
}

0 comments on commit a1f2013

Please sign in to comment.
You can’t perform that action at this time.