1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.locale.parsing">
<title>Normalization and Localization</title>
<para>
<classname>Zend_Locale_Format</classname> is an internal component used by
<classname>Zend_Locale</classname>. All locale aware classes use
<classname>Zend_Locale_Format</classname> for normalization and localization of numbers and
dates. Normalization involves parsing input from a variety of data representations, like
dates, into a standardized, structured representation, such as a <acronym>PHP</acronym>
array with year, month, and day elements.
</para>
<para>
The exact same string containing a number or a date might mean different things to people
with different customs and conventions. Disambiguation of numbers and dates requires rules
about how to interpret these strings and normalize the values into a standardized data
structure. Thus, all methods in <classname>Zend_Locale_Format</classname> require a locale
in order to parse the input data.
<note>
<title>Default "root" Locale</title>
<para>
If no locale is specified, then normalization and localization will use the standard
"root" locale, which might yield unexpected behavior, if the input originated in a
different locale, or output for a specific locale was expected.
</para>
</note>
</para>
<sect2 id="zend.locale.number.normalize">
<title>Number normalization: getNumber($input, Array $options)</title>
<para>
There are many <ulink url="http://en.wikipedia.org/wiki/Numeral">number systems</ulink>
different from the common <ulink
url="http://en.wikipedia.org/wiki/Decimal">decimal system</ulink> (e.g. "3.14").
Numbers can be normalized with the <methodname>getNumber()</methodname> function to
obtain the standard decimal representation. for all number-related discussions in this
manual, <ulink
url="http://en.wikipedia.org/wiki/Arabic_numerals">Arabic/European numerals
(0,1,2,3,4,5,6,7,8,9)</ulink> are implied, unless explicitly stated otherwise. The
options array may contain a 'locale' to define grouping and decimal characters. The
array may also have a 'precision' to truncate excess digits from the result.
</para>
<example id="zend.locale.number.normalize.example-1">
<title>Number normalization</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::getNumber('13.524,678',
array('locale' => $locale,
'precision' => 3)
);
print $number; // will return 13524.678
]]></programlisting>
</example>
<sect3 id="zend.locale.number.normalize.precision">
<title>Precision and Calculations</title>
<para>
Since <methodname>getNumber($value, array $options = array())</methodname> can
normalize extremely large numbers, check the result carefully before using finite
precision calculations, such as ordinary <acronym>PHP</acronym> math operations. For
example, <command>if ((string)int_val($number) != $number) {</command> use <ulink
url="http://www.php.net/bc">BCMath</ulink> or <ulink
url="http://www.php.net/gmp">GMP</ulink>. Most <acronym>PHP</acronym>
installations support the BCMath extension.
</para>
<para>
Also, the precision of the resulting decimal representation can be rounded to a
desired length with <methodname>getNumber()</methodname> with the option
'<property>precision</property>'. If no precision is given, no rounding occurs. Use
only <acronym>PHP</acronym> integers to specify the precision.
</para>
<para>
If the resulting decimal representation should be truncated to a desired length
instead of rounded the option '<property>number_format</property>' can be used
instead. Define the length of the decimal representation with the desired length
of zeros. The result will then not be rounded. So if the defined precision within
<property>number_format</property> is zero the value "1.6" will return "1", not "2.
See the example nearby:
</para>
<example id="zend.locale.number.normalize.precision.example-1">
<title>Number normalization with precision</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::getNumber('13.524,678',
array('precision' => 1,
'locale' => $locale)
);
print $number; // will return 13524.7
$number = Zend_Locale_Format::getNumber('13.524,678',
array('number_format' => '#.00',
'locale' => $locale)
);
print $number; // will return 13524.67
]]></programlisting>
</example>
</sect3>
</sect2>
<sect2 id="zend.locale.number.localize">
<title>Number localization</title>
<para>
<methodname>toNumber($value, array $options = array())</methodname> can localize numbers
to the following <link linkend="zend.locale.appendix">supported locales</link>. This
function will return a localized string of the given number in a conventional format for
a specific locale. The 'number_format' option explicitly specifies a non-default number
format for use with <methodname>toNumber()</methodname>.
</para>
<example id="zend.locale.number.localize.example-1">
<title>Number localization</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::toNumber(13547.36,
array('locale' => $locale));
// will return 13.547,36
print $number;
]]></programlisting>
</example>
<para>
<note>
<title>Unlimited length</title>
<para>
<methodname>toNumber()</methodname> can localize numbers with unlimited length.
It is not related to integer or float limitations.
</para>
</note>
</para>
<para>
The same way as within <methodname>getNumber()</methodname>,
<methodname>toNumber()</methodname> handles precision. If no precision is given, the
complete localized number will be returned.
</para>
<example id="zend.locale.number.localize.example-2">
<title>Number localization with precision</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::toNumber(13547.3678,
array('precision' => 2,
'locale' => $locale));
// will return 13.547,37
print $number;
]]></programlisting>
</example>
<para>
Using the option 'number_format' a self defined format for generating a number can be
defined. The format itself has to be given in <acronym>CLDR</acronym> format as
described below. The locale is used to get separation, precision and other number
formatting signs from it. German for example defines ',' as precision separation and in
English the '.' sign is used.
</para>
<table id="zend.locale.number.localize.table-1">
<title>Format tokens for self generated number formats</title>
<tgroup cols="4">
<thead>
<row>
<entry>Token</entry>
<entry>Description</entry>
<entry>Example format</entry>
<entry>Generated output</entry>
</row>
</thead>
<tbody>
<row>
<entry>#0</entry>
<entry>Generates a number without precision and separation</entry>
<entry>#0</entry>
<entry>1234567</entry>
</row>
<row>
<entry>,</entry>
<entry>
Generates a separation with the length from separation to next
separation or to 0
</entry>
<entry>#,##0</entry>
<entry>1,234,567</entry>
</row>
<row>
<entry>#,##,##0</entry>
<entry>
Generates a standard separation of 3 and all following separations with
2
</entry>
<entry>#,##,##0</entry>
<entry>12,34,567</entry>
</row>
<row>
<entry>.</entry>
<entry>Generates a precision</entry>
<entry>#0.#</entry>
<entry>1234567.1234</entry>
</row>
<row>
<entry>0</entry>
<entry>Generates a precision with a defined length</entry>
<entry>#0.00</entry>
<entry>1234567.12</entry>
</row>
</tbody>
</tgroup>
</table>
<example id="zend.locale.number.localize.example-3">
<title>Using a self defined number format</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::toNumber(13547.3678,
array('number_format' => '#,#0.00',
'locale' => 'de')
);
// will return 1.35.47,36
print $number;
$number = Zend_Locale_Format::toNumber(13547.3,
array('number_format' => '#,##0.00',
'locale' => 'de')
);
// will return 13.547,30
print $number;
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.number.test">
<title>Number testing</title>
<para>
<methodname>isNumber($value, array $options = array())</methodname> checks if a given
string is a number and returns <constant>TRUE</constant> or <constant>FALSE</constant>.
</para>
<example id="zend.locale.number.test.example-1">
<title>Number testing</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale();
if (Zend_Locale_Format::isNumber('13.445,36', array('locale' => 'de_AT'))) {
print "Number";
} else {
print "not a Number";
}
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.float.normalize">
<title>Float value normalization</title>
<para>
Floating point values can be parsed with the
<methodname>getFloat($value, array $options = array())</methodname> function. A floating
point value will be returned.
</para>
<example id="zend.locale.float.normalize.example-1">
<title>Floating point value normalization</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::getFloat('13.524,678',
array('precision' => 2,
'locale' => $locale)
);
// will return 13524.68
print $number;
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.float.localize">
<title>Floating point value localization</title>
<para>
<methodname>toFloat()</methodname> can localize floating point values. This function
will return a localized string of the given number.
</para>
<example id="zend.locale.float.localize.example-1">
<title>Floating point value localization</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::toFloat(13547.3655,
array('precision' => 1,
'locale' => $locale)
);
// will return 13.547,4
print $number;
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.float.test">
<title>Floating point value testing</title>
<para>
<methodname>isFloat($value, array $options = array())</methodname> checks if a given
string is a floating point value and returns <constant>TRUE</constant> or
<constant>FALSE</constant>.
</para>
<example id="zend.locale.float.test.example-1">
<title>Floating point value testing</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::isFloat('13.445,36', array('locale' => $locale))) {
print "float";
} else {
print "not a float";
}
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.integer.normalize">
<title>Integer value normalization</title>
<para>
Integer values can be parsed with the <methodname>getInteger()</methodname> function. A
integer value will be returned.
</para>
<example id="zend.locale.integer.normalize.example-1">
<title>Integer value normalization</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::getInteger('13.524,678',
array('locale' => $locale));
// will return 13524
print $number;
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.integer.localize">
<title>Integer point value localization</title>
<para>
<methodname>toInteger($value, array $options = array())</methodname> can localize
integer values. This function will return a localized string of the given number.
</para>
<example id="zend.locale.integer.localize.example-1">
<title>Integer value localization</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
$number = Zend_Locale_Format::toInteger(13547.3655,
array('locale' => $locale));
// will return 13.547
print $number;
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.integer.test">
<title>Integer value testing</title>
<para>
<methodname>isInteger($value, array $options = array())</methodname> checks if a given
string is an integer value and returns <constant>TRUE</constant> or
<constant>FALSE</constant>.
</para>
<example id="zend.locale.integer.test.example-1">
<title>Integer value testing</title>
<programlisting language="php"><![CDATA[
$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::isInteger('13.445', array('locale' => $locale))) {
print "integer";
} else {
print "not an integer";
}
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.locale.numbersystems">
<title>Numeral System Conversion</title>
<para>
<methodname>Zend_Locale_Format::convertNumerals()</methodname> converts digits between
different <ulink url="http://en.wikipedia.org/wiki/Arabic_numerals">numeral
systems</ulink>, including the standard Arabic/European/Latin numeral system
(0,1,2,3,4,5,6,7,8,9), not to be confused with <ulink
url="http://en.wikipedia.org/wiki/Eastern_Arabic_numerals">Eastern Arabic
numerals</ulink> sometimes used with the Arabic language to express numerals.
Attempts to use an unsupported numeral system will result in an exception, to avoid
accidentally performing an incorrect conversion due to a spelling error. All characters
in the input, which are not numerals for the selected numeral system, are copied to the
output with no conversion provided for unit separator characters.
<classname>Zend_Locale</classname>* components rely on the data provided by
<acronym>CLDR</acronym> (see their <ulink
url="http://unicode.org/cldr/data/diff/supplemental/languages_and_scripts.html?sortby=date">list
of scripts grouped by language</ulink>).
</para>
<para>
In <acronym>CLDR</acronym> and hereafter, the Europena/Latin numerals will
be referred to as "Latin" or by the assigned 4-letter code "Latn".
Also, the <acronym>CLDR</acronym> refers to this numeral systems as "scripts".
</para>
<para>
Suppose a web form collected a numeric input expressed using Eastern Arabic digits
"١٠٠". Most software and <acronym>PHP</acronym> functions expect input using Arabic
numerals. Fortunately, converting this input to its equivalent Latin numerals "100"
requires little effort using <methodname>convertNumerals($inputNumeralString,
$sourceNumeralSystem, $destNumeralSystem)</methodname>, which returns the
<varname>$input</varname> with numerals in the script
<varname>$sourceNumeralSystem</varname> converted to the script
<varname>$destNumeralSystem</varname>.
</para>
<example id="zend.locale.numbersystems.example-1">
<title>Converting numerals from Eastern Arabic scripts to European/Latin scripts</title>
<programlisting language="php"><![CDATA[
$arabicScript = "١٠٠"; // Arabic for "100" (one hundred)
$latinScript = Zend_Locale_Format::convertNumerals($arabicScript,
'Arab',
'Latn');
print "\nOriginal: " . $arabicScript;
print "\nNormalized: " . $latinScript;
]]></programlisting>
</example>
<para>
Similarly, any of the supported numeral systems may be converted to any other supported
numeral system.
</para>
<example id="zend.locale.numbersystems.example-2">
<title>Converting numerals from Latin script to Eastern Arabic script</title>
<programlisting language="php"><![CDATA[
$latinScript = '123';
$arabicScript = Zend_Locale_Format::convertNumerals($latinScript,
'Latn',
'Arab');
print "\nOriginal: " . $latinScript;
print "\nLocalized: " . $arabicScript;
]]></programlisting>
</example>
<example id="zend.locale.numbersystems.example-3">
<title>
Getting 4 letter CLDR script code using a native-language name of the script
</title>
<programlisting language="php"><![CDATA[
function getScriptCode($scriptName, $locale)
{
$scripts2names = Zend_Locale_Data::getList($locale, 'script');
$names2scripts = array_flip($scripts2names);
return $names2scripts[$scriptName];
}
echo getScriptCode('Latin', 'en'); // outputs "Latn"
echo getScriptCode('Tamil', 'en'); // outputs "Taml"
echo getScriptCode('tamoul', 'fr'); // outputs "Taml"
]]></programlisting>
</example>
<para>
For a list of supported numeral systems call
<methodname>Zend_Locale::getTranslationList('numberingsystem', 'en')</methodname>.
</para>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
|