Skip to content

TranslationManager

The translation manager manages the interface between the application and the current translation engine implementation

class TranslationManager implements ResetInterface

ImplementsResetInterface
Since1.0.0
SourceTranslation/TranslationManager.php
ConstantValueDescription
CURRENCY'cur'
DATETIME'date'
MESSAGE'msg'
NUMBER'num'
MethodDescription
[`_(mixed $message, ?string $domain = null, QuioteLocalestring
[`__(string $singularMessage, string $pluralMessage, int $amount, ?string $domain = null, QuioteLocalestring
[`_c(mixed $number, ?string $domain = null, QuioteLocalestring
[`_d(mixed $date, ?string $domain = null, QuioteLocalestring
[`_n(mixed $number, ?string $domain = null, QuioteLocalestring
createTimeZone(mixed $id, bool $cache = true): ?DateTimeZoneCreates a new timezone instance for the given identifier.
getAvailableLocales(): array<string, array{identifier: string, identifierData: array{language: ?string, script: ?string, territory: ?string, variant: ?string, options: array<string, string>, locale_str: ?string, option_str: ?string}, parameters: array<string, mixed>}>Returns the list of available locales.
getContext(): ContextRetrieve the current application context.
getCurrencyFraction(string $currency): array{digits: int, rounding: int}Returns an array containing digits and rounding information for a currency.
getCurrentLocale(): ?QuioteLocaleRetrieve the current locale.
getCurrentLocaleIdentifier(): ?stringRetrieve the current locale identifier.
getCurrentTimeZone(): ?DateTimeZoneGets the instance of the current timezone.
getDefaultDomain(): stringRetrieve the default domain.
getDefaultLocale(): QuioteLocaleRetrieve the default locale.
getDefaultLocaleIdentifier(): ?stringRetrieve the default locale identifier.
getDefaultTimeZone(): ?DateTimeZoneGet the default timezone instance.
getDomainTranslator(string $domain, string $type): ?ITranslatorReturns the translators for a given domain and type.
getLocale(string $identifier, bool $forceNew = false): QuioteLocaleReturns a new Locale object from the given identifier.
getLocaleIdentifier(string $identifier): stringReturns the identifier of the available locale which matches the given locale identifier most.
getMatchingLocaleIdentifiers(string $identifier): array<int, string>Returns all the identifiers of the available locales which match the given locale identifier.
getTerritoryData(string $country): array<string, mixed>Returns the stored information from the ldml supplemental data about a territory.
getTimeZoneTerritory(string $id, bool &$hasMultipleZones = false): ?stringGets the territory id a (resolved) timezone id belongs to.
initialize(Context $context, array<string, mixed> $parameters = []): voidInitialize this TranslationManager.
reset(): voidReset per-request locale state for worker compatibility.
[`resolveTimeZoneId(DateTimeZonestring $id): ?string`](#resolvetimezoneid)
setDefaultDomain(string $domain): voidSets the default domain.
[`setDefaultTimeZone(DateTimeZonestring $id): void`](#setdefaulttimezone)
setLocale(string $identifier): voidSets the current locale.
shutdown(): voidExecute the shutdown procedure.
startup(): voidDo any necessary startup work after initialization.

public function _(mixed $message, ?string $domain = null, QuioteLocale|string|null $locale = null, ?array<int, mixed> $parameters = null): string

Translate a message into the current locale.

The parameters which should be used for sprintf on the translated string.

ParameterTypeDescription
$messagemixedThe message.
$domain?``stringThe domain in which the translation should be done.
$localeQuioteLocale`string
$parameters?``array``<``int``, ``mixed``>The parameters which should be used for sprintf on the translated string.

Returns string — The translated message.

public function __(string $singularMessage, string $pluralMessage, int $amount, ?string $domain = null, QuioteLocale|string|null $locale = null, ?array<int, mixed> $parameters = null): string

Translate a singular/plural message into the current locale.

The parameters which should be used for sprintf on the translated string.

ParameterTypeDescription
$singularMessagestringThe message for the singular form.
$pluralMessagestringThe message for the plural form.
$amountintThe amount for which the translation should happen.
$domain?``stringThe domain in which the translation should be done.
$localeQuioteLocale`string
$parameters?``array``<``int``, ``mixed``>The parameters which should be used for sprintf on the translated string.

Returns string — The translated message.

public function _c(mixed $number, ?string $domain = null, QuioteLocale|string|null $locale = null): string

Formats a currency amount in the current locale.

The locale which should be used for formatting. Defaults to the currently active locale.

ParameterTypeDescription
$numbermixedThe number to be formatted.
$domain?``stringThe domain in which the amount should be formatted.
$localeQuioteLocale`string

Returns string — The formatted number.

public function _d(mixed $date, ?string $domain = null, QuioteLocale|string|null $locale = null): string

Formats a date in the current locale.

The locale which should be used for formatting. Defaults to the currently active locale.

ParameterTypeDescription
$datemixedThe date to be formatted.
$domain?``stringThe domain in which the date should be formatted.
$localeQuioteLocale`string

Returns string — The formatted date.

public function _n(mixed $number, ?string $domain = null, QuioteLocale|string|null $locale = null): string

Formats a number in the current locale.

The locale which should be used for formatting. Defaults to the currently active locale.

ParameterTypeDescription
$numbermixedThe number to be formatted.
$domain?``stringThe domain in which the number should be formatted.
$localeQuioteLocale`string

Returns string — The formatted number.

public function createTimeZone(mixed $id, bool $cache = true): ?DateTimeZone

Creates a new timezone instance for the given identifier.

Whether to use/populate the timezone instance cache.

ParameterTypeDescription
$idmixedThe timezone identifier
$cacheboolWhether to use/populate the timezone instance cache.

Returns ?``DateTimeZone — The timezone instance for the given id.

public function getAvailableLocales(): array<string, array{identifier: string, identifierData: array{language: ?string, script: ?string, territory: ?string, variant: ?string, options: array<string, string>, locale_str: ?string, option_str: ?string}, parameters: array<string, mixed>}>

Returns the list of available locales.

Returns array``<``string``, ``array{identifier: string, identifierData: array{language: ?string, script: ?string, territory: ?string, variant: ?string, options: array<string, string>, locale_str: ?string, option_str: ?string}, parameters: array<string, mixed>}``>

final public function getContext(): Context

Retrieve the current application context.

Returns Context — The current Context instance.

public function getCurrencyFraction(string $currency): array{digits: int, rounding: int}

Returns an array containing digits and rounding information for a currency.

The uppercase 3 letter currency iso code.

ParameterTypeDescription
$currencystringThe uppercase 3 letter currency iso code.

Returns array{digits: int, rounding: int} — The data.

public function getCurrentLocale(): ?QuioteLocale

Retrieve the current locale.

Returns ?QuioteLocale — The current locale.

public function getCurrentLocaleIdentifier(): ?string

Retrieve the current locale identifier.

This may not necessarily match what has be given to setLocale() but instead the identifier of the closest match from the available locales.

Returns ?``string — The locale identifier.

public function getCurrentTimeZone(): ?DateTimeZone

Gets the instance of the current timezone.

Returns ?``DateTimeZone — The current timezone instance.

public function getDefaultDomain(): string

Retrieve the default domain.

Returns string — The default domain.

public function getDefaultLocale(): QuioteLocale

Retrieve the default locale.

Returns QuioteLocale — The current default.

public function getDefaultLocaleIdentifier(): ?string

Retrieve the default locale identifier.

Returns ?``string — The default locale identifier.

public function getDefaultTimeZone(): ?DateTimeZone

Get the default timezone instance.

Returns ?``DateTimeZone — The default timezone instance.

public function getDomainTranslator(string $domain, string $type): ?ITranslator

Returns the translators for a given domain and type.

The type of the translator.

ParameterTypeDescription
$domainstringThe domain.
$typestringThe type of the translator.

Returns ?ITranslator — The translator instance.

public function getLocale(string $identifier, bool $forceNew = false): QuioteLocale

Returns a new Locale object from the given identifier.

Force a new instance even if an identical one exists.

ParameterTypeDescription
$identifierstringThe locale identifier
$forceNewboolForce a new instance even if an identical one exists.

Returns QuioteLocale — The locale instance which matches the available locales most.

public function getLocaleIdentifier(string $identifier): string

Returns the identifier of the available locale which matches the given locale identifier most.

A locale identifier

ParameterTypeDescription
$identifierstringA locale identifier

Returns string — The actual locale identifier of the available locale.

public function getMatchingLocaleIdentifiers(string $identifier): array<int, string>

Returns all the identifiers of the available locales which match the given locale identifier.

A locale identifier

ParameterTypeDescription
$identifierstringA locale identifier

Returns array``<``int``, ``string``> — The actual locale identifiers of the available locales.

public function getTerritoryData(string $country): array<string, mixed>

Returns the stored information from the ldml supplemental data about a territory.

The uppercase 2 letter country iso code.

ParameterTypeDescription
$countrystringThe uppercase 2 letter country iso code.

Returns array``<``string``, ``mixed``> — The data.

public function getTimeZoneTerritory(string $id, bool &$hasMultipleZones = false): ?string

Gets the territory id a (resolved) timezone id belongs to.

Will receive whether the territory has multiple time zones

ParameterTypeDescription
$idstringThe resolved timezone id.
$hasMultipleZonesboolWill receive whether the territory has multiple time zones

Returns ?``string — The territory identifier or null.

public function initialize(Context $context, array<string, mixed> $parameters = []): void

Initialize this TranslationManager.

An associative array of initialization parameters.

ParameterTypeDescription
$contextContextThe current application context.
$parametersarray``<``string``, ``mixed``>An associative array of initialization parameters.

public function reset(): void

Reset per-request locale state for worker compatibility.

A request that called setLocale() must not influence the next one, so the selected locale is dropped. It is restored to the configured default rather than left empty: Context::reset() calls this between requests and the manager is reused as-is, without a second initialize(), so a manager with no locale at all would make the next request’s first locale-dependent lookup fail — e.g. StreamTemplateLayer asking for the current identifier to build its template lookup path, which rejects an empty one.

Every registered translator is reset too, unconditionally: setLocale() above notifies translators of the new locale via loadCurrentLocale(), but only when there IS a default locale to fall back to and only once loadCurrentLocale() next runs. Without a default locale (or before that lazy re-check happens), translators would otherwise keep serving the previous request’s locale/domain data — the exact cross-request bleed this method exists to prevent.

public function resolveTimeZoneId(DateTimeZone|string $id): ?string

Resolved the given timezone identifier to its ‘real’ timezone id.

The timezone id to be resolved

ParameterTypeDescription
$id`DateTimeZone““string`

Returns ?``string — The resolved timezone id

public function setDefaultDomain(string $domain): void

Sets the default domain.

The new default domain.

ParameterTypeDescription
$domainstringThe new default domain.

public function setDefaultTimeZone(DateTimeZone|string $id): void

Sets the default time zone.

The timezone identifier

ParameterTypeDescription
$id`DateTimeZone““string`

public function setLocale(string $identifier): void

Sets the current locale.

The locale identifier.

ParameterTypeDescription
$identifierstringThe locale identifier.

public function shutdown(): void

Execute the shutdown procedure.

public function startup(): void

Do any necessary startup work after initialization.

This method is not called directly after initialize().