Skip to content

Commit 2d699ce

Browse files
authored
Update Translatable.php
1 parent 9f91072 commit 2d699ce

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

src/Translatable.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace SertxuDeveloper\Translatable;
44

5-
use Illuminate\Http\RedirectResponse;
6-
use Illuminate\Http\Request;
7-
use Illuminate\Support\Facades\App;
85
use Illuminate\Support\Facades\Route;
9-
use Illuminate\Support\Facades\Session;
106
use Illuminate\Support\Facades\URL;
117

128
class Translatable {
@@ -16,35 +12,23 @@ public function checkLocaleInSupportedLocales($locale) {
1612
return in_array($locale, $locales);
1713
}
1814

19-
public function isFallbackLocaleHidden() {
20-
return config('translatable.hide_fallback_locale');
15+
public function getLocaleFromRequest() {
16+
$params = explode('/', request()->getPathInfo());
17+
// Dump the first element (empty string) as getPathInfo() always returns a leading slash
18+
array_shift($params);
19+
if (\count($params) > 0) {
20+
$locale = $params[0];
21+
if ($this->checkLocaleInSupportedLocales($locale)) return $locale;
22+
}
23+
return config('translatable.fallback_locale');
2124
}
2225

2326
public function isFallbackLocale($locale) {
2427
return config('translatable.fallback_locale') === $locale;
2528
}
2629

27-
public function switchToLocale($locale) {
28-
$request = request();
29-
$params = explode('/', $request->getPathInfo());
30-
// Dump the first element (empty string) as getPathInfo() always returns a leading slash
31-
array_shift($params);
32-
if (\count($params) > 0) {
33-
if ($this->checkLocaleInSupportedLocales($locale)) {
34-
if ($this->checkLocaleInSupportedLocales($params[0])) array_shift($params);
35-
$params = implode('/', $params);
36-
if ($this->isFallbackLocaleHidden() && $this->isFallbackLocale($locale)) {
37-
$url = $params;
38-
} else {
39-
$url = $locale . '/' . $params;
40-
}
41-
}
42-
} else {
43-
$url = '';
44-
}
45-
46-
$url = $url = $request->root() . "/${url}";
47-
return $url;
30+
public function isFallbackLocaleHidden() {
31+
return config('translatable.hide_fallback_locale');
4832
}
4933

5034
public function route($name, $parameters = [], $absolute = false, $locale = null) {
@@ -61,30 +45,46 @@ public function route($name, $parameters = [], $absolute = false, $locale = null
6145
$url = URL::route($name, $parameters, $absolute);
6246
} else {
6347
$params = explode('/', $name);
64-
if ($params[0] == null) array_shift($params);
48+
if ($params[0] == null)
49+
array_shift($params);
50+
6551
if (\count($params) > 0) {
66-
if ($this->checkLocaleInSupportedLocales($params[0])) array_shift($params);
52+
if ($this->checkLocaleInSupportedLocales($params[0]))
53+
array_shift($params);
54+
6755
$params = implode('/', $params);
68-
if ($this->isFallbackLocaleHidden() && $this->isFallbackLocale($locale)) {
69-
$url = $params;
70-
} else {
71-
$url = "/$locale/$params";
72-
}
56+
57+
if (!$this->isFallbackLocaleHidden() || !$this->isFallbackLocale($locale))
58+
$params = "${locale}/${params}";
59+
60+
$url = URL::to($params);
7361
}
7462
}
75-
63+
7664
return $url;
7765
}
7866

79-
public function getLocaleFromRequest() {
80-
$params = explode('/', request()->getPathInfo());
67+
public function switchToLocale($locale) {
68+
$request = request();
69+
$params = explode('/', $request->getPathInfo());
8170
// Dump the first element (empty string) as getPathInfo() always returns a leading slash
8271
array_shift($params);
8372
if (\count($params) > 0) {
84-
$locale = $params[0];
85-
if ($this->checkLocaleInSupportedLocales($locale)) return $locale;
73+
if ($this->checkLocaleInSupportedLocales($locale)) {
74+
if ($this->checkLocaleInSupportedLocales($params[0])) array_shift($params);
75+
$params = implode('/', $params);
76+
if ($this->isFallbackLocaleHidden() && $this->isFallbackLocale($locale)) {
77+
$url = $params;
78+
} else {
79+
$url = $locale . '/' . $params;
80+
}
81+
}
82+
} else {
83+
$url = '';
8684
}
87-
return config('translatable.fallback_locale');
85+
86+
$url = $url = $request->root() . "/${url}";
87+
return $url;
8888
}
8989

9090
/**

0 commit comments

Comments
 (0)