|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\Locale; |
|
4
|
|
|
|
|
5
|
|
|
use Locale as PhpLocale; |
|
6
|
|
|
|
|
7
|
|
|
class Locale |
|
8
|
|
|
{ |
|
9
|
11 |
|
public static function extensoOrdinal($number) |
|
10
|
|
|
{ |
|
11
|
11 |
|
return self::numberToWords($number, false, true); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
21 |
|
public static function extensoCurrency($number) |
|
15
|
|
|
{ |
|
16
|
21 |
|
return self::numberToWords($number, true, false); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
19 |
|
public static function extensoCardinal($number) |
|
20
|
|
|
{ |
|
21
|
19 |
|
return self::numberToWords($number, false, false); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function extenso($number) |
|
25
|
|
|
{ |
|
26
|
|
|
return self::extensoCurrency($number); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
46 |
|
protected static function translate($currency = false, $ordinals = false) |
|
30
|
|
|
{ |
|
31
|
46 |
|
$translate = array(); |
|
32
|
|
|
|
|
33
|
46 |
|
if ($ordinals) { |
|
34
|
8 |
|
$translate['singular'] = array("", "", "milésimo", "milhão", "bilhão", "trilhão", "quatrilhão"); |
|
35
|
8 |
|
$translate['plural'] = array("", "", "milésimo", "milhões", "bilhões", "trilhões", "quatrilhões"); |
|
36
|
8 |
|
$translate['centanas'] = array("", "centésimo", "ducentésimo", "trecentésimo", "quadrigentésimo", "quingentésimo", "sexcentésimo", "septigentésimo", "octigentésimo", "nongentésimo"); |
|
37
|
8 |
|
$translate['dezenas'] = array("", "décimo", "vigésimo", "trigésimo", "quadragésimo", "quinquagésimo", "sexagésimo", "septuagésimo", "octogésimo", "nonagésimo"); |
|
38
|
8 |
|
$translate['dezenas10'] = array("décimo", "décimo primeiro", "décimo segundo", "décimo terceiro", "décimo quarto", "décimo quinto", "décimo sexto", "décimo sétimo", "décimo oitavo", "décimo nono"); |
|
39
|
8 |
|
$translate['unidades'] = array("", "primeiro", "segundo", "terceiro", "quarto", "quinto", "sexto", "sétimo", "oitavo", "nono"); |
|
40
|
8 |
|
$translate['cento'] = "centésimo"; |
|
41
|
8 |
|
$translate['separator'] = " "; |
|
42
|
8 |
|
$translate['preposicao'] = " "; |
|
43
|
8 |
|
$translate['zero'] = "-"; |
|
44
|
8 |
|
} else { |
|
45
|
38 |
|
$translate['singular'] = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão"); |
|
46
|
38 |
|
$translate['plural'] = array("", "", "mil", "milhões", "bilhões", "trilhões", "quatrilhões"); |
|
47
|
38 |
|
$translate['centanas'] = array("", "cem", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"); |
|
48
|
38 |
|
$translate['dezenas'] = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa"); |
|
49
|
38 |
|
$translate['dezenas10'] = array("dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"); |
|
50
|
38 |
|
$translate['unidades'] = array("", "um", "dois", "tres", "quatro", "cinco", "seis", "sete", "oito", "nove"); |
|
51
|
38 |
|
$translate['cento'] = "cento"; |
|
52
|
38 |
|
$translate['separator'] = " e "; |
|
53
|
38 |
|
$translate['preposicao'] = ""; |
|
54
|
38 |
|
$translate['zero'] = "zero"; |
|
55
|
38 |
|
$translate['separator_decimal'] = ", "; |
|
56
|
|
|
|
|
57
|
38 |
|
if($currency){ |
|
58
|
20 |
|
$translate['singular'][0] = "centavo"; |
|
59
|
20 |
|
$translate['singular'][1] = "real"; |
|
60
|
20 |
|
$translate['plural'][0] = "centavos"; |
|
61
|
20 |
|
$translate['plural'][1] = "reais"; |
|
62
|
20 |
|
$translate['zero'] = "zero reais"; |
|
63
|
20 |
|
$translate['preposicao'] = " de "; // ex: um milhao de reais |
|
64
|
20 |
|
$translate['separator_decimal'] = " e "; |
|
65
|
20 |
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
46 |
|
return $translate; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Number to Extenso String |
|
73
|
|
|
* |
|
74
|
|
|
* @param float $number |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
51 |
|
protected static function numberToWords($number, $currency = false, $ordinals = false) |
|
79
|
|
|
{ |
|
80
|
51 |
|
$number = trim($number); |
|
81
|
51 |
|
if (preg_match("/[^0-9\.]/", $number)) { |
|
82
|
3 |
|
return $number; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
48 |
|
$number = $ordinals ? intval($number) : floatval($number); |
|
86
|
|
|
|
|
87
|
48 |
|
if ($ordinals && $number >= 1000) { |
|
88
|
2 |
|
return $number.'º'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
46 |
|
$translate = self::translate($currency, $ordinals); |
|
92
|
|
|
|
|
93
|
46 |
|
$number = number_format($number, 2, ".", "."); |
|
94
|
46 |
|
$inteiro = explode(".", $number); |
|
95
|
46 |
|
for ($i=0;$i<count($inteiro);$i++) { |
|
|
|
|
|
|
96
|
46 |
|
for ($ii=strlen($inteiro[$i]);$ii<3;$ii++) { |
|
97
|
46 |
|
$inteiro[$i] = "0".$inteiro[$i]; |
|
98
|
46 |
|
} |
|
99
|
46 |
|
} |
|
100
|
|
|
|
|
101
|
46 |
|
$fim = count($inteiro) - ($inteiro[count($inteiro)-1] > 0 ? 1 : 2); |
|
102
|
46 |
|
$rt = ''; |
|
103
|
46 |
|
$z = 0; |
|
104
|
46 |
|
for ($i=0;$i<count($inteiro);$i++) { |
|
|
|
|
|
|
105
|
46 |
|
$number = $inteiro[$i]; |
|
106
|
46 |
|
if (($number > 100) && ($number < 200)) { |
|
107
|
6 |
|
$rc = $translate['cento']; |
|
108
|
6 |
|
} else { |
|
109
|
46 |
|
$rc = $translate['centanas'][$number[0]]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
46 |
|
if ($number[1] < 2) { |
|
113
|
46 |
|
$rd = ""; |
|
114
|
46 |
|
} else { |
|
115
|
18 |
|
$rd = $translate['dezenas'][$number[1]]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
46 |
|
if ($number > 0) { |
|
119
|
43 |
|
if ($number[1] == 1) { |
|
120
|
11 |
|
$ru = $translate['dezenas10'][$number[2]]; |
|
121
|
11 |
|
} else { |
|
122
|
37 |
|
$ru = $translate['unidades'][$number[2]]; |
|
123
|
|
|
} |
|
124
|
43 |
|
} else { |
|
125
|
38 |
|
$ru = ""; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
46 |
|
$r = $rc; |
|
129
|
46 |
|
if ($rc && ($rd || $ru)) { |
|
130
|
8 |
|
$r .= $translate['separator']; |
|
131
|
8 |
|
} |
|
132
|
|
|
|
|
133
|
46 |
|
$r .= $rd; |
|
134
|
46 |
|
if ($rd && $ru) { |
|
135
|
14 |
|
$r .= $translate['separator']; |
|
136
|
14 |
|
} |
|
137
|
|
|
|
|
138
|
46 |
|
$r .= $ru; |
|
139
|
|
|
|
|
140
|
46 |
|
$t = count($inteiro)-1-$i; |
|
141
|
|
|
|
|
142
|
46 |
|
if ($r) { |
|
143
|
43 |
|
$r .= " "; |
|
144
|
43 |
|
if ($number > 1) { |
|
145
|
40 |
|
$r .= $translate['plural'][$t]; |
|
146
|
40 |
|
} else { |
|
147
|
10 |
|
$r .= $translate['singular'][$t]; |
|
148
|
|
|
} |
|
149
|
43 |
|
} |
|
150
|
|
|
|
|
151
|
46 |
|
if ($number == "000") { |
|
152
|
38 |
|
$z++; |
|
153
|
46 |
|
} elseif ($z > 0) { |
|
154
|
6 |
|
$z--; |
|
155
|
6 |
|
} |
|
156
|
|
|
|
|
157
|
46 |
|
if (($t==1) && ($z>0) && ($inteiro[0] > 0)) { |
|
158
|
13 |
|
if ($z > 1) { |
|
159
|
5 |
|
$r .= $translate['preposicao']; |
|
160
|
5 |
|
} |
|
161
|
|
|
|
|
162
|
13 |
|
$r .= $translate['plural'][$t]; |
|
163
|
13 |
|
} |
|
164
|
|
|
|
|
165
|
46 |
|
if ($r) { |
|
166
|
43 |
|
$rt = $rt; |
|
|
|
|
|
|
167
|
43 |
|
if (($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) { |
|
168
|
|
|
|
|
169
|
12 |
|
$rt .= (!$currency && $t == 0 && $i==$fim) ? ', ' : $translate['separator']; |
|
170
|
|
|
|
|
171
|
12 |
|
} else { |
|
172
|
43 |
|
if ($t == 0 && $rt) { |
|
173
|
2 |
|
$rt .= $currency ? $translate['separator'] : ', '; |
|
174
|
2 |
|
} else { |
|
175
|
43 |
|
$rt .= " "; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
43 |
|
$rt .= $r; |
|
180
|
43 |
|
} |
|
181
|
46 |
|
} |
|
182
|
|
|
|
|
183
|
46 |
|
$rt = preg_replace("/\s+/", " ", $rt); |
|
184
|
46 |
|
$rt = preg_replace("/\s+,\s*/", ", ", $rt); |
|
185
|
|
|
|
|
186
|
46 |
|
return trim($rt) ?: $translate['zero']; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Ufs |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public static function states() |
|
195
|
|
|
{ |
|
196
|
|
|
return config('locales.ufs'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Sigla Ufs |
|
201
|
|
|
* |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
|
|
public static function siglaStates() |
|
205
|
|
|
{ |
|
206
|
|
|
$keys = array_keys(self::ufs()); |
|
|
|
|
|
|
207
|
|
|
return array_combine($keys, $keys); |
|
208
|
|
|
} |
|
209
|
|
|
} |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: