Cosmo (PHP) — API reference

Cosmo extends Locale
in package

Table of Contents

Constants

FULL  : int = \IntlDateFormatter::FULL
LONG  : int = \IntlDateFormatter::LONG
MEDIUM  : int = \IntlDateFormatter::MEDIUM
NONE  : int = \IntlDateFormatter::NONE
SHORT  : int = \IntlDateFormatter::SHORT
TIME_TYPES  : array<string|int, mixed> = ['none' => self::NONE, 'short' => self::SHORT,...
UNIT_TYPES  : array<string|int, mixed> = ['short' => 'unitsNarrow', 'medium' => 'unitsSh...

Properties

$locale  : string|null
$modifiers  : array<string|int, mixed>
$subtags  : array<string|int, mixed>

Methods

__construct()  : mixed
calendar()  : string
Translate the calendar identifier (e.g. "buddhist" -> "Buddhist Calendar")
country()  : string
Translate the country of a locale (e.g. AU -> Australia)
create()  : Cosmo
createFromHttp()  : Cosmo
Creates a Cosmo instance from an HTTP Accept-Language header.
createFromSubtags()  : Cosmo
Creates a Cosmo instance from an array of locale subtags instead of a locale string.
currency()  : string
Returns the localised name or symbol of a currency.
date()  : string
Formats a date value (no time component).
direction()  : string
Returns the text direction of a language: 'rtl' or 'ltr'.
duration()  : string
flag()  : string
Returns the emoji of a locale (e.g. AU -> 🇦🇺)
formatMoment()  : string
Formats a date/time value using a custom ICU pattern.
get()  : ResourceBundle|int|array<string|int, mixed>|string|null
Retrieves a value from an ICU resource bundle, falling back to the primary language then root.
language()  : string
Returns the localised name of a language (e.g. 'en' -> 'English', 'glk' -> 'Gilaki').
message()  : string
Formats an ICU message string with the given arguments.
moment()  : string
Formats a date, time, or date+time value using the locale's conventions.
money()  : string
Formats a monetary value using the locale's currency format.
number()  : string
Formats a number using the locale's default number format.
ordinal()  : string
Formats a number as a localised ordinal (e.g. 1 -> '1st' in English).
percentage()  : string
Formats a decimal value as a localised percentage (e.g. 0.2 -> '20%').
quote()  : string
Wraps a string in the locale's quotation marks (e.g. "text" in English, «text» in Persian).
script()  : string
Returns the localised name of a script (e.g. 'Hans' -> 'Simplified Chinese').
spellout()  : string
Spells out a number in the locale's language (e.g. 42 -> 'forty-two' in English).
symbol()  : string
Returns a localised number symbol (e.g. 'decimal_separator', 'percent').
time()  : string
Formats a time value (no date component).
unit()  : string
Formats a measurement value with a localised unit (e.g. 2.19 gigabytes, 26 degrees Celsius).
bundleToPluralMessage()  : string
extract()  : ResourceBundle|int|array<string|int, mixed>|string|null
getTimeType()  : int

Constants

FULL

public int FULL = \IntlDateFormatter::FULL

LONG

public int LONG = \IntlDateFormatter::LONG

MEDIUM

public int MEDIUM = \IntlDateFormatter::MEDIUM

NONE

public int NONE = \IntlDateFormatter::NONE

SHORT

public int SHORT = \IntlDateFormatter::SHORT

TIME_TYPES

public array<string|int, mixed> TIME_TYPES = ['none' => self::NONE, 'short' => self::SHORT, 'medium' => self::MEDIUM, 'long' => self::LONG, 'full' => self::FULL, 'n' => self::NONE, 's' => self::SHORT, 'm' => self::MEDIUM, 'l' => self::LONG, 'f' => self::FULL]

UNIT_TYPES

public array<string|int, mixed> UNIT_TYPES = ['short' => 'unitsNarrow', 'medium' => 'unitsShort', 'long' => 'units', 'full' => 'units', 's' => 'unitsNarrow', 'm' => 'unitsShort', 'l' => 'units', 'f' => 'units']

Properties

$locale read-only

public string|null $locale

$modifiers read-only

public array<string|int, mixed> $modifiers

$subtags read-only

public array<string|int, mixed> $subtags

Methods

__construct()

public __construct([string|null $locale = null ][, array<string|int, mixed> $modifiers = [] ]) : mixed
Parameters
$locale : string|null = null

BCP 47 locale identifier, e.g. en_AU. Defaults to the system locale.

$modifiers : array<string|int, mixed> = []

Optional overrides: 'calendar', 'currency', 'timezone'.

calendar()

Translate the calendar identifier (e.g. "buddhist" -> "Buddhist Calendar")

public calendar(string $calendar) : string
Parameters
$calendar : string
Return values
string

country()

Translate the country of a locale (e.g. AU -> Australia)

public country([string|Sentinel|null $country = Sentinel::Unset ]) : string
Parameters
$country : string|Sentinel|null = Sentinel::Unset

ISO 3166 country codes or a valid locale

Return values
string

create()

public static create([string $locale = null ][, array<string|int, mixed> $modifiers = [] ]) : Cosmo

Use new Cosmo() directly — PHP 8.4 supports new Cosmo($locale)->method() without parentheses.

Parameters
$locale : string = null
$modifiers : array<string|int, mixed> = []
Return values
Cosmo

createFromHttp()

Creates a Cosmo instance from an HTTP Accept-Language header.

public static createFromHttp([string|null $header = null ][, array<string|int, mixed> $modifiers = [] ]) : Cosmo
Parameters
$header : string|null = null

Accept-Language header value. Defaults to $_SERVER['HTTP_ACCEPT_LANGUAGE'].

$modifiers : array<string|int, mixed> = []

Optional overrides: 'calendar', 'currency', 'timezone'.

Return values
Cosmo

createFromSubtags()

Creates a Cosmo instance from an array of locale subtags instead of a locale string.

public static createFromSubtags(array<string|int, mixed> $subtags[, array<string|int, mixed> $modifiers = [] ]) : Cosmo
Parameters
$subtags : array<string|int, mixed>

Locale subtag array, e.g. ['language' => 'en', 'region' => 'AU'].

$modifiers : array<string|int, mixed> = []

Optional overrides: 'calendar', 'currency', 'timezone'.

Tags
see
Locale::composeLocale()

for the expected array format.

Return values
Cosmo

currency()

Returns the localised name or symbol of a currency.

public currency([string|null|Sentinel $currencyCode = Sentinel::Unset ][, bool $getSymbol = false ][, bool $strict = false ]) : string
Parameters
$currencyCode : string|null|Sentinel = Sentinel::Unset

ISO 4217 currency code, e.g. 'AUD'. Defaults to the locale's currency.

$getSymbol : bool = false

Return the currency symbol (e.g. '$') instead of the full name.

$strict : bool = false

Throw if the currency code is invalid instead of returning it as-is.

Tags
throws
Exception

If $strict is true and the currency code is not recognised.

Return values
string

date()

Formats a date value (no time component).

public date(mixed $value[, string $type = 'short' ]) : string
Parameters
$value : mixed

A DateTimeInterface, IntlCalendar, Unix timestamp, or localtime() array.

$type : string = 'short'

Format type: 'none', 'short', 'medium', 'long', or 'full'.

Return values
string

direction()

Returns the text direction of a language: 'rtl' or 'ltr'.

public direction([string|null|Sentinel $language = Sentinel::Unset ]) : string
Parameters
$language : string|null|Sentinel = Sentinel::Unset

BCP 47 language code. Defaults to the instance locale.

Return values
string

'rtl' or 'ltr'.

duration()

public duration(float $duration[, bool $withWords = false ]) : string
Parameters
$duration : float
$withWords : bool = false

this currently works for English, for other languages it has no effect on the output

Return values
string

formatMoment()

Formats a date/time value using a custom ICU pattern.

public formatMoment(mixed $value, string $pattern[, string|null $calendar = null ]) : string
Parameters
$value : mixed

A DateTimeInterface, IntlCalendar, Unix timestamp (int/float), or localtime() array.

$pattern : string

ICU date/time pattern, e.g. 'YYYY-MM-dd'.

$calendar : string|null = null

Pass 'gregorian' to force the Gregorian calendar. Defaults to the locale's native calendar.

Tags
throws
Exception

If the value cannot be formatted.

Return values
string

get()

Retrieves a value from an ICU resource bundle, falling back to the primary language then root.

public get(string $bundleName, string ...$path) : ResourceBundle|int|array<string|int, mixed>|string|null
Parameters
$bundleName : string

ICU bundle name, e.g. Bundle::LOCALE.

$path : string

One or more keys to traverse into the bundle.

Return values
ResourceBundle|int|array<string|int, mixed>|string|null

language()

Returns the localised name of a language (e.g. 'en' -> 'English', 'glk' -> 'Gilaki').

public language([string|null|Sentinel $language = Sentinel::Unset ]) : string

If you have a full locale identifier (e.g. en_AU), pass it through Locale::getPrimaryLanguage() first.

Parameters
$language : string|null|Sentinel = Sentinel::Unset

BCP 47 language code. Defaults to the instance locale.

Return values
string

Empty string if the language is null or empty.

message()

Formats an ICU message string with the given arguments.

public message(string $message, array<string|int, mixed> $args) : string
Parameters
$message : string

ICU message pattern, e.g. '{0, plural, one {# item} other {# items}'.

$args : array<string|int, mixed>

Arguments to substitute into the pattern.

Return values
string

moment()

Formats a date, time, or date+time value using the locale's conventions.

public moment(mixed $value[, string $dateType = 'short' ][, string $timeType = 'short' ][, string|null $calendar = null ][, string|null $pattern = null ]) : string
Parameters
$value : mixed

A DateTimeInterface, IntlCalendar, Unix timestamp (int/float), or localtime() array.

$dateType : string = 'short'

Date format: 'none', 'short', 'medium', 'long', or 'full'.

$timeType : string = 'short'

Time format: 'none', 'short', 'medium', 'long', or 'full'.

$calendar : string|null = null

Pass 'gregorian' to force the Gregorian calendar regardless of locale. Defaults to the locale's native calendar (e.g. Persian for fa_IR).

$pattern : string|null = null

Optional ICU date/time pattern, overrides $dateType/$timeType when set.

Tags
throws
Exception

If the value cannot be formatted.

Return values
string

money()

Formats a monetary value using the locale's currency format.

public money(float $value[, string|null $currency = null ][, string $pattern = '' ][, int|null $precision = null ][, bool $strict = false ]) : string
Parameters
$value : float

The amount to format.

$currency : string|null = null

ISO 4217 currency code, e.g. 'AUD'. Defaults to the locale's currency.

$pattern : string = ''

Optional NumberFormatter pattern to override the default format.

$precision : int|null = null

Number of decimal digits. Defaults to the currency's standard precision.

$strict : bool = false

Throw if no currency is available instead of returning an empty string.

Tags
throws
Exception

If $strict is true and no currency code is set.

Return values
string

number()

Formats a number using the locale's default number format.

public number(float $number) : string
Parameters
$number : float
Return values
string

ordinal()

Formats a number as a localised ordinal (e.g. 1 -> '1st' in English).

public ordinal(int $number) : string
Parameters
$number : int
Return values
string

percentage()

Formats a decimal value as a localised percentage (e.g. 0.2 -> '20%').

public percentage(float $value[, int $precision = 3 ]) : string
Parameters
$value : float

Decimal value, e.g. 0.2 for 20%.

$precision : int = 3

Maximum number of decimal digits.

Return values
string

quote()

Wraps a string in the locale's quotation marks (e.g. "text" in English, «text» in Persian).

public quote(string $quote) : string
Parameters
$quote : string

The text to quote.

Return values
string

script()

Returns the localised name of a script (e.g. 'Hans' -> 'Simplified Chinese').

public script([string|null|Sentinel $script = Sentinel::Unset ]) : string

If omitted, uses the script subtag from the instance locale if present.

Parameters
$script : string|null|Sentinel = Sentinel::Unset

ISO 15924 script code. Defaults to the locale's script subtag.

Return values
string

spellout()

Spells out a number in the locale's language (e.g. 42 -> 'forty-two' in English).

public spellout(float $number) : string
Parameters
$number : float
Return values
string

symbol()

Returns a localised number symbol (e.g. 'decimal_separator', 'percent').

public symbol(int|string $symbol) : string

Accepts a NumberFormatter constant (e.g. NumberFormatter::DECIMAL_SEPARATOR_SYMBOL) or a case-insensitive string name without the _SYMBOL suffix.

Parameters
$symbol : int|string

NumberFormatter symbol constant or string name.

Tags
throws
Exception

If the string name does not match a known symbol.

Return values
string

time()

Formats a time value (no date component).

public time(mixed $value[, string $type = 'short' ]) : string
Parameters
$value : mixed

A DateTimeInterface, IntlCalendar, Unix timestamp, or localtime() array.

$type : string = 'short'

Format type: 'none', 'short', 'medium', 'long', or 'full'.

Return values
string

unit()

Formats a measurement value with a localised unit (e.g. 2.19 gigabytes, 26 degrees Celsius).

public unit(string $unit, string $scale, float|int $value[, string $type = 'full' ]) : string
Parameters
$unit : string

Unit category, e.g. 'digital', 'temperature', 'mass'.

$scale : string

Unit scale within the category, e.g. 'gigabyte', 'celsius', 'gram'.

$value : float|int

The numeric value to format.

$type : string = 'full'

Format width: 'short', 'medium', 'long', or 'full'.

Tags
throws
Exception

If $type is not a valid format width.

see
https://intl.rmcreative.ru/site/unit-data?locale=en

for available units and scales.

Return values
string

bundleToPluralMessage()

private bundleToPluralMessage(ResourceBundle $bundle) : string
Parameters
$bundle : ResourceBundle
Return values
string

extract()

private extract(mixed $locale, mixed $bundleName, array<string|int, mixed> $path) : ResourceBundle|int|array<string|int, mixed>|string|null
Parameters
$locale : mixed
$bundleName : mixed
$path : array<string|int, mixed>
Return values
ResourceBundle|int|array<string|int, mixed>|string|null

getTimeType()

private getTimeType(string $type) : int
Parameters
$type : string
Return values
int
On this page

Search results