0 follower

Final Class Yiisoft\I18n\Locale

InheritanceYiisoft\I18n\Locale
ImplementsStringable

Locale stores locale information created from BCP 47 formatted string.

Method Details

Hide inherited methods

__construct() public method

Locale constructor.

public mixed __construct ( string $localeString )
$localeString string

BCP 47 formatted locale string.

throws InvalidArgumentException

                public function __construct(string $localeString)
{
    if (!preg_match(self::getBCP47Regex(), $localeString, $matches)) {
        throw new InvalidArgumentException($localeString . ' is not valid BCP 47 formatted locale string.');
    }
    if (!empty($matches['language'])) {
        $this->language = strtolower($matches['language']);
    }
    if (!empty($matches['region'])) {
        $this->region = strtoupper($matches['region']);
    }
    if (!empty($matches['variant'])) {
        $this->variant = $matches['variant'];
    }
    if (!empty($matches['extendedLanguage'])) {
        $this->extendedLanguage = $matches['extendedLanguage'];
    }
    if (!empty($matches['extension'])) {
        $this->extension = $matches['extension'];
    }
    if (!empty($matches['script'])) {
        $this->script = ucfirst(strtolower($matches['script']));
    }
    if (!empty($matches['grandfathered'])) {
        $this->grandfathered = $matches['grandfathered'];
    }
    if (!empty($matches['private'])) {
        $this->private = preg_replace('~^x-~', '', $matches['private']);
    }
    if (!empty($matches['keywords'])) {
        foreach (explode(';', $matches['keywords']) as $pair) {
            [$key, $value] = explode('=', $pair);
            if ($key === 'calendar') {
                $this->calendar = $value;
            }
            if ($key === 'colcasefirst') {
                $this->colcasefirst = $value;
            }
            if ($key === 'collation') {
                $this->collation = $value;
            }
            if ($key === 'colnumeric') {
                $this->colnumeric = $value;
            }
            if ($key === 'currency') {
                $this->currency = $value;
            }
            if ($key === 'numbers') {
                $this->numbers = $value;
            }
            if ($key === 'hours') {
                $this->hours = $value;
            }
        }
    }
}

            
__toString() public method

public string __toString ( )

                public function __toString(): string
{
    return $this->asString();
}

            
asString() public method

public string asString ( )
return string

Locale string.

                public function asString(): string
{
    if ($this->grandfathered !== null) {
        return $this->grandfathered;
    }
    $result = [];
    if ($this->language !== null) {
        $result[] = $this->language;
        if ($this->extendedLanguage !== null) {
            $result[] = $this->extendedLanguage;
        }
        if ($this->script !== null) {
            $result[] = $this->script;
        }
        if ($this->region !== null) {
            $result[] = $this->region;
        }
        if ($this->variant !== null) {
            $result[] = $this->variant;
        }
        if ($this->extension !== null) {
            $result[] = $this->extension;
        }
    }
    if ($this->private !== null) {
        $result[] = 'x-' . $this->private;
    }
    $keywords = [];
    if ($this->currency !== null) {
        $keywords[] = 'currency=' . $this->currency;
    }
    if ($this->colcasefirst !== null) {
        $keywords[] = 'colcasefirst=' . $this->colcasefirst;
    }
    if ($this->collation !== null) {
        $keywords[] = 'collation=' . $this->collation;
    }
    if ($this->colnumeric !== null) {
        $keywords[] = 'colnumeric=' . $this->colnumeric;
    }
    if ($this->calendar !== null) {
        $keywords[] = 'calendar=' . $this->calendar;
    }
    if ($this->numbers !== null) {
        $keywords[] = 'numbers=' . $this->numbers;
    }
    if ($this->hours !== null) {
        $keywords[] = 'hours=' . $this->hours;
    }
    $string = implode('-', $result);
    if ($keywords !== []) {
        $string .= '@' . implode(';', $keywords);
    }
    return $string;
}

            
calendar() public method

public string|null calendar ( )
return string|null

ICU calendar.

                public function calendar(): ?string
{
    return $this->calendar;
}

            
colcasefirst() public method

public string|null colcasefirst ( )
return string|null

ICU case-first collation.

                public function colcasefirst(): ?string
{
    return $this->colcasefirst;
}

            
collation() public method

public string|null collation ( )
return string|null

ICU collation.

                public function collation(): ?string
{
    return $this->collation;
}

            
colnumeric() public method

public string|null colnumeric ( )
return string|null

ICU numeric collation.

                public function colnumeric(): ?string
{
    return $this->colnumeric;
}

            
currency() public method

public string currency ( )
return string

ICU currency.

                public function currency(): ?string
{
    return $this->currency;
}

            
extendedLanguage() public method

public string|null extendedLanguage ( )
return string|null

Extended language subtags.

                public function extendedLanguage(): ?string
{
    return $this->extendedLanguage;
}

            
fallbackLocale() public method

Returns fallback locale.

public self fallbackLocale ( )
return self

Fallback locale.

                public function fallbackLocale(): self
{
    $fallback = $this
        ->withCalendar(null)
        ->withColcasefirst(null)
        ->withCollation(null)
        ->withColnumeric(null)
        ->withCurrency(null)
        ->withExtendedLanguage(null)
        ->withNumbers(null)
        ->withHours(null)
        ->withPrivate(null);
    if ($fallback->variant() !== null) {
        return $fallback->withVariant(null);
    }
    if ($fallback->region() !== null) {
        return $fallback->withRegion(null);
    }
    if ($fallback->script() !== null) {
        return $fallback->withScript(null);
    }
    return $fallback;
}

            
hours() public method

public string|null hours ( )
return string|null

Unicode hour cycle identifier.

                public function hours(): ?string
{
    return $this->hours;
}

            
language() public method

public string|null language ( )
return string|null

Two-letter ISO-639-2 language code.

                public function language(): ?string
{
    return $this->language;
}

            
numbers() public method

public string|null numbers ( )
return string|null

ICU numbers.

                public function numbers(): ?string
{
    return $this->numbers;
}

            
private() public method

public string|null private ( )

                public function private(): ?string
{
    return $this->private;
}

            
region() public method

public string region ( )
return string

Two-letter ISO 3166-1 country code.

                public function region(): ?string
{
    return $this->region;
}

            
script() public method

public string script ( )
return string

Four-letter ISO 15924 script code.

                public function script(): ?string
{
    return $this->script;
}

            
variant() public method

public string variant ( )
return string

Variant of language conventions to use.

                public function variant(): ?string
{
    return $this->variant;
}

            
withCalendar() public method

public self withCalendar ( string|null $calendar )
$calendar string|null

ICU calendar.

                public function withCalendar(?string $calendar): self
{
    $new = clone $this;
    $new->calendar = $calendar;
    return $new;
}

            
withColcasefirst() public method

public self withColcasefirst ( string|null $colcasefirst )
$colcasefirst string|null

ICU case-first collation.

                public function withColcasefirst(?string $colcasefirst): self
{
    $new = clone $this;
    $new->colcasefirst = $colcasefirst;
    return $new;
}

            
withCollation() public method

public self withCollation ( string|null $collation )
$collation string|null

ICU collation.

                public function withCollation(?string $collation): self
{
    $new = clone $this;
    $new->collation = $collation;
    return $new;
}

            
withColnumeric() public method

public self withColnumeric ( string|null $colnumeric )
$colnumeric string|null

ICU numeric collation.

                public function withColnumeric(?string $colnumeric): self
{
    $new = clone $this;
    $new->colnumeric = $colnumeric;
    return $new;
}

            
withCurrency() public method

public self withCurrency ( string|null $currency )
$currency string|null

ICU currency.

                public function withCurrency(?string $currency): self
{
    $new = clone $this;
    $new->currency = $currency;
    return $new;
}

            
withExtendedLanguage() public method

public self withExtendedLanguage ( string|null $extendedLanguage )
$extendedLanguage string|null

Extended language subtags.

                public function withExtendedLanguage(?string $extendedLanguage): self
{
    $new = clone $this;
    $new->extendedLanguage = $extendedLanguage;
    return $new;
}

            
withHours() public method

public self withHours ( string|null $hours )
$hours string|null

Unicode hour cycle identifier.

                public function withHours(?string $hours): self
{
    $new = clone $this;
    $new->hours = $hours;
    return $new;
}

            
withLanguage() public method

public self withLanguage ( string|null $language )
$language string|null

Two-letter ISO-639-2 language code.

                public function withLanguage(?string $language): self
{
    $new = clone $this;
    $new->language = $language;
    return $new;
}

            
withNumbers() public method

public self withNumbers ( string|null $numbers )
$numbers string|null

ICU numbers.

                public function withNumbers(?string $numbers): self
{
    $new = clone $this;
    $new->numbers = $numbers;
    return $new;
}

            
withPrivate() public method

public self withPrivate ( string|null $private )
$private string|null

                public function withPrivate(?string $private): self
{
    $new = clone $this;
    $new->private = $private;
    return $new;
}

            
withRegion() public method

public self withRegion ( string|null $region )
$region string|null

Two-letter ISO 3166-1 country code.

                public function withRegion(?string $region): self
{
    $new = clone $this;
    $new->region = $region;
    return $new;
}

            
withScript() public method

public self withScript ( string|null $script )
$script string|null

Four-letter ISO 15924 script code.

                public function withScript(?string $script): self
{
    $new = clone $this;
    $new->script = $script;
    return $new;
}

            
withVariant() public method

public self withVariant ( string|null $variant )
$variant string|null

Variant of language conventions to use.

                public function withVariant(?string $variant): self
{
    $new = clone $this;
    $new->variant = $variant;
    return $new;
}