|
1 |
| -<?php namespace KiaKing\LaravelLocale; |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace KiaKing\LaravelLocale; |
2 | 4 |
|
3 | 5 | use Illuminate\Contracts\Config\Repository as Config;
|
4 | 6 | use Illuminate\Http\Request;
|
5 | 7 | use Illuminate\Contracts\Routing\UrlGenerator;
|
6 | 8 |
|
7 |
| -class LocaleUrlGenerator { |
8 |
| - |
9 |
| - /** |
10 |
| - * Instance of config. |
11 |
| - * |
12 |
| - * @var \Illuminate\Contracts\Config\Repository |
13 |
| - */ |
14 |
| - protected $config; |
15 |
| - |
16 |
| - /** |
17 |
| - * Instance of request. |
18 |
| - * |
19 |
| - * @var \Illuminate\Http\Request |
20 |
| - */ |
21 |
| - protected $request; |
22 |
| - |
23 |
| - /** |
24 |
| - * Instance of url generator. |
25 |
| - * |
26 |
| - * @var \Illuminate\Contracts\Routing\UrlGenerator |
27 |
| - */ |
28 |
| - protected $url; |
29 |
| - |
30 |
| - /** |
31 |
| - * Create LocaleUrlGenerator instance. |
32 |
| - * |
33 |
| - * @param \Illuminate\Contracts\Config\Repository $config |
34 |
| - * @param \Illuminate\Http\Request $request |
35 |
| - * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
36 |
| - */ |
37 |
| - function __construct(Config $config, Request $request, UrlGenerator $url) |
38 |
| - { |
39 |
| - $this->config = $config; |
40 |
| - $this->request = $request; |
41 |
| - $this->url = $url; |
42 |
| - } |
43 |
| - |
44 |
| - /** |
45 |
| - * Generate a absolute URL to the given path. |
46 |
| - * |
47 |
| - * @param string $path |
48 |
| - * @param mixed $extra |
49 |
| - * @param bool $secure |
50 |
| - * @return string |
51 |
| - */ |
52 |
| - public function url($path, $extra = [], $secure = null) |
53 |
| - { |
54 |
| - if ($this->isCurrentLocaleDefault()) |
55 |
| - { |
56 |
| - return $this->url->to($path, $extra, $secure); |
57 |
| - } |
58 |
| - |
59 |
| - $path = $this->config->get('app.locale') . '/' . $path; |
60 |
| - |
61 |
| - return $this->url->to($path, $extra, $secure); |
62 |
| - } |
63 |
| - |
64 |
| - /** |
65 |
| - * Get the URL to a named route. |
66 |
| - * |
67 |
| - * @param string $name |
68 |
| - * @param mixed $parameters |
69 |
| - * @param bool $absolute |
70 |
| - * @return string |
71 |
| - */ |
72 |
| - public function route($name, $parameters = [], $absolute = true) |
73 |
| - { |
74 |
| - if ($this->isCurrentLocaleDefault()) |
75 |
| - { |
76 |
| - return $this->url->route($name, $parameters, $absolute); |
77 |
| - } |
78 |
| - |
79 |
| - $name = $this->config->get('app.locale') . '.' . $name; |
80 |
| - |
81 |
| - return $this->url->route($name, $parameters, $absolute); |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * Get the URL to a different locale. |
86 |
| - * |
87 |
| - * @param string $locale |
88 |
| - * @return string |
89 |
| - */ |
90 |
| - public function change($locale) |
91 |
| - { |
92 |
| - if ($locale == $this->config->get('app.locale')) |
93 |
| - { |
94 |
| - return $this->request->fullUrl(); |
95 |
| - } |
96 |
| - |
97 |
| - $preg = '/\/.{2}($|\/)/'; |
98 |
| - |
99 |
| - if ($locale == $this->config->get('app.fallback_locale')) |
100 |
| - { |
101 |
| - return preg_replace($preg, '/', $this->request->fullUrl()); |
102 |
| - } |
103 |
| - |
104 |
| - if ( ! $this->isCurrentLocaleDefault()) |
105 |
| - { |
106 |
| - return preg_replace($preg, "/{$locale}/", $this->request->fullUrl()); |
107 |
| - } |
108 |
| - |
109 |
| - $root = $this->request->root(); |
110 |
| - |
111 |
| - return str_replace($root, "{$root}/{$locale}", $this->request->fullUrl()); |
112 |
| - } |
113 |
| - |
114 |
| - /** |
115 |
| - * Check if current locale is default locale or not. |
116 |
| - * |
117 |
| - * @return bool |
118 |
| - */ |
119 |
| - protected function isCurrentLocaleDefault() |
120 |
| - { |
121 |
| - return ($this->config->get('app.locale') == $this->config->get('app.fallback_locale')); |
122 |
| - } |
123 |
| - |
| 9 | +class LocaleUrlGenerator |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Instance of config. |
| 13 | + * |
| 14 | + * @var \Illuminate\Contracts\Config\Repository |
| 15 | + */ |
| 16 | + protected $config; |
| 17 | + |
| 18 | + /** |
| 19 | + * Instance of request. |
| 20 | + * |
| 21 | + * @var \Illuminate\Http\Request |
| 22 | + */ |
| 23 | + protected $request; |
| 24 | + |
| 25 | + /** |
| 26 | + * Instance of url generator. |
| 27 | + * |
| 28 | + * @var \Illuminate\Contracts\Routing\UrlGenerator |
| 29 | + */ |
| 30 | + protected $url; |
| 31 | + |
| 32 | + /** |
| 33 | + * Create LocaleUrlGenerator instance. |
| 34 | + * |
| 35 | + * @param \Illuminate\Contracts\Config\Repository $config |
| 36 | + * @param \Illuminate\Http\Request $request |
| 37 | + * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
| 38 | + */ |
| 39 | + function __construct(Config $config, Request $request, UrlGenerator $url) |
| 40 | + { |
| 41 | + $this->config = $config; |
| 42 | + $this->request = $request; |
| 43 | + $this->url = $url; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Generate a absolute URL to the given path. |
| 48 | + * |
| 49 | + * @param string $path |
| 50 | + * @param mixed $extra |
| 51 | + * @param bool $secure |
| 52 | + * @return string |
| 53 | + */ |
| 54 | + public function url($path, $extra = [], $secure = null) |
| 55 | + { |
| 56 | + if ($this->isCurrentLocaleDefault()) { |
| 57 | + return $this->url->to($path, $extra, $secure); |
| 58 | + } |
| 59 | + |
| 60 | + $path = $this->config->get('app.locale') . '/' . $path; |
| 61 | + |
| 62 | + return $this->url->to($path, $extra, $secure); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the URL to a named route. |
| 67 | + * |
| 68 | + * @param string $name |
| 69 | + * @param mixed $parameters |
| 70 | + * @param bool $absolute |
| 71 | + * @return string |
| 72 | + */ |
| 73 | + public function route($name, $parameters = [], $absolute = true) |
| 74 | + { |
| 75 | + if ($this->isCurrentLocaleDefault()) { |
| 76 | + return $this->url->route($name, $parameters, $absolute); |
| 77 | + } |
| 78 | + |
| 79 | + $name = $this->config->get('app.locale') . '.' . $name; |
| 80 | + |
| 81 | + return $this->url->route($name, $parameters, $absolute); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the URL to a different locale. |
| 86 | + * |
| 87 | + * @param string $locale |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function change($locale) |
| 91 | + { |
| 92 | + if ($locale == $this->config->get('app.locale')) { |
| 93 | + return $this->request->fullUrl(); |
| 94 | + } |
| 95 | + |
| 96 | + if ($locale == $this->config->get('app.fallback_locale')) { |
| 97 | + return $this->replaceLocaleString('/'); |
| 98 | + } |
| 99 | + |
| 100 | + if ( ! $this->isCurrentLocaleDefault()) { |
| 101 | + return $this->replaceLocaleString("/{$locale}/"); |
| 102 | + } |
| 103 | + |
| 104 | + $root = $this->request->root(); |
| 105 | + |
| 106 | + return str_replace($root, "{$root}/{$locale}", $this->request->fullUrl()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Replace locale string from uri. |
| 111 | + * |
| 112 | + * @param string $replace |
| 113 | + * @return string |
| 114 | + */ |
| 115 | + protected function replaceLocaleString($replace) |
| 116 | + { |
| 117 | + $preg = ''; |
| 118 | + $count = 0; |
| 119 | + |
| 120 | + foreach ($this->config->get('locale.available_locales') as $key => $available) { |
| 121 | + if ($available != $this->config->get('app.fallback_locale')) { |
| 122 | + $preg = $count > 0 ? $preg.'|'.$available : $available; |
| 123 | + $count++; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return preg_replace('/\/('.$preg.')($|\/)/', $replace, $this->request->fullUrl(), 1); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Check if current locale is default locale or not. |
| 132 | + * |
| 133 | + * @return bool |
| 134 | + */ |
| 135 | + protected function isCurrentLocaleDefault() |
| 136 | + { |
| 137 | + return ($this->config->get('app.locale') == $this->config->get('app.fallback_locale')); |
| 138 | + } |
124 | 139 | }
|
0 commit comments