|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Prefix; |
|
4
|
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\LocaleConfig; |
|
6
|
|
|
use Illuminate\Translation\Translator; |
|
7
|
|
|
|
|
8
|
|
|
class Url extends Base |
|
9
|
|
|
{ |
|
10
|
|
|
protected $separator = '/'; |
|
11
|
|
|
protected $translator; |
|
12
|
|
|
|
|
13
|
61 |
|
public function __construct(LocaleConfig $localeConfig, Translator $translator) |
|
14
|
|
|
{ |
|
15
|
61 |
|
parent::__construct($localeConfig); |
|
16
|
61 |
|
$this->translator = $translator; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
44 |
|
public function trimUrl($url): string |
|
20
|
|
|
{ |
|
21
|
44 |
|
return trim($url, '/ ') ?: '/'; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
29 |
|
public function rawRouteUrl($locale, $route, array $options = []): string |
|
25
|
|
|
{ |
|
26
|
29 |
|
$unlocaleUrl = $options[$locale] ?? |
|
27
|
1 |
|
$this->translator->get('routes.' . $route, [], $locale); |
|
28
|
|
|
|
|
29
|
29 |
|
return $this->trimUrl($unlocaleUrl); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
39 |
|
public function switchLocale($locale, $url, array $options = []): string |
|
33
|
|
|
{ |
|
34
|
39 |
|
$unlocalized = $this->removeLocale($url); |
|
35
|
39 |
|
$localized = $this->addLocale($locale, $unlocalized, $options); |
|
36
|
|
|
|
|
37
|
39 |
|
return $this->trimUrl($localized); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
41 |
|
public function addLocale($locale, $unlocalized, $options = []): string |
|
41
|
|
|
{ |
|
42
|
41 |
|
$localized = $this->localeConfig->addLocaleToUrl($options) ? |
|
43
|
40 |
|
parent::addLocale($locale, $unlocalized) : |
|
44
|
2 |
|
$unlocalized; |
|
45
|
|
|
|
|
46
|
41 |
|
return $this->trimUrl($localized); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|