1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Usefull functions to support multi-language feedback |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait CommonLibLocale |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
protected $commonLibFlags = null; |
40
|
|
|
protected $tCmnLb = null; |
41
|
|
|
protected $tCmnSession = null; |
42
|
|
|
protected $tCmnSuperGlobals = null; |
43
|
|
|
|
44
|
|
|
private function getCommonLocaleFolder() |
45
|
|
|
{ |
46
|
|
|
$pathes = explode(DIRECTORY_SEPARATOR, __DIR__); |
47
|
|
|
$pathDepth = count($pathes); |
48
|
|
|
$localePath = []; |
49
|
|
|
foreach ($pathes as $key => $value) { |
50
|
|
|
if ($key < ($pathDepth - 1)) { |
51
|
|
|
$localePath[] = $value; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return implode(DIRECTORY_SEPARATOR, $localePath); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Stores given language or default one into global session variable |
59
|
|
|
* (In order to avoid potential language injections from other applications session will revert |
60
|
|
|
* to the default language if application one is not among the one are not supported here) |
61
|
|
|
* |
62
|
|
|
* @return NOTHING |
63
|
|
|
*/ |
64
|
|
|
private function handleLanguageIntoSession() |
65
|
|
|
{ |
66
|
|
|
$this->settingsCommonLib(); |
67
|
|
|
$this->initializeSprGlbAndSession(); |
68
|
|
|
if (is_null($this->tCmnSuperGlobals->get('lang'))) { |
69
|
|
|
$this->tCmnSession->set('lang', $this->commonLibFlags['default_language']); |
70
|
|
|
} elseif (!is_null($this->tCmnSuperGlobals->get('lang'))) { |
71
|
|
|
$this->tCmnSession->set('lang', filter_var($this->tCmnSuperGlobals->get('lang'), FILTER_SANITIZE_STRING)); |
72
|
|
|
} |
73
|
|
|
if (!array_key_exists($this->tCmnSession->get('lang'), $this->commonLibFlags['available_languages'])) { |
74
|
|
|
$this->tCmnSession->set('lang', $this->commonLibFlags['default_language']); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Takes care of instatiation of localization libraries |
80
|
|
|
* used within current module for multi-languages support |
81
|
|
|
* |
82
|
|
|
* @return NOTHING |
83
|
|
|
*/ |
84
|
|
|
private function handleLocalizationCommon() |
85
|
|
|
{ |
86
|
|
|
$this->handleLanguageIntoSession(); |
87
|
|
|
$localizationFile = $this->getCommonLocaleFolder() . '/locale/' |
88
|
|
|
. $this->tCmnSuperGlobals->get('lang') . '/LC_MESSAGES/' |
89
|
|
|
. $this->commonLibFlags['localization_domain'] |
90
|
|
|
. '.mo'; |
91
|
|
|
$extrClass = new \Gettext\Extractors\Mo(); |
92
|
|
|
$translations = $extrClass->fromFile($localizationFile); |
93
|
|
|
$this->tCmnLb = new \Gettext\Translator(); |
94
|
|
|
$this->tCmnLb->loadTranslations($translations); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function initializeSprGlbAndSession() |
98
|
|
|
{ |
99
|
|
|
$rqst = new \Symfony\Component\HttpFoundation\Request; |
100
|
|
|
$this->tCmnSuperGlobals = $rqst->createFromGlobals(); |
101
|
|
|
$sBridge = new \Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage(); |
102
|
|
|
$this->tCmnSession = new \Symfony\Component\HttpFoundation\Session\Session($sBridge); |
103
|
|
|
$this->tCmnSession->start(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Central function to deal with multi-language messages |
108
|
|
|
* |
109
|
|
|
* @param string $localizedStringCode |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function lclMsgCmn($localizedStringCode) |
113
|
|
|
{ |
114
|
|
|
if (is_null($this->commonLibFlags)) { |
115
|
|
|
$this->settingsCommonLib(); |
116
|
|
|
$this->handleLocalizationCommon(); |
117
|
|
|
} |
118
|
|
|
return $this->tCmnLb->gettext($localizedStringCode); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function lclMsgCmnNumber($singularString, $pluralString, $numberToEvaluate) |
122
|
|
|
{ |
123
|
|
|
if (is_null($this->commonLibFlags)) { |
124
|
|
|
$this->settingsCommonLib(); |
125
|
|
|
$this->handleLocalizationCommon(); |
126
|
|
|
} |
127
|
|
|
return $this->tCmnLb->ngettext($singularString, $pluralString, $numberToEvaluate); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function setNumberFormat($content, $features = null) |
131
|
|
|
{ |
132
|
|
|
$features = $this->setNumberFormatFeatures($features); |
133
|
|
|
$fmt = new \NumberFormatter($features['locale'], $features['style']); |
134
|
|
|
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $features['MinFractionDigits']); |
135
|
|
|
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $features['MaxFractionDigits']); |
136
|
|
|
return $fmt->format($content); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function setNumberFormatFeatures($features) |
140
|
|
|
{ |
141
|
|
|
if (is_null($this->tCmnSession)) { |
142
|
|
|
$this->initializeSprGlbAndSession(); |
143
|
|
|
} |
144
|
|
|
if (is_null($features)) { |
145
|
|
|
$features = [ |
146
|
|
|
'locale' => $this->tCmnSuperGlobals->get('lang'), |
147
|
|
|
'style' => \NumberFormatter::DECIMAL, |
148
|
|
|
'MinFractionDigits' => 0, |
149
|
|
|
'MaxFractionDigits' => 0, |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
if (!isset($features['locale'])) { |
153
|
|
|
$features['locale'] = $this->tCmnSuperGlobals->get('lang'); |
154
|
|
|
} |
155
|
|
|
if (!isset($features['style'])) { |
156
|
|
|
$features['style'] = \NumberFormatter::DECIMAL; |
157
|
|
|
} |
158
|
|
|
return $features; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Settings |
163
|
|
|
* |
164
|
|
|
* @return NOTHING |
165
|
|
|
*/ |
166
|
|
|
private function settingsCommonLib() |
167
|
|
|
{ |
168
|
|
|
$this->commonLibFlags = [ |
169
|
|
|
'available_languages' => [ |
170
|
|
|
'en_US' => 'US English', |
171
|
|
|
'ro_RO' => 'Română', |
172
|
|
|
'it_IT' => 'Italiano', |
173
|
|
|
], |
174
|
|
|
'default_language' => 'en_US', |
175
|
|
|
'localization_domain' => 'common-locale' |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|