class Translator extends NamespacedItemResolver implements Translator

Traits

Constants

protected LOCALE_CONTEXT_KEY

Context key for the per-request locale override.

protected MISSING_KEY_HANDLING_CONTEXT_KEY

Context key for suppressing missing-key callbacks in the current coroutine.

protected MISSING_GROUPS_CONTEXT_KEY_PREFIX

Context key prefix for translation groups missing from one execution.

Properties

protected array $parsed

The explicitly seeded parsed items.

from  NamespacedItemResolver
static protected array $macros

The registered string macros.

from  Macroable
static protected int $nextTranslatorId

The next worker-unique translator identifier.

protected string $fallback

The fallback locale used by the translator.

protected string $locale

The base locale used by the translator.

protected array $loaded

The array of loaded translation groups. Empty loader results are excluded.

protected array<string, array<string, array<string, list<array{item: string, value: mixed}>>>> $pendingLines

Translation line operations waiting for their group to load.

protected string $missingGroupsContextKey

The coroutine-local key for this translator's missing groups.

protected MessageSelector|null $selector

The message selector.

protected ?callable $determineLocalesUsing

The callable that should be invoked to determine applicable locales.

protected array<class-string, callable> $stringableHandlers

The custom rendering callbacks for stringable objects.

protected null|callable $missingTranslationKeyCallback

The callback that is responsible for handling missing translation keys.

Methods

array
parseKey(string $key)

Parse a key into namespace, group, and item.

array
parseBasicSegments(array $segments)

Parse an array of basic segments.

array
parseNamespacedSegments(string $key)

Parse an array of namespaced segments.

void
setParsedKey(string $key, array $parsed)

Set the parsed value of a key.

void
flushParsedKeys()

Flush the cache of parsed keys.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

string
firstClosureParameterType(Closure $closure)

Get the class name of the first parameter of the given Closure.

array
firstClosureParameterTypes(Closure $closure)

Get the class names of the first parameter of the given Closure, including union types.

array
closureParameterTypes(Closure $closure)

Get the class names / types of the parameters of the given Closure.

array
closureReturnTypes(Closure $closure)

Get the class names / types of the return type of the given Closure.

__construct(Loader $loader, string $locale)

Create a new translator instance.

bool
hasForLocale(string $key, string|null $locale = null)

Determine if a translation exists for a given locale.

bool
has(string $key, string|null $locale = null, bool $fallback = true)

Determine if a translation exists.

array|string
get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)

Get the translation for the given key.

array|string
getTranslation(string $key, array $lineReplacements, array $missingKeyReplacements, string $locale, bool $fallback)

Get the translation using separate line and missing-key replacements.

string
string(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)

Get the specified string translation value.

string
ensureStringTranslation(string $key, array|string $value)

Ensure a translation value is a string.

array
array(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)

Get the specified array translation value.

string
choice(string $key, array|Countable|float|int $number, array $replace = [], string|null $locale = null)

Get a translation according to an integer value.

string
localeForChoice(string $key, string|null $locale)

Get the proper locale for a choice operation.

array|string|null
getLine(string $namespace, string $group, string $locale, string|null $item, array $replace)

Retrieve a language line out the loaded array.

array|string
makeReplacements(array|string $line, array $replace)

Make the place-holder replacements on a line.

void
addLines(array $lines, string $locale, string $namespace = '*')

Add translation lines to the given locale.

void
load(string $namespace, string $group, string $locale)

Load the specified language group.

bool
isLoaded(string $namespace, string $group, string $locale)

Determine if the given group has been loaded.

missingTranslationGroups()

Get the translation groups missing from the current execution.

void
markMissingTranslationGroup(string $namespace, string $group, string $locale)

Mark a translation group as missing from the current execution.

string
handleMissingTranslationKey(string $key, array $replace, string|null $locale, bool $fallback)

Handle a missing translation key.

bool
shouldHandleMissingTranslationKeys()

Determine if missing translation keys should be handled in this coroutine.

mixed
withoutMissingKeyHandling(callable $callback)

Run the callback without firing missing-key callbacks in this coroutine.

handleMissingKeysUsing(callable|null $callback)

Register a callback that is responsible for handling missing translation keys.

void
addNamespace(string $namespace, string $hint)

Add a new namespace to the loader.

void
addPath(string $path)

Add a new path to the loader.

void
addJsonPath(string $path)

Add a new JSON path to the loader.

array
localeArray(string|null $locale)

Get the array of locales to be checked.

void
determineLocalesUsing(callable $callback)

Specify a callback that should be invoked to determine the applicable locale array.

getSelector()

Get the message selector instance.

void
setSelector(MessageSelector $selector)

Set the message selector instance.

getLoader()

Get the language line loader implementation.

string
locale()

Get the current locale being used.

string
getLocale()

Get the current locale being used.

void
setLocale(string $locale)

Set the current locale.

void
setBaseLocale(string $locale)

Set the base locale used by the translator.

void
ensureValidLocale(string $locale)

Ensure the locale is safe to use as part of a translation path.

string
getFallback()

Get the fallback locale being used.

void
setFallback(string $fallback)

Set the fallback locale being used.

void
setLoaded(array $loaded)

Set the loaded translation groups.

void
stringable(Closure|string $class, callable|null $handler = null)

Add a handler to be executed in order to format a given class to a string during translation replacements.

static void
flushState()

Flush all static state.

Details

at line 561
array parseKey(string $key)

Parse a key into namespace, group, and item.

Parameters

string $key

Return Value

array

protected array parseBasicSegments(array $segments)

Parse an array of basic segments.

Parameters

array $segments

Return Value

array

protected array parseNamespacedSegments(string $key)

Parse an array of namespaced segments.

Parameters

string $key

Return Value

array

void setParsedKey(string $key, array $parsed)

Set the parsed value of a key.

Entries remain cached for this resolver instance's lifetime, which is the worker lifetime when called on the shared translator.

Parameters

string $key
array $parsed

Return Value

void

void flushParsedKeys()

Flush the cache of parsed keys.

Return Value

void

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in ReflectsClosures at line 26
protected string firstClosureParameterType(Closure $closure)

Get the class name of the first parameter of the given Closure.

Parameters

Closure $closure

Return Value

string

Exceptions

ReflectionException
RuntimeException

in ReflectsClosures at line 49
protected array firstClosureParameterTypes(Closure $closure)

Get the class names of the first parameter of the given Closure, including union types.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException
RuntimeException

in ReflectsClosures at line 75
protected array closureParameterTypes(Closure $closure)

Get the class names / types of the parameters of the given Closure.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException

in ReflectsClosures at line 97
protected array closureReturnTypes(Closure $closure)

Get the class names / types of the return type of the given Closure.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException

at line 108
__construct(Loader $loader, string $locale)

Create a new translator instance.

Parameters

Loader $loader

The loader implementation

string $locale

the default locale being used by the translator

at line 120
bool hasForLocale(string $key, string|null $locale = null)

Determine if a translation exists for a given locale.

Parameters

string $key
string|null $locale

Return Value

bool

at line 128
bool has(string $key, string|null $locale = null, bool $fallback = true)

Determine if a translation exists.

Parameters

string $key
string|null $locale
bool $fallback

Return Value

bool

at line 149
array|string get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)

Get the translation for the given key.

Parameters

string $key
array $replace
string|null $locale
bool $fallback

Return Value

array|string

at line 159
protected array|string getTranslation(string $key, array $lineReplacements, array $missingKeyReplacements, string $locale, bool $fallback)

Get the translation using separate line and missing-key replacements.

Parameters

string $key
array $lineReplacements
array $missingKeyReplacements
string $locale
bool $fallback

Return Value

array|string

at line 215
string string(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)

Get the specified string translation value.

Parameters

string $key
array $replace
string|null $locale
bool $fallback

Return Value

string

Exceptions

InvalidArgumentException

at line 226
protected string ensureStringTranslation(string $key, array|string $value)

Ensure a translation value is a string.

Parameters

string $key
array|string $value

Return Value

string

at line 244
array array(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)

Get the specified array translation value.

Parameters

string $key
array $replace
string|null $locale
bool $fallback

Return Value

array

Exceptions

InvalidArgumentException

at line 260
string choice(string $key, array|Countable|float|int $number, array $replace = [], string|null $locale = null)

Get a translation according to an integer value.

Parameters

string $key
array|Countable|float|int $number
array $replace
string|null $locale

Return Value

string

at line 287
protected string localeForChoice(string $key, string|null $locale)

Get the proper locale for a choice operation.

Parameters

string $key
string|null $locale

Return Value

string

at line 297
protected array|string|null getLine(string $namespace, string $group, string $locale, string|null $item, array $replace)

Retrieve a language line out the loaded array.

Parameters

string $namespace
string $group
string $locale
string|null $item
array $replace

Return Value

array|string|null

at line 317
protected array|string makeReplacements(array|string $line, array $replace)

Make the place-holder replacements on a line.

Parameters

array|string $line
array $replace

Return Value

array|string

at line 369
void addLines(array $lines, string $locale, string $namespace = '*')

Add translation lines to the given locale.

Boot-only. The lines are stored on the shared Translator instance and affect every subsequent lookup in the worker.

Parameters

array $lines
string $locale
string $namespace

Return Value

void

at line 392
void load(string $namespace, string $group, string $locale)

Load the specified language group.

Parameters

string $namespace
string $group
string $locale

Return Value

void

at line 426
protected bool isLoaded(string $namespace, string $group, string $locale)

Determine if the given group has been loaded.

Parameters

string $namespace
string $group
string $locale

Return Value

bool

at line 434
protected MissingTranslationGroups|null missingTranslationGroups()

Get the translation groups missing from the current execution.

Return Value

MissingTranslationGroups|null

at line 445
protected void markMissingTranslationGroup(string $namespace, string $group, string $locale)

Mark a translation group as missing from the current execution.

Parameters

string $namespace
string $group
string $locale

Return Value

void

at line 462
protected string handleMissingTranslationKey(string $key, array $replace, string|null $locale, bool $fallback)

Handle a missing translation key.

Parameters

string $key
array $replace
string|null $locale
bool $fallback

Return Value

string

at line 484
protected bool shouldHandleMissingTranslationKeys()

Determine if missing translation keys should be handled in this coroutine.

Return Value

bool

at line 492
protected mixed withoutMissingKeyHandling(callable $callback)

Run the callback without firing missing-key callbacks in this coroutine.

Parameters

callable $callback

Return Value

mixed

at line 518
Translator handleMissingKeysUsing(callable|null $callback)

Register a callback that is responsible for handling missing translation keys.

The callback receives the key, caller-supplied replacements, locale, and fallback flag. Choice lookups add the automatic count replacement only after selecting the plural form.

Boot-only. The callback is stored on the shared Translator instance and affects every subsequent missing-key lookup in the worker.

Parameters

callable|null $callback

Return Value

Translator

at line 531
void addNamespace(string $namespace, string $hint)

Add a new namespace to the loader.

Boot-only. The namespace is added to the shared loader and affects every subsequent translation lookup in the worker.

Parameters

string $namespace
string $hint

Return Value

void

at line 542
void addPath(string $path)

Add a new path to the loader.

Boot-only. The path is added to the shared loader and affects every subsequent translation lookup in the worker.

Parameters

string $path

Return Value

void

at line 553
void addJsonPath(string $path)

Add a new JSON path to the loader.

Boot-only. The path is added to the shared loader and affects every subsequent translation lookup in the worker.

Parameters

string $path

Return Value

void

at line 575
protected array localeArray(string|null $locale)

Get the array of locales to be checked.

Parameters

string|null $locale

Return Value

array

at line 590
void determineLocalesUsing(callable $callback)

Specify a callback that should be invoked to determine the applicable locale array.

Boot-only. The callback is stored on the shared Translator instance and affects every subsequent locale resolution in the worker.

Parameters

callable $callback

Return Value

void

at line 598
MessageSelector getSelector()

Get the message selector instance.

Return Value

MessageSelector

at line 613
void setSelector(MessageSelector $selector)

Set the message selector instance.

Boot-only. The selector is stored on the shared Translator instance and affects every subsequent choice() call in the worker.

Parameters

MessageSelector $selector

Return Value

void

at line 621
Loader getLoader()

Get the language line loader implementation.

Return Value

Loader

at line 629
string locale()

Get the current locale being used.

Return Value

string

at line 637
string getLocale()

Get the current locale being used.

Return Value

string

at line 647
void setLocale(string $locale)

Set the current locale.

Parameters

string $locale

Return Value

void

Exceptions

InvalidArgumentException

at line 662
void setBaseLocale(string $locale)

Set the base locale used by the translator.

Boot-only. The locale is shared by the worker's Translator instance and affects requests without a coroutine-local locale override.

Parameters

string $locale

Return Value

void

Exceptions

InvalidArgumentException

at line 674
protected void ensureValidLocale(string $locale)

Ensure the locale is safe to use as part of a translation path.

Parameters

string $locale

Return Value

void

Exceptions

InvalidArgumentException

at line 685
string getFallback()

Get the fallback locale being used.

Return Value

string

at line 697
void setFallback(string $fallback)

Set the fallback locale being used.

Boot-only. The fallback is shared by the worker's Translator instance and affects every subsequent translation lookup in that worker. Use setLocale() for a current-request locale override.

Parameters

string $fallback

Return Value

void

at line 710
void setLoaded(array $loaded)

Set the loaded translation groups.

Boot-only. The groups are stored on the shared Translator instance and affect every subsequent translation lookup in the worker.

Parameters

array $loaded

Return Value

void

at line 728
void stringable(Closure|string $class, callable|null $handler = null)

Add a handler to be executed in order to format a given class to a string during translation replacements.

Boot-only. The handler is stored on the shared Translator instance and affects every subsequent replacement in the worker.

Parameters

Closure|string $class
callable|null $handler

Return Value

void

at line 747
static void flushState()

Flush all static state.

Return Value

void