| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TChoiceFormat, I18N choice format component.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TChoiceFormat.php 2482 2008-07-30 02:07:13Z knut $
|
|
|
10 |
* @package System.I18N
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Get the ChoiceFormat class.
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.I18N.core.ChoiceFormat');
|
|
|
17 |
Prado::using('System.I18N.TTranslate');
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* TChoiceFormat class.
|
|
|
21 |
*
|
|
|
22 |
* This component performs message/string choice translation. The translation
|
|
|
23 |
* source is set in the TGlobalization module. The following example
|
|
|
24 |
* demonstrates a simple 2 choice message translation.
|
|
|
25 |
* <code>
|
|
|
26 |
* <com:TChoiceFormat Value="1">[1] One Apple. |[2] Two Apples</com:TChoiceFormat>
|
|
|
27 |
* </code>
|
|
|
28 |
*
|
|
|
29 |
* The Choice has <b>Value</b> "1" (one), thus the translated string
|
|
|
30 |
* is "One Apple". If the <b>Value</b> is "2", then it will show
|
|
|
31 |
* "Two Apples".
|
|
|
32 |
*
|
|
|
33 |
* The message/string choices are separated by the pipe "|" followed
|
|
|
34 |
* by a set notation of the form
|
|
|
35 |
* # <tt>[1,2]</tt> -- accepts values between 1 and 2, inclusive.
|
|
|
36 |
* # <tt>(1,2)</tt> -- accepts values between 1 and 2, excluding 1 and 2.
|
|
|
37 |
* # <tt>{1,2,3,4}</tt> -- only values defined in the set are accepted.
|
|
|
38 |
* # <tt>[-Inf,0)</tt> -- accepts value greater or equal to negative infinity
|
|
|
39 |
* and strictly less than 0
|
|
|
40 |
* Any non-empty combinations of the delimiters of square and round brackets
|
|
|
41 |
* are acceptable.
|
|
|
42 |
*
|
|
|
43 |
* The string choosen for display depends on the <b>Value</b> property.
|
|
|
44 |
* The <b>Value</b> is evaluated for each set until the Value is found
|
|
|
45 |
* to belong to a particular set.
|
|
|
46 |
*
|
|
|
47 |
* Properties
|
|
|
48 |
* - <b>Value</b>, float,
|
|
|
49 |
* <br>Gets or sets the Value that determines which string choice to display.
|
|
|
50 |
* Since version 3.1.2 the following set notation is also possible.
|
|
|
51 |
*
|
|
|
52 |
* # <tt>{n: n % 10 > 1 && n % 10 < 5}</tt> -- matches numbers like 2, 3, 4, 22, 23, 24
|
|
|
53 |
*
|
|
|
54 |
* Where set is defined by the expression after <tt>n:</tt>. In particular, the expression
|
|
|
55 |
* accepts the following mathematical/logical operators to form a set of logical conditions
|
|
|
56 |
* on the value given by <tt>n</tt>:
|
|
|
57 |
* # <tt><</tt> -- less than.
|
|
|
58 |
* # <tt><=</tt> -- less than equals.
|
|
|
59 |
* # <tt>></tt> -- greater than.
|
|
|
60 |
* # <tt>>=</tt> -- greater than equals.
|
|
|
61 |
* # <tt>==</tt> -- of equal value.
|
|
|
62 |
* # <tt>%</tt> -- modulo, e.g., 1 % 10 equals 1, 11 % 10 equals 1.
|
|
|
63 |
* # <tt>-</tt> -- minus, negative.
|
|
|
64 |
* # <tt>+</tt> -- addition.
|
|
|
65 |
* # <tt>&</tt> -- conditional AND.
|
|
|
66 |
* # <tt>&&</tt> -- condition AND with short circuit.
|
|
|
67 |
* # <tt>|</tt> -- conditional OR.
|
|
|
68 |
* # <tt>||</tt> -- conditional OR with short circuit.
|
|
|
69 |
* # <tt>!</tt> -- negation.
|
|
|
70 |
*
|
|
|
71 |
* Additional round brackets can also be used to perform grouping.
|
|
|
72 |
*
|
|
|
73 |
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
74 |
* @version v1.0, last update on Fri Dec 24 21:38:49 EST 2004
|
|
|
75 |
* @package System.I18N
|
|
|
76 |
*/
|
|
|
77 |
class TChoiceFormat extends TTranslate
|
|
|
78 |
{
|
|
|
79 |
/**
|
|
|
80 |
* @return float the numerical value.
|
|
|
81 |
*/
|
|
|
82 |
public function getValue()
|
|
|
83 |
{
|
|
|
84 |
return $this->getViewState('Value','');
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Sets the numerical choice value
|
|
|
89 |
* @param float the choice value
|
|
|
90 |
*/
|
|
|
91 |
public function setValue($value)
|
|
|
92 |
{
|
|
|
93 |
$this->setViewState('Value',$value,'');
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Display the choosen translated string.
|
|
|
98 |
* Overrides the parent method, also calls parent's renderBody to
|
|
|
99 |
* translate.
|
|
|
100 |
*/
|
|
|
101 |
protected function translateText($text, $subs)
|
|
|
102 |
{
|
|
|
103 |
$text = parent::translateText($text, $subs);
|
|
|
104 |
$choice = new ChoiceFormat();
|
|
|
105 |
$value = $this->getValue();
|
|
|
106 |
$string = $choice->format($text, $value);
|
|
|
107 |
if($string)
|
|
|
108 |
return strtr($string, array('{Value}'=> $value));
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
?>
|