0 follower

CLocalizedFormatter

Package system.utils
Inheritance class CLocalizedFormatter » CFormatter » CApplicationComponent » CComponent
Implements IApplicationComponent
Since 1.1.14
Source Code framework/utils/CLocalizedFormatter.php
CLocalizedFormatter provides a set of commonly used data formatting methods based on the current locale settings.

It provides the same functionality as CFormatter, but overrides all the settings for booleanFormat, datetimeFormat and numberFormat with the values for the current locale. Because of this you are not able to configure these properties for CLocalizedFormatter directly. Date and time format can be adjusted by setting dateFormat and timeFormat.

It uses CApplication::locale by default but you can set a custom locale by using setLocale-method.

For a list of recognizable format types, and details on how to call the formatter methods, see CFormatter documentation.

To replace the application component 'format', which is registered by CApplication by default, you can put this in your application 'components' config: 'format' => array( 'class' => 'CLocalizedFormatter', ),

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
behaviors array the behaviors that should be attached to this component. CApplicationComponent
booleanFormat array the text to be displayed when formatting a boolean value. CFormatter
dateFormat string the width of the date pattern. CLocalizedFormatter
datetimeFormat string the format string to be used to format a date and time using PHP date() function. CFormatter
htmlPurifier CHtmlPurifier the HTML purifier instance CFormatter
htmlPurifierOptions array the options to be passed to CHtmlPurifier instance used in this class. CFormatter
isInitialized boolean Checks if this application component has been initialized. CApplicationComponent
locale CLocale $locale the locale currently used for formatting values CLocalizedFormatter
numberFormat array the format used to format a number with PHP number_format() function. CFormatter
sizeFormat array the format used to format size (bytes). CFormatter
timeFormat string the width of the time pattern. CLocalizedFormatter

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the format method when its shortcut is invoked. CFormatter
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
format() Formats a value based on the given type. CFormatter
formatBoolean() Formats the value as a boolean. CLocalizedFormatter
formatDate() Formats the value as a date using the locales date formatter. CLocalizedFormatter
formatDatetime() Formats the value as a date and time using the locales date formatter. CLocalizedFormatter
formatEmail() Formats the value as a mailto link. CFormatter
formatHtml() Formats the value as HTML text without any encoding. CFormatter
formatImage() Formats the value as an image tag. CFormatter
formatNtext() Formats the value as a HTML-encoded plain text and converts newlines with HTML <br /> or CFormatter
formatNumber() Formats the value as a number using the locales number formatter. CLocalizedFormatter
formatRaw() Formats the value as is without any formatting. CFormatter
formatSize() Formats the value in bytes as a size in human readable form. CFormatter
formatText() Formats the value as a HTML-encoded plain text. CFormatter
formatTime() Formats the value as a time using the locales date formatter. CLocalizedFormatter
formatUrl() Formats the value as a hyperlink. CFormatter
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getHtmlPurifier() Returns the HTML purifier instance CFormatter
getIsInitialized() Checks if this application component has been initialized. CApplicationComponent
getLocale() Returns $locale the locale currently used for formatting values CLocalizedFormatter
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. CApplicationComponent
raiseEvent() Raises an event. CComponent
setLocale() Set the locale to use for formatting values. CLocalizedFormatter

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
normalizeDateValue() Normalizes an expression as a timestamp. CFormatter

Property Details

dateFormat property
public string $dateFormat;

the width of the date pattern. It can be 'full', 'long', 'medium' and 'short'. Defaults to 'medium'.

locale property
public CLocale getLocale()
public void setLocale(CLocale|string $locale)

$locale the locale currently used for formatting values

timeFormat property
public string $timeFormat;

the width of the time pattern. It can be 'full', 'long', 'medium' and 'short'. Defaults to 'medium'.

Method Details

formatBoolean() method
public string formatBoolean(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
Source Code: framework/utils/CLocalizedFormatter.php#79 (show)
public function formatBoolean($value)
{
    return 
$value Yii::t('yii','Yes') : Yii::t('yii','No');
}

Formats the value as a boolean.

See Also

formatDate() method
public string formatDate(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
Source Code: framework/utils/CLocalizedFormatter.php#91 (show)
public function formatDate($value)
{
    return 
$this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), $this->dateFormatnull);
}

Formats the value as a date using the locales date formatter.

formatDatetime() method
public string formatDatetime(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
Source Code: framework/utils/CLocalizedFormatter.php#116 (show)
public function formatDatetime($value)
{
    return 
$this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), $this->dateFormat$this->timeFormat);
}

Formats the value as a date and time using the locales date formatter.

formatNumber() method
public string formatNumber(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
Source Code: framework/utils/CLocalizedFormatter.php#127 (show)
public function formatNumber($value)
{
    return 
$this->getLocale()->numberFormatter->formatDecimal($value);
}

Formats the value as a number using the locales number formatter.

formatTime() method
public string formatTime(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
Source Code: framework/utils/CLocalizedFormatter.php#103 (show)
public function formatTime($value)
{
    return 
$this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), null$this->timeFormat);
}

Formats the value as a time using the locales date formatter.

getLocale() method
public CLocale getLocale()
{return} CLocale $locale the locale currently used for formatting values
Source Code: framework/utils/CLocalizedFormatter.php#65 (show)
public function getLocale()
{
    if(
$this->_locale === null) {
        
$this->setLocale(Yii::app()->locale);
    }
    return 
$this->_locale;
}

setLocale() method
public void setLocale(CLocale|string $locale)
$locale CLocale|string an instance of CLocale or a locale ID
Source Code: framework/utils/CLocalizedFormatter.php#54 (show)
public function setLocale($locale)
{
    if(
is_string($locale))
        
$locale=CLocale::getInstance($locale);
    
$this->sizeFormat['decimalSeparator']=$locale->getNumberSymbol('decimal');
    
$this->_locale=$locale;
}

Set the locale to use for formatting values.