I18NHelper.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * I18NHelper.
  11. *
  12. * @package symfony
  13. * @subpackage helper
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: I18NHelper.php 11700 2008-09-21 10:53:44Z fabien $
  16. */
  17. function __($text, $args = array(), $catalogue = 'messages')
  18. {
  19. if (sfConfig::get('sf_i18n'))
  20. {
  21. return sfContext::getInstance()->getI18N()->__($text, $args, $catalogue);
  22. }
  23. else
  24. {
  25. if (empty($args))
  26. {
  27. $args = array();
  28. }
  29. // replace object with strings
  30. foreach ($args as $key => $value)
  31. {
  32. if (is_object($value) && method_exists($value, '__toString'))
  33. {
  34. $args[$key] = $value->__toString();
  35. }
  36. }
  37. return strtr($text, $args);
  38. }
  39. }
  40. function format_number_choice($text, $args = array(), $number, $catalogue = 'messages')
  41. {
  42. $translated = __($text, $args, $catalogue);
  43. $choice = new sfChoiceFormat();
  44. $retval = $choice->format($translated, $number);
  45. if ($retval === false)
  46. {
  47. throw new sfException(sprintf('Unable to parse your choice "%s".', $translated));
  48. }
  49. return $retval;
  50. }
  51. function format_country($country_iso, $culture = null)
  52. {
  53. $c = sfCultureInfo::getInstance($culture === null ? sfContext::getInstance()->getUser()->getCulture() : $culture);
  54. $countries = $c->getCountries();
  55. return isset($countries[$country_iso]) ? $countries[$country_iso] : '';
  56. }
  57. function format_language($language_iso, $culture = null)
  58. {
  59. $c = sfCultureInfo::getInstance($culture === null ? sfContext::getInstance()->getUser()->getCulture() : $culture);
  60. $languages = $c->getLanguages();
  61. return isset($languages[$language_iso]) ? $languages[$language_iso] : '';
  62. }