class Translator extends NamespacedItemResolver implements Translator (View source)

Traits

Properties

protected array $parsed

A cache of the parsed items.

from  NamespacedItemResolver
static protected array $macros

The registered string macros.

from  Macroable
protected Loader $loader

The loader implementation.

protected string $locale

The default locale being used by the translator.

protected string $fallback

The fallback locale used by the translator.

protected array $loaded

The array of loaded translation groups.

protected MessageSelector $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 callable|null $missingTranslationKeyCallback

The callback that is responsible for handling missing translation keys.

protected bool $handleMissingTranslationKeys

Indicates whether missing translation keys should be handled.

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, object|callable $macro)

Register a custom macro.

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

Mix another object into the class.

static bool
hasMacro(string $name)

Checks 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.

void
__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, Countable|int|float|array $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.

string|array|null
getLine(string $namespace, string $group, string $locale, string $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.

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
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.

Details

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

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

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

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

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

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

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

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

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

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

void __construct(Loader $loader, string $locale)

Create a new translator instance.

Parameters

Loader $loader
string $locale

Return Value

void

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

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

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

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

Get a translation according to an integer value.

Parameters

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

Return Value

string

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

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

Retrieve a language line out the loaded array.

Parameters

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

Return Value

string|array|null

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

Make the place-holder replacements on a line.

Parameters

string $line
array $replace

Return Value

string

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

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

Load the specified language group.

Parameters

string $namespace
string $group
string $locale

Return Value

void

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

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

Translator handleMissingKeysUsing(callable|null $callback)

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

Parameters

callable|null $callback

Return Value

Translator

void addNamespace(string $namespace, string $hint)

Add a new namespace to the loader.

Parameters

string $namespace
string $hint

Return Value

void

void addJsonPath(string $path)

Add a new JSON path to the loader.

Parameters

string $path

Return Value

void

protected array localeArray(string|null $locale)

Get the array of locales to be checked.

Parameters

string|null $locale

Return Value

array

void determineLocalesUsing(callable $callback)

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

Parameters

callable $callback

Return Value

void

MessageSelector getSelector()

Get the message selector instance.

Return Value

MessageSelector

void setSelector(MessageSelector $selector)

Set the message selector instance.

Parameters

MessageSelector $selector

Return Value

void

Loader getLoader()

Get the language line loader implementation.

Return Value

Loader

string locale()

Get the default locale being used.

Return Value

string

string getLocale()

Get the default locale being used.

Return Value

string

void setLocale(string $locale)

Set the default locale.

Parameters

string $locale

Return Value

void

Exceptions

InvalidArgumentException

string getFallback()

Get the fallback locale being used.

Return Value

string

void setFallback(string $fallback)

Set the fallback locale being used.

Parameters

string $fallback

Return Value

void

void setLoaded(array $loaded)

Set the loaded translation groups.

Parameters

array $loaded

Return Value

void

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