Login | Register   
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


advertisement
 

Base Concepts of Internationalization in PHP : Page 2

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


advertisement
Using the I18N_Country Class
You use the I18N_Country class to determine the country name from a country code. The class has these two methods:

  • void getName([: $code = '']): Returns the name of the country using the country code passed by the string argument $code.
  • void isValidCode(mixed $code): Check whether the country code passed by the $code argument is valid.
Here's a simple example that shows how to use the methods of the I18N_Country class:

<?php require_once 'I18N/Country.php'; $countries_codes = array('AF','AQ','BD', 'MB','DE','FR','HR','JP','IS','IT','MU', 'RO','US','JA'); print '<font face style="Arial" color="000076">'; print '<h4><u>Countries codes</u></h4>'; foreach( $countries_codes as $code ) { //Create an instance of the I18N_Country class $country = new I18N_Country (); print '<font face style="Arial" color="000076" size="3"><b>'.$code.'</b> -->'; //Check if $code is a valid code if($country->isValidCode($code)) { //Return name of the country for country code passed print ( $country->getName($code) ).'<br />'; } else { print '<font face style="Arial" color="red" size="3">'; print ( '<b> This is not a valid code!!! </b><br />' ); } } ?>

The output of this example is:

Countries codes AF -->AFGHANISTAN AQ -->ANTARCTICA BD -->BANGLADESH MB --> <font color=red>This is not a valid code!!!</font> DE -->GERMANY FR -->FRANCE HR -->CROATIA (local name: Hrvatska) JP -->JAPAN IS -->ICELAND IT -->ITALY MU -->MAURITIUS RO -->ROMANIA US -->UNITED STATES JA --> <font color=red>This is not a valid code!!! </font>

Using the I18N_Language Class
You use the I18N_Language class to determine the language name from a specified language code using the getName method:

  • void getName([: $code = ''], : 1): Returns the name of the language using the language code passed by the string argument $code. The 1 parameter represents the string language code.
Here's a simple application that uses the I18N_Language class:



<?php require_once 'I18N/Language.php'; $language_codes = array('af','da','es','de','fr','ja', 'it','mn','ro','en'); print '<font face style="Arial" color="000076">'; print '<h4><u>Language codes</u></h4>'; foreach( $language_codes as $code ) { //Create an instance of the I18N_Language class $language = new I18N_Language (); print '<font face style="Arial" color="000076" size="3"><b>'. $code.'</b> -->'; //Return name of the language for language code passed print ($language->getName($code)).'<br />'; } ?>

The output of this example is:

Language codes af -->Afrikaans da -->Danish es -->Spanish de -->German fr -->French ja -->Japanese it -->Italian mn -->Mongolian ro -->Romanian en -->English

The combination of the user's country and language give you the locale.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap