1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Presspack\Framework\Support\Localization; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
|
8
|
|
|
class Localize |
9
|
|
|
{ |
10
|
|
|
protected $request; |
11
|
|
|
public $supportedLocales; |
12
|
|
|
|
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->request = app()['request']; |
16
|
|
|
$this->supportedLocales = $this->getSupportedLocales(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getSupportedLocales() |
20
|
|
|
{ |
21
|
|
|
return null === config('presspack.supported_locales') |
22
|
|
|
? DB::table('icl_languages')->where('active', '1')->pluck('code')->all() |
23
|
|
|
: config('presspack.supported_locales'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function prefixFromRequest(int $segment = 0): ?string |
27
|
|
|
{ |
28
|
|
|
$url = $this->request->getUri(); |
29
|
|
|
|
30
|
|
|
return $this->getLocaleFromUrl($url, $segment); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getLocaleFromUrl(string $url, int $segment = 0): ?string |
34
|
|
|
{ |
35
|
|
|
return $this->parseUrl($url, $segment)['locale']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function parseUrl(string $url, int $segment = 0) |
39
|
|
|
{ |
40
|
|
|
$parsedUrl = parse_url($url); |
41
|
|
|
$parsedUrl['segments'] = array_values(array_filter(explode('/', $parsedUrl['path']), 'strlen')); |
42
|
|
|
$localeCandidate = array_get($parsedUrl['segments'], $segment, false); |
|
|
|
|
43
|
|
|
$parsedUrl['locale'] = \in_array($localeCandidate, $this->supportedLocales, true) ? $localeCandidate : null; |
44
|
|
|
$parsedUrl['query'] = array_get($parsedUrl, 'query', false); |
|
|
|
|
45
|
|
|
$parsedUrl['fragment'] = array_get($parsedUrl, 'fragment', false); |
|
|
|
|
46
|
|
|
unset($parsedUrl['path']); |
47
|
|
|
|
48
|
|
|
return $parsedUrl; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isValidLocale(string $locale): bool |
52
|
|
|
{ |
53
|
|
|
return \in_array($locale, $this->supportedLocales, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function url(string $url, string $locale = null, int $segment = 0): string |
57
|
|
|
{ |
58
|
|
|
$locale = $locale ?: App::getLocale(); |
59
|
|
|
$cleanUrl = $this->cleanUrl($url, $segment); |
60
|
|
|
$parsedUrl = $this->parseUrl($cleanUrl, $segment); |
61
|
|
|
// Check if there are enough segments, if not return url unchanged: |
62
|
|
|
if (\count($parsedUrl['segments']) >= $segment) { |
63
|
|
|
array_splice($parsedUrl['segments'], $segment, 0, $locale); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return url($this->pathFromParsedUrl($parsedUrl)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Removes the domain and locale (if present) of a given url. |
71
|
|
|
* |
72
|
|
|
* Ex: |
73
|
|
|
* http://www.domain.com/locale/random => /random, |
74
|
|
|
* https://www.domain.com/random => /random, http://domain.com/random?param=value => /random?param=value. |
75
|
|
|
*/ |
76
|
|
|
public function cleanUrl(string $url, int $segment = 0): string |
77
|
|
|
{ |
78
|
|
|
$parsedUrl = $this->parseUrl($url, $segment); |
79
|
|
|
// Remove locale from segments: |
80
|
|
|
if ($parsedUrl['locale']) { |
81
|
|
|
unset($parsedUrl['segments'][$segment]); |
82
|
|
|
$parsedUrl['locale'] = false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->pathFromParsedUrl($parsedUrl); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the uri for the given parsed url based on its segments, query and fragment. |
90
|
|
|
*/ |
91
|
|
|
protected function pathFromParsedUrl(array $parsedUrl): string |
92
|
|
|
{ |
93
|
|
|
$path = '/'.implode('/', $parsedUrl['segments']); |
94
|
|
|
if ($parsedUrl['query']) { |
95
|
|
|
$path .= "?{$parsedUrl['query']}"; |
96
|
|
|
} |
97
|
|
|
if ($parsedUrl['fragment']) { |
98
|
|
|
$path .= "#{$parsedUrl['fragment']}"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $path; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.