9
9
10
10
namespace ElCheco \Translator ;
11
11
12
-
13
12
use NumberFormatter ;
14
13
use Psr \Log \LoggerInterface ;
15
- use function array_key_exists ;
16
- use function array_shift ;
17
- use function end ;
18
- use function func_get_args ;
19
- use function gettype ;
20
- use function is_numeric ;
21
- use function is_object ;
22
- use function key ;
23
- use function method_exists ;
24
- use function sprintf ;
25
14
26
15
final class Translator implements TranslatorInterface
27
16
{
28
-
29
-
30
17
private const ZERO_INDEX = -1 ;
31
18
32
19
/**
@@ -46,9 +33,9 @@ final class Translator implements TranslatorInterface
46
33
/**
47
34
* fallback locale
48
35
*
49
- * @var string
36
+ * @var null| string
50
37
*/
51
- private $ fallBackLocale = ' en_US ' ;
38
+ private $ fallBackLocale ;
52
39
53
40
/**
54
41
* @var DictionaryInterface|null
@@ -100,21 +87,25 @@ public function setFallbackLocale(string $locale): TranslatorInterface
100
87
}
101
88
102
89
103
- public function translate ($ message , $ count = null )
90
+ public function translate ($ message , ... $ parameters ): string
104
91
{
105
92
// avoid processing for empty values
106
93
if ($ message === null || $ message === '' ) {
107
94
return '' ;
108
95
}
109
96
97
+ if (!isset ($ parameters [0 ])) {
98
+ $ parameters [0 ] = null ;
99
+ }
100
+
110
101
// convert to string
111
102
if (\is_object ($ message ) && \method_exists ($ message , '__toString ' )) {
112
103
$ message = (string ) $ message ;
113
104
}
114
105
115
106
// numbers are formatted using locale settings (count parameter is used to define decimals)
116
107
if (\is_numeric ($ message )) {
117
- return $ this ->formatNumber ($ message , (int ) $ count );
108
+ return $ this ->formatNumber ($ message , (int ) $ parameters [ ' count ' ] );
118
109
}
119
110
120
111
// check message to be string
@@ -139,15 +130,15 @@ public function translate($message, $count = null)
139
130
// process plural
140
131
if (\is_array ($ translation )) {
141
132
142
- if ($ count === null ) {
133
+ if (( isset ( $ parameters [ 0 ]) && $ parameters [ 0 ] === null ) ) {
143
134
$ this ->warn ('Multiple plural forms are available (message: %s), but the $count is null. ' , $ message );
144
135
145
136
// fallback to highest
146
- $ count = \max (array_keys ($ translation ));
137
+ $ parameters [ 0 ] = \max (\ array_keys ($ translation ));
147
138
}
148
139
149
140
$ t = new Translation ($ translation );
150
- $ result = $ t ->get ($ count );
141
+ $ result = $ t ->get ($ parameters [ 0 ] );
151
142
152
143
}
153
144
@@ -157,24 +148,18 @@ public function translate($message, $count = null)
157
148
}
158
149
}
159
150
160
- // process parameters
161
- $ args = \func_get_args ();
162
-
163
- // remove message
164
- \array_shift ($ args );
165
-
166
151
// remove count if not provided or explicitly set to null
167
- if ($ count === null ) {
168
- \array_shift ($ args );
152
+ if ($ parameters [ 0 ] === null ) {
153
+ \array_shift ($ parameters );
169
154
}
170
155
171
- if (\count ($ args )) {
156
+ if (\count ($ parameters )) {
172
157
173
158
// preserve some nette placeholders
174
159
$ template = \str_replace (['%label ' , '%name ' , '%value ' ], ['%%label ' , '%%name ' , '%%value ' ], $ result );
175
160
176
161
// apply parameters
177
- $ result = \vsprintf ($ template , $ args );
162
+ $ result = \vsprintf ($ template , $ parameters );
178
163
}
179
164
180
165
return $ result ;
0 commit comments