@@ -7,12 +7,10 @@ class Translator
77{
88 public static $ current ;
99
10- private $ dictionary = array ();
1110 private $ domain ;
11+ private $ dictionary = array ();
1212 private $ context_glue = "\004" ;
13- private $ pluralCount = 2 ;
14- private $ pluralCode = 'return ($n != 1); ' ;
15- private $ pluralFunction ;
13+ private $ plurals = array ();
1614
1715
1816 /**
@@ -38,61 +36,51 @@ public static function initGettextFunctions(Translator $translator)
3836 public function loadTranslations ($ translations )
3937 {
4038 if (is_object ($ translations ) && $ translations instanceof Translations) {
41- $ this -> loadArray ( PhpArray::toArray ($ translations) );
39+ $ translations = PhpArray::toArray ($ translations );
4240 } elseif (is_string ($ translations ) && is_file ($ translations )) {
43- $ this ->loadArray (include $ translations );
44- } elseif (is_array ($ translations )) {
45- $ this ->loadArray ($ translations );
46- } else {
41+ $ translations = include $ translations ;
42+ } elseif (!is_array ($ translations )) {
4743 throw new \InvalidArgumentException ('Invalid Translator: only arrays, files or instance of Translations are allowed ' );
4844 }
4945
46+ foreach ($ translations as $ translation ) {
47+ $ this ->addTranslations ($ translation );
48+ }
49+
5050 return $ this ;
5151 }
5252
53- /**
54- * Loads translations from an array
55- *
56- * @param array $translations
57- */
58- protected function loadArray (array $ translations )
59- {
60- foreach ($ translations as &$ translation ) {
61- $ domain = isset ($ translation ['' ]['domain ' ]) ? $ translation ['' ]['domain ' ] : 'messages ' ;
62-
63- //Set the first domain loaded as default domain
64- if (!$ this ->domain ) {
65- $ this ->domain = $ domain ;
66- }
67-
68- // If a plural form is set we extract those values
69- if (isset ($ translation ['' ]['plural-forms ' ])) {
70- list ($ count , $ code ) = explode ('; ' , $ translation ['' ]['plural-forms ' ]);
71- $ this ->pluralCount = (int ) str_replace ('nplurals= ' , '' , $ count );
72-
73- // extract just the expression turn 'n' into a php variable '$n'.
74- // Slap on a return keyword and semicolon at the end.
75- $ this ->pluralCode = str_replace ('plural= ' , 'return ' , str_replace ('n ' , '$n ' , $ code )).'; ' ;
76- }
77-
78- unset($ translation ['' ]);
79- $ this ->addTranslations ($ translation , $ domain );
80- }
81- }
8253
8354 /**
8455 * Set new translations to the dictionary
8556 *
8657 * @param array $translations
87- * @param null|string $domain
8858 */
89- public function addTranslations (array $ translations, $ domain = null )
59+ public function addTranslations (array $ translations )
9060 {
91- if ($ domain === null ) {
92- $ domain = $ this ->domain ;
61+ $ info = isset ($ translations ['' ]) ? $ translations ['' ] : null ;
62+ unset($ translations ['' ]);
63+
64+ $ domain = isset ($ info ['domain ' ]) ? $ info ['domain ' ] : 'messages ' ;
65+
66+ //Set the first domain loaded as default domain
67+ if (!$ this ->domain ) {
68+ $ this ->domain = $ domain ;
9369 }
9470
9571 if (!isset ($ this ->dictionary [$ domain ])) {
72+ // If a plural form is set we extract those values
73+ $ pluralForms = empty ($ info ['plural-forms ' ]) ? 'nplurals=2; plural=(n != 1) ' : $ info ['plural-forms ' ];
74+
75+ list ($ count , $ code ) = explode ('; ' , $ pluralForms , 2 );
76+
77+ // extract just the expression turn 'n' into a php variable '$n'.
78+ // Slap on a return keyword and semicolon at the end.
79+ $ this ->plurals [$ domain ] = [
80+ 'count ' => (int ) str_replace ('nplurals= ' , '' , $ count ),
81+ 'code ' => str_replace ('plural= ' , 'return ' , str_replace ('n ' , '$n ' , $ code )).'; ' ,
82+ ];
83+
9684 $ this ->dictionary [$ domain ] = $ translations ;
9785 } else {
9886 $ this ->dictionary [$ domain ] = array_replace_recursive ($ this ->dictionary [$ domain ], $ translations );
@@ -236,7 +224,7 @@ public function dpgettext($domain, $context, $original)
236224 */
237225 public function dnpgettext ($ domain , $ context , $ original , $ plural , $ value )
238226 {
239- $ key = $ this ->isPlural ($ value );
227+ $ key = $ this ->isPlural ($ domain , $ value );
240228 $ translation = $ this ->getTranslation ($ domain , $ context , $ original );
241229
242230 if (isset ($ translation [$ key ]) && $ translation [$ key ] !== '' ) {
@@ -250,22 +238,27 @@ public function dnpgettext($domain, $context, $original, $plural, $value)
250238 * Executes the plural decision code given the number to decide which
251239 * plural version to take.
252240 *
241+ * @param string $domain
253242 * @param string $n
254243 * @return int
255244 */
256- public function isPlural ($ n )
245+ public function isPlural ($ domain , $ n )
257246 {
258- if (!$ this ->pluralFunction ) {
259- $ this ->pluralFunction = create_function ('$n ' , self ::fixTerseIfs ($ this ->pluralCode ));
247+ if (!isset ($ this ->plurals [$ domain ])) {
248+ throw new \InvalidArgumentException ("The gettext domain ' $ domain' is not loaded " );
249+ }
250+
251+ if (!isset ($ this ->plurals [$ domain ]['function ' ])) {
252+ $ this ->plurals [$ domain ]['function ' ] = create_function ('$n ' , self ::fixTerseIfs ($ this ->plurals [$ domain ]['code ' ]));
260253 }
261254
262- if ($ this ->pluralCount <= 2 ) {
263- return (call_user_func ($ this ->pluralFunction , $ n )) ? 2 : 1 ;
255+ if ($ this ->plurals [ $ domain ][ ' count ' ] <= 2 ) {
256+ return (call_user_func ($ this ->plurals [ $ domain ][ ' function ' ] , $ n )) ? 2 : 1 ;
264257 }
265258
266259 // We need to +1 because while (GNU) gettext codes assume 0 based,
267260 // this gettext actually stores 1 based.
268- return (call_user_func ($ this ->pluralFunction , $ n )) + 1 ;
261+ return (call_user_func ($ this ->plurals [ $ domain ][ ' function ' ] , $ n )) + 1 ;
269262 }
270263
271264 /**
0 commit comments