| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* I18NHelper.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage helper
|
|
|
16 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: I18NHelper.php 31894 2011-01-24 18:12:37Z fabien $
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
function __($text, $args = array(), $catalogue = 'messages')
|
|
|
21 |
{
|
|
|
22 |
if (sfConfig::get('sf_i18n'))
|
|
|
23 |
{
|
|
|
24 |
return sfContext::getInstance()->getI18N()->__($text, $args, $catalogue);
|
|
|
25 |
}
|
|
|
26 |
else
|
|
|
27 |
{
|
|
|
28 |
if (empty($args))
|
|
|
29 |
{
|
|
|
30 |
$args = array();
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
// replace object with strings
|
|
|
34 |
foreach ($args as $key => $value)
|
|
|
35 |
{
|
|
|
36 |
if (is_object($value) && method_exists($value, '__toString'))
|
|
|
37 |
{
|
|
|
38 |
$args[$key] = $value->__toString();
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
return strtr($text, $args);
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Format a string according to a number.
|
|
|
48 |
*
|
|
|
49 |
* Every segment is separated with |
|
|
|
50 |
* Each segment defines an intervale and a value.
|
|
|
51 |
*
|
|
|
52 |
* For example :
|
|
|
53 |
*
|
|
|
54 |
* * [0]Nobody is logged|[1]There is 1 person logged|(1,+Inf]There are %number persons logged
|
|
|
55 |
*
|
|
|
56 |
* @param string $text Text used for different number values
|
|
|
57 |
* @param array $args Arguments to replace in the string
|
|
|
58 |
* @param int $number Number to use to determine the string to use
|
|
|
59 |
* @param string $catalogue Catalogue for translation
|
|
|
60 |
*
|
|
|
61 |
* @return string Result of the translation
|
|
|
62 |
*/
|
|
|
63 |
function format_number_choice($text, $args = array(), $number, $catalogue = 'messages')
|
|
|
64 |
{
|
|
|
65 |
$translated = __($text, $args, $catalogue);
|
|
|
66 |
|
|
|
67 |
$choice = new sfChoiceFormat();
|
|
|
68 |
|
|
|
69 |
$retval = $choice->format($translated, $number);
|
|
|
70 |
|
|
|
71 |
if ($retval === false)
|
|
|
72 |
{
|
|
|
73 |
throw new sfException(sprintf('Unable to parse your choice "%s".', $translated));
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return $retval;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
function format_country($country_iso, $culture = null)
|
|
|
80 |
{
|
|
|
81 |
$c = sfCultureInfo::getInstance($culture === null ? sfContext::getInstance()->getUser()->getCulture() : $culture);
|
|
|
82 |
$countries = $c->getCountries();
|
|
|
83 |
|
|
|
84 |
return isset($countries[$country_iso]) ? $countries[$country_iso] : '';
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
function format_language($language_iso, $culture = null)
|
|
|
88 |
{
|
|
|
89 |
$c = sfCultureInfo::getInstance($culture === null ? sfContext::getInstance()->getUser()->getCulture() : $culture);
|
|
|
90 |
$languages = $c->getLanguages();
|
|
|
91 |
|
|
|
92 |
return isset($languages[$language_iso]) ? $languages[$language_iso] : '';
|
|
|
93 |
}
|