1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pmochine\LaravelTongue\Localization; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Application; |
6
|
|
|
use Pmochine\LaravelTongue\Misc\Config; |
7
|
|
|
|
8
|
|
|
class Locale |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Our instance of the Laravel app. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Foundation\Application |
14
|
|
|
*/ |
15
|
|
|
protected $app = ''; |
16
|
|
|
|
17
|
|
|
public function __construct(Application $app) |
18
|
|
|
{ |
19
|
|
|
$this->app = $app; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Gets the current app locale that is set. |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function get(): string |
28
|
|
|
{ |
29
|
|
|
return $this->app->getLocale(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the locale in the app. |
34
|
|
|
* |
35
|
|
|
* @param string $locale |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function set(string $locale): void |
39
|
|
|
{ |
40
|
|
|
$this->app->setLocale($locale); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set and saves locale in app & cookies and sets regions. |
45
|
|
|
* |
46
|
|
|
* @param string $locale |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function save(string $locale): void |
50
|
|
|
{ |
51
|
|
|
$this->set($locale); |
52
|
|
|
$this->saveInCookie($locale); |
53
|
|
|
$this->setRegionalTimeAndMoney($locale); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Save locale in cookie. |
58
|
|
|
* |
59
|
|
|
* @param string $locale |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function saveInCookie(string $locale): void |
63
|
|
|
{ |
64
|
|
|
if (Config::cookieLocalization()) { |
65
|
|
|
Localization::cookie($locale); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setRegionalTimeAndMoney(string $locale): void |
70
|
|
|
{ |
71
|
|
|
// Regional locale such as de_DE, so formatLocalized works in Carbon |
72
|
|
|
$regional = tongue()->speaking('regional', $locale); |
73
|
|
|
|
74
|
|
|
if ($regional && is_string($regional)) { |
75
|
|
|
setlocale(LC_TIME, $regional.'.UTF-8'); |
76
|
|
|
setlocale(LC_MONETARY, $regional.'.UTF-8'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the twist of the tongue. |
82
|
|
|
* Return the direction left or right. |
83
|
|
|
* e.g. for arabic language. |
84
|
|
|
* |
85
|
|
|
* @param string $script |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function scriptDirection(string $script): string |
89
|
|
|
{ |
90
|
|
|
switch ($script) { |
91
|
|
|
case 'Arab': |
92
|
|
|
case 'Hebr': |
93
|
|
|
case 'Mong': |
94
|
|
|
case 'Tfng': |
95
|
|
|
case 'Thaa': |
96
|
|
|
return 'rtl'; |
97
|
|
|
default: |
98
|
|
|
return 'ltr'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Finds the locale in the URL. |
104
|
|
|
* Like en, de... |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function find(): string |
109
|
|
|
{ |
110
|
|
|
return Localization::decipherTongue(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|