A helper class to convert a CLDR formula to a gettext formula.
More...
|
static | convertFormula ($cldrFormula) |
| Converts a formula from the CLDR representation to the gettext representation. More...
|
|
|
static | convertAtom ($cldrAtom) |
| Converts an atomic part of the CLDR formula to its gettext representation. More...
|
|
static | expandAtom ($atom) |
| Expands an atom containing a range (for instance: 'n == 1,3..5'). More...
|
|
A helper class to convert a CLDR formula to a gettext formula.
Definition at line 9 of file FormulaConverter.php.
◆ convertAtom()
static Gettext\Languages\FormulaConverter::convertAtom |
( |
|
$cldrAtom | ) |
|
|
staticprivate |
Converts an atomic part of the CLDR formula to its gettext representation.
- Parameters
-
string | $cldrAtom | The CLDR formula atom to convert. |
- Exceptions
-
- Returns
- bool|string Returns true if the gettext will always evaluate to true, false if gettext will always evaluate to false, return the gettext formula otherwise.
Definition at line 70 of file FormulaConverter.php.
References $m.
73 $gettextAtom = $cldrAtom;
74 $gettextAtom = str_replace(
' = ',
' == ', $gettextAtom);
75 $gettextAtom = str_replace(
'i',
'n', $gettextAtom);
76 if (preg_match(
'/^n( % \d+)? (!=|==) \d+$/', $gettextAtom)) {
79 if (preg_match(
'/^n( % \d+)? (!=|==) \d+(,\d+|\.\.\d+)+$/', $gettextAtom)) {
80 return self::expandAtom($gettextAtom);
82 if (preg_match(
'/^(?:v|w)(?: % 10+)? == (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
83 return (intval(
$m[1]) === 0) ? true :
false;
85 if (preg_match(
'/^(?:v|w)(?: % 10+)? != (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
86 return (intval(
$m[1]) === 0) ? false :
true;
88 if (preg_match(
'/^(?:f|t)(?: % 10+)? == (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
89 return (intval(
$m[1]) === 0) ? true :
false;
91 if (preg_match(
'/^(?:f|t)(?: % 10+)? != (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
92 return (intval(
$m[1]) === 0) ? false :
true;
94 throw new Exception(
"Unable to convert the formula chunk '$cldrAtom' from CLDR to gettext");
◆ convertFormula()
static Gettext\Languages\FormulaConverter::convertFormula |
( |
|
$cldrFormula | ) |
|
|
static |
Converts a formula from the CLDR representation to the gettext representation.
- Parameters
-
string | $cldrFormula | The CLDR formula to convert. |
- Exceptions
-
- Returns
- bool|string Returns true if the gettext will always evaluate to true, false if gettext will always evaluate to false, return the gettext formula otherwise.
Definition at line 17 of file FormulaConverter.php.
References array.
Referenced by Gettext\Languages\Category\__construct().
19 if (strpbrk($cldrFormula,
'()') !==
false) {
20 throw new Exception(
"Unable to convert the formula '$cldrFormula': parenthesis handling not implemented");
22 $orSeparatedChunks =
array();
23 foreach (explode(
' or ', $cldrFormula) as $cldrFormulaChunk) {
24 $gettextFormulaChunk = null;
25 $andSeparatedChunks =
array();
26 foreach (explode(
' and ', $cldrFormulaChunk) as $cldrAtom) {
27 $gettextAtom = self::convertAtom($cldrAtom);
28 if ($gettextAtom ===
false) {
30 $gettextFormulaChunk =
false;
32 } elseif ($gettextAtom !==
true) {
33 $andSeparatedChunks[] = $gettextAtom;
36 if (!isset($gettextFormulaChunk)) {
37 if (empty($andSeparatedChunks)) {
39 $gettextFormulaChunk =
true;
41 $gettextFormulaChunk = implode(
' && ', $andSeparatedChunks);
43 switch ($gettextFormulaChunk) {
44 case 'n >= 0 && n <= 2 && n != 2':
45 $gettextFormulaChunk =
'n == 0 || n == 1';
50 if ($gettextFormulaChunk ===
true) {
53 } elseif ($gettextFormulaChunk !==
false) {
54 $orSeparatedChunks[] = $gettextFormulaChunk;
57 if (empty($orSeparatedChunks)) {
61 return implode(
' || ', $orSeparatedChunks);
Create styles array
The data for the language used.
◆ expandAtom()
static Gettext\Languages\FormulaConverter::expandAtom |
( |
|
$atom | ) |
|
|
staticprivate |
Expands an atom containing a range (for instance: 'n == 1,3..5').
- Parameters
-
- Exceptions
-
- Returns
- string
Definition at line 102 of file FormulaConverter.php.
References $from, $m, and array.
105 if (preg_match(
'/^(n(?: % \d+)?) (==|!=) (\d+(?:\.\.\d+|,\d+)+)$/', $atom,
$m)) {
109 foreach (explode(
',',
$m[3]) as $range) {
111 if ((!isset($chunk)) && preg_match(
'/^\d+$/', $range)) {
112 $chunk =
"$what $op $range";
114 if ((!isset($chunk)) && preg_match(
'/^(\d+)\.\.(\d+)$/', $range,
$m)) {
117 if (($to -
$from) === 1) {
120 $chunk =
"($what == $from || $what == $to)";
123 $chunk =
"$what != $from && $what == $to";
129 $chunk =
"$what >= $from && $what <= $to";
132 if ($what ===
'n' &&
$from <= 0) {
133 $chunk =
"$what > $to";
135 $chunk =
"($what < $from || $what > $to)";
141 if (!isset($chunk)) {
142 throw new Exception(
"Unhandled range '$range' in '$atom'");
146 if (count($chunks) === 1) {
151 return '('.implode(
' || ', $chunks).
')';
break;
153 return implode(
' && ', $chunks);
156 throw new Exception(
"Unable to expand '$atom'");
Create styles array
The data for the language used.
The documentation for this class was generated from the following file: