Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
locale_manager.inc
1 <?php
18 require_once SQ_FUDGE_PATH.'/general/text.inc';
19 
20 
33 {
34 
35 
42  public $locale_stack = Array();
43 
44 
51  public $_strings = Array();
52 
53 
60  public $_errors = Array();
61 
62 
69  public $_internal_messages = Array();
70 
71 
79  private $_assets_included = Array();
80 
81 
89  private $_packages_included = Array();
90 
91 
99  private $_core_included = Array();
100 
101 
108  function __construct()
109  {
110  $this->MySource_Object();
111 
112  }//end constructor
113 
114 
122  function __destruct()
123  {
124  if (array_get_index($this->_tmp, 'strings_modified', FALSE) && isset($GLOBALS['SQ_SYSTEM'])) {
125  $this->locale_stack = Array();
126  // Attempt to store me so that I don't have to go through all that again.
127  $deja_vu = $GLOBALS['SQ_SYSTEM']->getDejaVu();
128  if ($deja_vu) {
129  $deja_vu->remember(SQ_DEJA_VU_LOCALE, NULL, $this);
130  }
131  }
132 
133  }//end destructor
134 
135 
144  public function getCurrentLocale()
145  {
146  if (count($this->locale_stack) <= 0) return FALSE; // no locale is current
147  return $this->locale_stack[0];
148 
149  }//end getCurrentLocale()
150 
151 
161  public function getLocaleName($locale_code=NULL)
162  {
163  if (is_null($locale_code)) {
164  $locale_code = $this->getCurrentLocale();
165  }
166  require SQ_FUDGE_PATH.'/standards_lists/locales.inc';
167  return array_get_index($standards_lists_locales, $locale_code, 'Unknown');
168 
169  }//end getLocaleName()
170 
171 
180  public function setCurrentLocale($locale)
181  {
182  array_unshift($this->locale_stack, $locale);
183  $this->includeCoreStrings($locale);
184 
185  return TRUE;
186 
187  }//end setCurrentLocale()
188 
189 
196  public function restoreCurrentLocale()
197  {
198  if (count($this->locale_stack) <= 0) return FALSE; // stack would underflow if we tried to shift this out
199  array_shift($this->locale_stack);
200  return TRUE;
201 
202  }//end restoreCurrentLocale()
203 
204 
217  public function getLocaleParts($locale)
218  {
219  $locale_parts = Array(NULL, NULL, NULL);
220 
221  preg_match('|([A-Za-z]{2})(\_([A-Za-z]{2})(\@([A-Za-z]+))?)?|', $locale, $matches);
222 
223  // switch on the number of match expansions we get - if we have all 3
224  // parts we will have 6 matches, if we have language and country we will
225  // have 4, if just the language we will have 2. This means the time can
226  // be ripe for a fall-through switch
227 
228  switch (count($matches)) {
229  case 6:
230  $locale_parts[2] = $matches[5];
231  // no break; fall through to case 4
232 
233  case 4:
234  $locale_parts[1] = $matches[3];
235  // no break; fall through to case 2
236 
237  case 2:
238  $locale_parts[0] = $matches[1];
239  break;
240 
241  default:
242  trigger_localised_error('SYS0193', E_USER_WARNING, $locale);
243  return FALSE;
244  break;
245  }//end switch
246 
247  return $locale_parts;
248 
249  }//end getLocaleParts()
250 
251 
267  public function getCumulativeLocaleParts($locale, $lang_sep='_', $var_sep='@')
268  {
269  $locale_parts = $this->getLocaleParts($locale);
270 
271  $cum_locale_parts = Array();
272  $cum_locale_parts[] = $locale_parts[0];
273 
274  if (count($cum_locale_parts) >= 2) {
275  $cum_locale_parts[] = $cum_locale_parts[0].$lang_sep.$locale_parts[1];
276 
277  if (count($cum_locale_parts) >= 3) {
278  $cum_locale_parts[] = $cum_locale_parts[1].$var_sep.$locale_parts[2];
279  }
280  }
281 
282  return $cum_locale_parts;
283 
284  }//end getCumulativeLocaleParts()
285 
286 
295  public function getString($string_code)
296  {
297  $locale = $this->getCurrentLocale();
298 
299  // if second parameter is an array, use that as our list of params
300  if (!is_array($func_args = func_get_arg(1))) {
301  $func_args = func_get_args();
302  $func_args = array_slice($func_args,1);
303  }
304 
305  if (!isset($this->_strings[$locale])) {
306  return "Source string for $string_code not found";
307  }
308 
309  if (!isset($this->_strings[$locale][$string_code])) {
310  return "Source string for $string_code not found for locale $locale";
311  }
312 
313  return vsprintf($this->_strings[$locale][$string_code],$func_args);
314 
315  }//end getString()
316 
317 
327  public function includeAssetStrings($type_code, $locale=NULL)
328  {
329  if (!isset($this->_assets_included[$type_code])) {
330  $this->_assets_included[$type_code] = Array();
331  }
332 
333  if ($locale == NULL) {
334  $locale = $this->getCurrentLocale();
335  }
336 
337 
338  if (!array_get_index($this->_assets_included[$type_code], $locale, FALSE)) {
339  $this->_tmp['strings_modified'] = TRUE; // So that we know we've changed.
340  $this->_assets_included[$type_code][$locale] = TRUE;
341 
342  // include packages, changing \ for / so it works on Windows systems
343  if ($type_code != 'asset') {
344  $asset_dir = str_replace('\\', '/', SQ_SYSTEM_ROOT.'/'.$GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code, 'dir'));
345 
346  if (strpos($asset_dir, str_replace('\\', '/', SQ_CORE_PACKAGE_PATH)) !== FALSE) {
347  $this->includePackageStrings('__core__', $locale);
348  } else {
349  $asset_dir = str_replace(str_replace('\\', '/', SQ_PACKAGES_PATH).'/', '', $asset_dir);
350  $package = substr($asset_dir, 0, strpos($asset_dir, '/'));
351  $this->includePackageStrings($package, $locale);
352  }
353  }
354 
355  list($lang, $country, $variant) = $this->getLocaleParts($locale);
356  $locales = Array($lang);
357  if (!empty($country)) {
358  $locales[] = $lang.'_'.$country;
359  if (!empty($variant)) {
360  $locales[] = $lang.'_'.$country.'@'.$variant;
361  }
362  }
363 
364  foreach ($locales as $this_locale) {
365  $file = SQ_DATA_PATH.'/private/asset_types/'.$type_code.'/strings.'.$this_locale;
366 
367  if (is_file($file)) {
368  $strings = unserialize(file_get_contents($file));
369 
370  if (!isset($this->_strings[$locale])) {
371  $this->_strings[$locale] = Array();
372  }
373  $this->_strings[$locale] = array_merge($this->_strings[$locale], $strings);
374  }
375  }
376  }//end if
377 
378  }//end includeAssetStrings()
379 
380 
390  public function includePackageStrings($package_name, $locale=NULL)
391  {
392  if (!isset($this->_packages_included[$package_name])) {
393  $this->_packages_included[$package_name] = Array();
394  }
395 
396  if ($locale == NULL) {
397  $locale = $this->getCurrentLocale();
398  }
399 
400  if (!array_get_index($this->_packages_included[$package_name], $locale, FALSE)) {
401  $this->_tmp['strings_modified'] = TRUE; // So that we know we've changed.
402  $this->_packages_included[$package_name][$locale] = TRUE;
403 
404  list($lang, $country, $variant) = $this->getLocaleParts($locale);
405  $locales = Array($lang);
406  if (!empty($country)) {
407  $locales[] = $lang.'_'.$country;
408  if (!empty($variant)) {
409  $locales[] = $lang.'_'.$country.'@'.$variant;
410  }
411  }
412 
413  foreach ($locales as $this_locale) {
414  $file = SQ_DATA_PATH.'/private/packages/'.$package_name.'/strings.'.$this_locale;
415  if (file_exists($file)) {
416  $strings = unserialize(file_get_contents($file));
417 
418  if (!isset($this->_strings[$locale])) {
419  $this->_strings[$locale] = Array();
420  }
421  $this->_strings[$locale] = array_merge($this->_strings[$locale], $strings);
422  }
423 
424  $file = SQ_DATA_PATH.'/private/packages/'.$package_name.'/errors.'.$this_locale;
425  if (is_file($file)) {
426  $strings = unserialize(file_get_contents($file));
427 
428  if (!isset($this->_errors[$locale])) {
429  $this->_errors[$locale] = Array();
430  }
431  $this->_errors[$locale] = array_merge($this->_errors[$locale], $strings);
432  }
433 
434  $file = SQ_DATA_PATH.'/private/packages/'.$package_name.'/internal_messages.'.$this_locale;
435  if (is_file($file)) {
436  $strings = unserialize(file_get_contents($file));
437 
438  if (!isset($this->_internal_messages[$locale])) {
439  $this->_internal_messages[$locale] = Array();
440  }
441  $this->_internal_messages[$locale] = array_merge($this->_internal_messages[$locale], $strings);
442  }
443  }
444  }//end if
445 
446  }//end includePackageStrings()
447 
448 
457  public function includeCoreStrings($locale=NULL)
458  {
459  if ($locale == NULL) {
460  $locale = $this->getCurrentLocale();
461  }
462 
463  if (!array_get_index($this->_core_included, $locale, FALSE)) {
464  $this->_tmp['strings_modified'] = TRUE; // So that we know we've changed.
465  $this->_core_included[$locale] = TRUE;
466 
467  list($lang, $country, $variant) = $this->getLocaleParts($locale);
468  $locales = Array($lang);
469  if (!empty($country)) {
470  $locales[] = $lang.'_'.$country;
471  if (!empty($variant)) {
472  $locales[] = $lang.'_'.$country.'@'.$variant;
473  }
474  }
475 
476  foreach ($locales as $this_locale) {
477  $file = SQ_DATA_PATH.'/private/system/core/strings.'.$this_locale;
478  if (is_file($file)) {
479  $strings = unserialize(file_get_contents($file));
480 
481  if (!isset($this->_strings[$locale])) {
482  $this->_strings[$locale] = Array();
483  }
484  $this->_strings[$locale] = array_merge($this->_strings[$locale], $strings);
485  }
486 
487  $file = SQ_DATA_PATH.'/private/system/core/errors.'.$this_locale;
488  if (is_file($file)) {
489  $strings = unserialize(file_get_contents($file));
490 
491  if (!isset($this->_errors[$locale])) {
492  $this->_errors[$locale] = Array();
493  }
494  $this->_errors[$locale] = array_merge($this->_errors[$locale], $strings);
495  }
496 
497  $file = SQ_DATA_PATH.'/private/system/core/internal_messages.'.$this_locale;
498  if (is_file($file)) {
499  $strings = unserialize(file_get_contents($file));
500 
501  if (!isset($this->_internal_messages[$locale])) {
502  $this->_internal_messages[$locale] = Array();
503  }
504  $this->_internal_messages[$locale] = array_merge($this->_internal_messages[$locale], $strings);
505  }
506  }
507  }//end if
508 
509  }//end includeCoreStrings()
510 
511 
527  public function raiseError($code, $error_level)
528  {
529  $locale = $this->getCurrentLocale();
530 
531  if (($error_level != E_USER_ERROR) && ($error_level != E_USER_WARNING) && ($error_level != E_USER_NOTICE)) {
532  trigger_error('Invalid error level passed when raising error ['.$code.']: '.$error_level, E_USER_WARNING);
533  $error_level = E_USER_ERROR;
534  }
535 
536  // if third parameter is an array, use that as our list of params
537  // otherwise, everything after 2nd param is our list of params that we
538  // will pump into the error, sprintf() style
539  if (func_num_args() == 2) {
540  $func_args = Array();
541  } else if (!is_array($func_args = func_get_arg(2))) {
542  $func_args = func_get_args();
543  $func_args = array_slice($func_args,2);
544  }
545 
546  $error_msg = $this->getErrorMessage($code, $func_args);
547  trigger_error($error_msg, $error_level);
548 
549  }//end raiseError()
550 
551 
560  public function getErrorMessage($code)
561  {
562  $locale = $this->getCurrentLocale();
563 
564  // if second parameter is an array, use that as our list of params
565  // otherwise, everything after 1st param is our list of params that we
566  // will pump into the error, sprintf() style
567  if (func_num_args() == 1) {
568  $func_args = Array();
569  } else if (!is_array($func_args = func_get_arg(1))) {
570  $func_args = func_get_args();
571  $func_args = array_slice($func_args,1);
572  }
573 
574  $valid_code = TRUE;
575  if (!isset($this->_errors[$locale])) {
576  $valid_code = FALSE;
577  }
578 
579  if (!isset($this->_errors[$locale][$code])) {
580  $valid_code = FALSE;
581  }
582 
583  if ($valid_code) {
584  $error_msg = vsprintf($this->_errors[$locale][$code], $func_args).' ['.$code.']';
585  } else {
586  $error_msg = 'Error text not found for this code and locale ['.$code.']';
587  if (!empty($func_args)) {
588  $error_msg .= "\n".var_export($func_args, 1);
589  }
590  }
591 
592  return $error_msg;
593 
594  }//end getErrorMessage()
595 
596 
607  public function getInternalMessageSubject($type, $keywords, $locale=NULL)
608  {
609  if (is_null($locale)) {
610  $locale = $this->getCurrentLocale();
611  }
612 
613  $valid_code = TRUE;
614  if (!isset($this->_internal_messages[$locale])) {
615  $valid_code = FALSE;
616  }
617 
618  if (!isset($this->_internal_messages[$locale][$type])) {
619  $valid_code = FALSE;
620  }
621 
622  if ($valid_code) {
623  $subject = $this->_internal_messages[$locale][$type]['subject'];
624  replace_keywords($subject, $keywords);
625  } else {
626  $subject = 'Internal message translation not found for this code and locale ['.$type.']';
627  }
628 
629  return $subject;
630 
631  }//end getInternalMessageSubject()
632 
633 
644  public function getInternalMessageBody($type, $keywords, $locale=NULL)
645  {
646  if (is_null($locale)) {
647  $locale = $this->getCurrentLocale();
648  }
649 
650  $valid_code = TRUE;
651  if (!isset($this->_internal_messages[$locale])) {
652  $valid_code = FALSE;
653  }
654 
655  if (!isset($this->_internal_messages[$locale][$type])) {
656  $valid_code = FALSE;
657  }
658 
659  if ($valid_code) {
660  $body = $this->_internal_messages[$locale][$type]['body'];
661  replace_keywords($body, $keywords);
662  } else {
663  $body = 'Internal message translation not found for this code and locale ['.$type.']';
664  }
665 
666  return $body;
667 
668  }//end getInternalMessageBody()
669 
670 
677  public function getJavascriptIncludes()
678  {
679  $res = Array(sq_web_path('lib').'/js/translation.js'); // we always need this one
680  list($lang, $country, $variant) = $this->getLocaleParts($GLOBALS['SQ_SYSTEM']->lm->getCurrentLocale());
681  if (file_exists(SQ_DATA_PATH.'/public/system/core/js_strings.'.$lang.'.js')) {
682  $res[] = sq_web_path('data').'/system/core/js_strings.'.$lang.'.js';
683  }
684  if (!empty($country)) {
685  if (file_exists(SQ_DATA_PATH.'/public/system/core/js_strings.'.$lang.'_'.$country.'.js')) {
686  $res[] = sq_web_path('data').'/system/core/js_strings.'.$lang.'_'.$country.'.js';
687  }
688 
689  if (!empty($variant)) {
690  if (file_exists(SQ_DATA_PATH.'/public/system/core/js_strings.'.$lang.'_'.$country.'@'.$variant.'.js')) {
691  $res[] = sq_web_path('data').'/system/core/js_strings.'.$lang.'_'.$country.'@'.$variant.'.js';
692  }
693  }
694  }
695  return $res;
696 
697  }//end getJavascriptIncludes()
698 
699 
700 }//end class
701 ?>