Final Class Yiisoft\I18n\Locale
| Inheritance | Yiisoft\I18n\Locale |
|---|---|
| Implements | Stringable |
Locale stores locale information created from BCP 47 formatted string.
Public Methods
Method Details
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;
}
}
}
}
| 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;
}
| public string|null calendar ( ) | ||
| return | string|null |
ICU calendar. |
|---|---|---|
public function calendar(): ?string
{
return $this->calendar;
}
| public string|null colcasefirst ( ) | ||
| return | string|null |
ICU case-first collation. |
|---|---|---|
public function colcasefirst(): ?string
{
return $this->colcasefirst;
}
| public string|null collation ( ) | ||
| return | string|null |
ICU collation. |
|---|---|---|
public function collation(): ?string
{
return $this->collation;
}
| public string|null colnumeric ( ) | ||
| return | string|null |
ICU numeric collation. |
|---|---|---|
public function colnumeric(): ?string
{
return $this->colnumeric;
}
| public string currency ( ) | ||
| return | string |
ICU currency. |
|---|---|---|
public function currency(): ?string
{
return $this->currency;
}
| public string|null extendedLanguage ( ) | ||
| return | string|null |
Extended language subtags. |
|---|---|---|
public function extendedLanguage(): ?string
{
return $this->extendedLanguage;
}
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;
}
| public string|null hours ( ) | ||
| return | string|null |
Unicode hour cycle identifier. |
|---|---|---|
public function hours(): ?string
{
return $this->hours;
}
| public string|null language ( ) | ||
| return | string|null |
Two-letter ISO-639-2 language code. |
|---|---|---|
public function language(): ?string
{
return $this->language;
}
| public string|null numbers ( ) | ||
| return | string|null |
ICU numbers. |
|---|---|---|
public function numbers(): ?string
{
return $this->numbers;
}
| public string region ( ) | ||
| return | string |
Two-letter ISO 3166-1 country code. |
|---|---|---|
public function region(): ?string
{
return $this->region;
}
| public string script ( ) | ||
| return | string |
Four-letter ISO 15924 script code. |
|---|---|---|
public function script(): ?string
{
return $this->script;
}
| public string variant ( ) | ||
| return | string |
Variant of language conventions to use. |
|---|---|---|
public function variant(): ?string
{
return $this->variant;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| public self withPrivate ( string|null $private ) | ||
| $private | string|null | |
public function withPrivate(?string $private): self
{
$new = clone $this;
$new->private = $private;
return $new;
}
| 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;
}
| 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;
}
| 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;
}
Signup or Login in order to comment.