Examples

Examples – Usage example for I18Nv2

API Documentation

Please see PEARs API Documentation for details on the API provided by I18Nv2.

Setting a locale

Because Un*x and Windows use different locale codes, PHPs setLocale() is not easily portable - I18Nv2::setLocale() attempts to provide this portability.

With I18Nv2 you can use standard locale codes like 'en_US' on both, Linux and Windows, though the list is far not complete yet, so if you stumble over a not covered locale (I18Nv2::$locales in I18Nv2::_main()), just drop a mail to the maintainer with the missing locale and its corresponding Win32 code.

I18Nv2::setLocale()

<?php
require_once 'I18Nv2.php';

foreach (array(
'en_US''de''fr_CN') as $locale) {
    if (!
$syslocale I18Nv2::setLocale($locale)) {
        echo 
"Locale '$locale' not available!\n";
    } else {
        echo 
"Systems locale for '$locale': '$syslocale'\n";
    }
}
?>

Retrieving locale conventions

I18Nv2 holds locale conventions returned by localeConv() stored statically, so they are easily accessible through I18Nv2::getInfo(). Have a look at the documentation of PHPs localeConv() for all available information.

I18Nv2::getInfo()

<?php
require_once 'I18Nv2.php';

I18Nv2::setLocale('fr');

$dec_point I18Nv2::getInfo('decimal_point');
echo 
"The decimal point for the french locale is '$dec_point'.\n";
echo 
"I18Nv2::getInfo() called without parameter returns all available information:\n";
print_r(I18Nv2::getInfo());
?>

Automatically transform output character set

I18Nv2 provides an easy way to utilize the ob_iconv_handler() through I18Nv2::autoConv().

I18Nv2::autoConv()

<?php
require_once 'I18Nv2.php';

// Writing a shell app that should also display nicely in a DOS box
if (I18Nv2_WIN) {
    
I18Nv2::autoConv('CP850');
}

// output some latin1 stuff
echo "äüöß\n";
?>

Using I18Nv2_Locale

I18Nv2_Locale is a formatter object that provides functionality to format dates, times, numbers and currencies in locale dependent conventions.

I18Nv2_Locale

<?php
require_once 'I18Nv2.php';

$locale = &I18Nv2::createLocale('de_AT');

echo 
"Format a currency value of 2000: ",
    
$locale->formatCurrency(2000I18Nv2_CURRENCY_INTERNATIONAL), "\n";

echo 
"Format todays date:              ",
    
$locale->formatDate(nullI18Nv2_DATETIME_FULL), "\n";

echo 
"Format current time:             ",
    
$locale->formatTime(nullI18Nv2_DATETIME_SHORT), "\n";
?>

Using I18Nv2_Negotiator

I18Nv2 provides a language, charset and locale negotiator for HTTP.

I18Nv2_Negotiator

<?php
require_once 'I18Nv2/Negotiator.php';

$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en-GB,en;q=0.5,de';
$_SERVER['HTTP_ACCEPT_CHARSET']  = 'utf-8,iso-8859-1;q=0.5';

$neg = &new I18Nv2_Negotiator;

echo 
"User agents preferred language:                  ",
    
$lang $neg->getLanguageMatch(), "\n";

echo 
"User agents preferred country for language '$lang': ",
    
$neg->getCountryMatch($lang), "\n";

echo 
"User agents preferred locale:                    ",
    
$neg->getLocaleMatch(), "\n";

echo 
"User agents preferred charset:                   ",
    
$neg->getCharsetMatch(), "\n";
?>

Using I18Nv2_Language

I18Nv2 provides translated lists of ISO language names.

I18Nv2_Language

<?php
require_once 'I18Nv2/Language.php';

$lang = &new I18Nv2_Language('it''iso-8859-1');

echo 
"Italian name for English: ",
    
$lang->getName('en'), "\n";

echo 
"Italian name for French:  ",
    
$lang->getName('fr'), "\n";
?>

Using I18Nv2_Country

I18Nv2 provides translated lists of ISO country names.

I18Nv2_Country

<?php
require_once 'I18Nv2/Country.php';

$country = &new I18Nv2_Country('de''iso-8859-1');

echo 
"German name for United States: ",
    
$country->getName('us'), "\n";

echo 
"German name for Italia:        ",
    
$country->getName('it'), "\n";
?>

Using I18Nv2_Country

I18Nv2 provides decorated classes for country and language lists.

I18Nv2_DecoratedList

<?php
require_once 'I18Nv2/Country.php';
require_once 
'I18Nv2/DecoratedList/HtmlSelect.php';
require_once 
'I18Nv2/DecoratedList/HtmlEntities.php';

$c = &new I18Nv2_Country('it''iso-8859-1');
$e = &new I18Nv2_DecoratedList_HtmlEntities($c);
$s = &new I18Nv2_DecoratedList_HtmlSelect($e);

// set some attributes
$s->attributes['select']['name'] = 'CountrySelect';
$s->attributes['select']['onchange'] = 'this.form.submit()';

// set a selected entry
$s->selected['DE'] = true;

// print a HTML safe select box
echo $s->getAllCodes();
?>

I18Nv2_CommonList::toDecoratedList()

<?php
require_once 'I18Nv2/Country.php';

$c = &new I18Nv2_Country('it''iso-8859-1');
$s = &$c->toDecoratedList('HtmlSelect');

// set some attributes
$s->attributes['select']['name'] = 'CountrySelect';
$s->attributes['select']['onchange'] = 'this.form.submit()';

// set a selected entry
$s->selected['IT'] = true;

// print a HTML select box
echo $s->getAllCodes();
?>
    Поддержать сайт на родительском проекте КГБ