Oihana PHP

DefaultLangTrait

Carries the **default language** — the locale a multilingual expression falls back to when the requested one is absent, or when no language is requested at all.

⚠ Not to be confused with the language requested by the current call (?lang=, see PrepareLang). The two travel side by side and mean opposite things: the requested language is an instruction, and it wins over everything; defaultLang is a default, and a default never overrides an explicit declaration. That is why they cannot share a name.

It is also distinct from LanguagesTrait, its sibling: languages is the set a request is allowed to ask for, defaultLang is the one answered when nothing was asked.

The trait is used at two levels, which is what makes it a trait rather than a property: a model declares the fallback its own multilingual fields use, and a controller reads the site-wide one from the configuration and pushes it into the model call. When both answer, the model wins — a site default must not silently override what a model states about itself, or the model would change behaviour depending on which host loads it.

Tags
example
// In a model or a controller constructor:
$this->initializeDefaultLang( $init , $container ) ;

// Declared explicitly. ⚠ Read the constant through the CLASS that uses the
// trait — `DefaultLangTrait::DEFAULT_LANG` is a fatal error since PHP 8.4:
new ProductModel( $container , [ ProductModel::DEFAULT_LANG => 'en' ] ) ;

// Or site-wide, through the container entry of the same name:
'defaultLang' => 'fr' ,
author

Marc Alcaraz

since
1.3.0

Table of Contents

Constants

DEFAULT_LANG  : string = 'defaultLang'
The 'defaultLang' parameter.

Properties

$defaultLang  : string|null
The default (fallback) language code, or null when none is declared.

Methods

initializeDefaultLang()  : static
Initialize the default language, from the `$init` definition or, failing that, from the container entry of the same name.

Constants

DEFAULT_LANG

The 'defaultLang' parameter.

public string DEFAULT_LANG = 'defaultLang'

Properties

$defaultLang

The default (fallback) language code, or null when none is declared.

public string|null $defaultLang = null

Methods

initializeDefaultLang()

Initialize the default language, from the `$init` definition or, failing that, from the container entry of the same name.

public initializeDefaultLang([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static

The tag is lowercased so a single spelling reaches whatever consumes it, and validated on the spot: a language tag names a key of a translations map, so it is interpolated verbatim rather than passed as a parameter, and a declaration nobody can fix from a request must fail loudly rather than quietly answer on nothing.

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

Optional definition carrying a defaultLang entry.

$container : ContainerInterface|null = null

Optional PSR-11 container for a site-wide fallback.

Tags
throws
ContainerExceptionInterface

If the container fails while retrieving the entry.

NotFoundExceptionInterface

If the container has no such entry.

ValidationException

If the declared value is not a valid language code.

Return values
static
On this page

Search results