Oihana PHP Standards

Locale

Represents and manipulates IETF BCP 47 / RFC 5646 locale tags.

Combines ISO 639-1 (language), ISO 15924 (script) and ISO 3166-1 (region) codes in a single value-object. Supports the minimal grammar: language[-script][-region][-variant…][-x-private…].

Two validation levels are available:

  • Tolerant (default): only the BCP 47 syntax is checked.
  • Strict : components are cross-validated against the ISO classes (ISO639_1, ISO15924, ISO3166_1).

Subtags are normalized to canonical case on input: language lowercase, script Titlecase, region uppercase, variants lowercase.

Example usage:

use org\ietf\Locale;

$l = new Locale('fr-FR');
echo $l->language; // "fr"
echo $l->region;   // "FR"

$l = new Locale('zh-Hant-TW');
echo $l->script;   // "Hant"

$l = new Locale('en-x-pig-latin');
echo $l->privateUse; // "x-pig-latin"

// Strict mode rejects unknown ISO codes
new Locale('zz-ZZ', strict: true); // throws InvalidArgumentException

// Tolerant mode still allows checking ISO compliance on demand
$tolerant = new Locale('zz-ZZ');
$tolerant->isStrict(); // false
Tags
link

BCP 47 / RFC 5646

author

Marc Alcaraz (ekameleon)

since
1.0.2

Table of Contents

Constants

SEPARATOR  : string = '-'
Subtag separator.
ZERO  : string = 'und'
Default zero value: ISO 639-2 `und` (undetermined language).

Properties

$language  : string
Language subtag (e.g. `fr`). Lowercase.
$privateUse  : string|null
Optional private-use subtag (e.g. `x-pig-latin`), or null.
$region  : string|null
Optional region subtag (2 letters uppercase, or 3 digits, or null).
$script  : string|null
Optional script subtag (e.g. `Hant`). Titlecase, or null.
$tag  : string
The canonical BCP 47 tag (e.g. `fr-FR`, `zh-Hant-TW`).
$variants  : array<int, string>
List of variant subtags (e.g. `["1996"]`). Lowercase. Empty array if none.
$_language  : string
$_privateUse  : string|null
$_region  : string|null
$_script  : string|null
$_strict  : bool
$_tag  : string
$_variants  : array<int, string>

Methods

__construct()  : mixed
Creates a new Locale instance.
__toString()  : string
String cast returns the canonical tag.
isStrict()  : bool
Returns true when the components cross-validate against the ISO classes (regardless of the strict mode used at construction).
serialize()  : string
Re-assembles the canonical tag from the parsed components.

Constants

SEPARATOR

Subtag separator.

public string SEPARATOR = '-'

ZERO

Default zero value: ISO 639-2 `und` (undetermined language).

public string ZERO = 'und'

Properties

$language read-only virtual

Language subtag (e.g. `fr`). Lowercase.

public string $language
Hooks
public string get

$privateUse read-only virtual

Optional private-use subtag (e.g. `x-pig-latin`), or null.

public string|null $privateUse
Hooks
public string|null get

$region read-only virtual

Optional region subtag (2 letters uppercase, or 3 digits, or null).

public string|null $region
Hooks
public string|null get

$script read-only virtual

Optional script subtag (e.g. `Hant`). Titlecase, or null.

public string|null $script
Hooks
public string|null get

$tag virtual

The canonical BCP 47 tag (e.g. `fr-FR`, `zh-Hant-TW`).

public string $tag
Tags
throws
InvalidArgumentException

If the value is not a valid tag (or fails strict validation if enabled)

Hooks
public string get public set

$variants read-only virtual

List of variant subtags (e.g. `["1996"]`). Lowercase. Empty array if none.

public array<int, string> $variants
Hooks
public array<int, string> get

$_language

private string $_language

$_privateUse

private string|null $_privateUse = null

$_region

private string|null $_region = null

$_script

private string|null $_script = null

$_variants

private array<int, string> $_variants = []

Methods

__construct()

Creates a new Locale instance.

public __construct([string|null $tag = null ][, bool $strict = false ]) : mixed
Parameters
$tag : string|null = null

BCP 47 locale tag, or null for the ZERO default

$strict : bool = false

If true, cross-validate against ISO classes (default: false)

Tags
throws
InvalidArgumentException

If the tag is invalid (or fails strict validation)

__toString()

String cast returns the canonical tag.

public __toString() : string
Return values
string

isStrict()

Returns true when the components cross-validate against the ISO classes (regardless of the strict mode used at construction).

public isStrict() : bool
Return values
bool

serialize()

Re-assembles the canonical tag from the parsed components.

private serialize() : string
Return values
string
On this page

Search results