dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Base Concepts of Internationalization in PHP : Page 3

Discover how to use internationalized numbers, currencies and dates/times with the I18N PEAR package.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Using the I18N_Number Class
This class localizes numbers. It formats a given number using either a default or defined format. The format function has the following prototype:

  • string format(mixed $number, [mixed $format = null]): Returns the formatted $number according to the format specified by the $format argument, which takes one of the values: I18N_NUMBER_FLOAT, I18N_NUMBER_INTEGER or a custom format created using the setFormat method described next. If the $format argument is missing then the default value is I18N_NUMBER_FLOAT.
The class inherits its next two functions from the I18N_Format class:

  • int setFormat(string $format): Define a custom format given by the $format argument.
  • void getFormat(): Returns a format identifier that can be passed to the format method for specifying the number's format type.
The following example sets a custom format and returns the formatted number and the associated format identifier:

<?php require_once 'I18N/Number.php'; //Create an instance of the I18N_Number class $number = new I18N_Number( 'en_US' ); //Print in the float format the pi value print( 'float --> '. $number->format( 999999,1 ) ).'<br />'; //Print in a custom format the pi value print( 'custom --> '.$number->format( 999999,$number->setFormat( array(5,'$','#'))) ).'<br />'; //Print the custom format id print ( 'custom format id --> '.$number->getFormat() ).'<br />'; //Print in the integer format the pi value print( 'integer --> '.$number->format( 999999,2 ) ).'<br />'; ?>

The result is:

float --> 999,999.000 custom --> 999#999$00000 custom format id --> 100 integer --> 999,999

Listing 1 shows a longer example that formats a given number for different languages. The listing sets up a simple input page where users can enter a number. It then formats the number appropriately for a range of different language codes. Figure 1 shows a screenshot of the simple interface.

 
Figure 1. Simple Numeric-Entry Interface: When a user enters a numeric value and clicks Send, the application displays the entered value in a variety of locale-specific formats.
For example, if a user entered 123456, the program would produce results like these:

Float format:


es_ES --> 123.456,000 nl_NL --> 123 456,000 de_DE --> 123.456,000 fr_FR --> 123 456,000 it_IT --> 123.456,000 en_US --> 123,456.000 The float format id is : 1

Integer format:

es_ES --> 123.456 nl_NL --> 123 456 de_DE --> 123.456 fr_FR --> 123 456 it_IT --> 123.456 en_US --> 123,456 The integer format id is : 2

Custom format:

es_ES --> 123*456&00 nl_NL --> 123*456&00 de_DE --> 123*456&00 fr_FR --> 123*456&00 it_IT --> 123*456&00 en_US --> 123*456&00 The custom format id is : 100

Using the I18N_Currency class
You use the I18N_Currency class to localize currency values and format specified amounts using a default or defined format. The format function has the next prototype:

  • void format(mixed $amount, [mixed $format = I18N_CURRENCY_LOCAL]): Returns the $amount formatted according to the specified format, which can be either I18N_CURRENCY_LOCAL, or I18N_CURRENCY_INTERNATIONAL, or can take a custom format created using the setFormat method described next. If the $format argument is missing, the default value is I18N_CURRENCY_LOCAL.
The I18N_Currency class inherits the setFormat and getFormat methods from the I18N_Format class (see the preceding section, "Using the I18N_Number Class").

Here's a short sample application that formats a given amount for different languages. This example uses a simple data-entry interface identical to the previous example (see Figure 1), and formats the user's entry appropriately for several different locales. You can download the complete code; the example below shows only the pertinent PHP code in the page:

<?php require_once 'I18N/Currency.php'; if(isset($_GET['currency'])) { $get_sum = $_GET['currency']; $languages = array('es_ES','nl_NL','de_DE','fr_FR','it_IT','en_US'); print '<font face style="Arial" color="000076">'; print '<h4><u>Currency format</u></h4>'; foreach( $languages as $lang ) { //Create an instance of the I18N_Currency class $currency = new I18N_Currency( $lang ); print '<font face style="Arial" color="000076" size="3"><b>'.$lang.'</b><br />'; //Print the $currency variable into the currency local format print 'Currency Local --> '.$currency->format($get_sum).'<br />'; //Print the $currency variable into the currency international format print 'Currency International --> '.$currency->format( $get_sum,2).'<br /><br />'; } } ?>

The output of this example is:

Currency format

es_ES Currency Local --> 123.456,00 € Currency International --> 123.456,00 Eur nl_NL Currency Local --> 123.456,00 € Currency International --> 123.456,00 Eur de_DE Currency Local --> 123.456,00 € Currency International --> 123.456,00 Eur fr_FR Currency Local --> 123.456,00 € Currency International --> 123.456,00 Eur it_IT Currency Local --> 123.456,00 € Currency International --> 123.456,00 Eur en_US Currency Local --> $123,456.00 Currency International --> $US 123,456.00



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date