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.

Properties

protected array $parsed

A cache of the parsed items.

from  NamespacedItemResolver
static protected array $macros

The registered string macros.

from  Macroable
protected string|null $fallback

The fallback locale used by the translator.

protected array $loaded

The array of loaded translation groups.

protected MessageSelector|null $selector

The message selector.

protected ?callable $determineLocalesUsing

The callable that should be invoked to determine applicable locales.

protected array $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.

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

Get the translation for the given key.

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.

string
makeReplacements(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.

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 determined 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 default locale being used.

string
getLocale()

Get the default locale being used.

void
setLocale(string $locale)

Set the default locale.

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(callable|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 383
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.

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 25
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 48
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 74
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 94
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 76
__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 86
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 94
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 115
mixed 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

mixed

at line 166
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 194
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 204
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 227
protected string makeReplacements(string $line, array $replace)

Make the place-holder replacements on a line.

Parameters

string $line
array $replace

Return Value

string

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

Add translation lines to the given locale.

Parameters

array $lines
string $locale
string $namespace

Return Value

void

at line 278
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 295
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 303
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 325
protected bool shouldHandleMissingTranslationKeys()

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

Return Value

bool

at line 333
protected mixed withoutMissingKeyHandling(callable $callback)

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

Parameters

callable $callback

Return Value

mixed

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

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

Parameters

callable|null $callback

Return Value

Translator

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

Add a new namespace to the loader.

Parameters

string $namespace
string $hint

Return Value

void

at line 367
void addPath(string $path)

Add a new path to the loader.

Parameters

string $path

Return Value

void

at line 375
void addJsonPath(string $path)

Add a new JSON path to the loader.

Parameters

string $path

Return Value

void

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

Get the array of locales to be checked.

Parameters

string|null $locale

Return Value

array

at line 409
void determineLocalesUsing(callable $callback)

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

Parameters

callable $callback

Return Value

void

at line 417
MessageSelector getSelector()

Get the message selector instance.

Return Value

MessageSelector

at line 432
void setSelector(MessageSelector $selector)

Set the message selector instance.

Boot-only. The selector is held on the singleton Translator and used for every choice() call across all coroutines.

Parameters

MessageSelector $selector

Return Value

void

at line 440
Loader getLoader()

Get the language line loader implementation.

Return Value

Loader

at line 448
string locale()

Get the default locale being used.

Return Value

string

at line 456
string getLocale()

Get the default locale being used.

Return Value

string

at line 466
void setLocale(string $locale)

Set the default locale.

Parameters

string $locale

Return Value

void

Exceptions

InvalidArgumentException

at line 478
string getFallback()

Get the fallback locale being used.

Return Value

string

at line 490
void setFallback(string $fallback)

Set the fallback locale being used.

Boot-only. The fallback is shared across all coroutines on the singleton Translator; per-request use races and affects every concurrent lookup. For per-request locale overrides use setLocale(), which is Context-scoped.

Parameters

string $fallback

Return Value

void

at line 502
void setLoaded(array $loaded)

Set the loaded translation groups.

Boot-only. Overwrites the singleton Translator's loaded-translation cache used across all coroutines; per-request use races and breaks concurrent lookups.

Parameters

array $loaded

Return Value

void

at line 510
void stringable(callable|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.

Parameters

callable|string $class
callable|null $handler

Return Value

void

at line 525
static void flushState()

Flush all static state.

Return Value

void