class Translator extends NamespacedItemResolver implements Translator

Traits

Properties

protected array $parsed

A cache of the parsed items.

from  NamespacedItemResolver
static protected array $macros

The registered string macros.

from  Macroable
protected LoaderInterface $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.

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.

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

Register a custom macro.

static bool
hasMacro(string $name)

Checks if macro is registered.

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.

void
__construct(LoaderInterface $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
trans(string $key, array $replace = [], string $locale = null)

Get the translation for a given key.

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

Get the translation for the given key.

string
getFromJson(string $key, array $replace = [], string $locale = null)

Get the translation for a given key from the JSON translation files.

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

Get a translation according to an integer value.

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

Get a translation according to an integer value.

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

array
sortReplacements(array $replace)

Sort the replacements array.

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.

void
addNamespace(string $namespace, string $hint)

Add a new namespace to the loader.

array
localeArray(string|null $locale)

Get the array of locales to be checked.

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.

Details

at line 360
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

in Macroable at line 24
static void macro(string $name, callable $macro)

Register a custom macro.

Parameters

string $name
callable $macro

Return Value

void

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

Checks if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 49
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 71
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 59
void __construct(LoaderInterface $loader, string $locale)

Create a new translator instance.

Parameters

LoaderInterface $loader
string $locale

Return Value

void

at line 72
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 85
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 98
mixed trans(string $key, array $replace = [], string $locale = null)

Get the translation for a given key.

Parameters

string $key
array $replace
string $locale

Return Value

mixed

at line 112
string|array|null 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

string|array|null

at line 148
string getFromJson(string $key, array $replace = [], string $locale = null)

Get the translation for a given key from the JSON translation files.

Parameters

string $key
array $replace
string $locale

Return Value

string

at line 183
string transChoice(string $key, int|array|Countable $number, array $replace = [], string $locale = null)

Get a translation according to an integer value.

Parameters

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

Return Value

string

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

Get a translation according to an integer value.

Parameters

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

Return Value

string

at line 223
protected string localeForChoice(string|null $locale)

Get the proper locale for a choice operation.

Parameters

string|null $locale

Return Value

string

at line 238
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

at line 258
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 283
protected array sortReplacements(array $replace)

Sort the replacements array.

Parameters

array $replace

Return Value

array

at line 298
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 315
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 337
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 349
void addNamespace(string $namespace, string $hint)

Add a new namespace to the loader.

Parameters

string $namespace
string $hint

Return Value

void

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

Get the array of locales to be checked.

Parameters

string|null $locale

Return Value

array

at line 387
MessageSelector getSelector()

Get the message selector instance.

Return Value

MessageSelector

at line 402
void setSelector(MessageSelector $selector)

Set the message selector instance.

Parameters

MessageSelector $selector

Return Value

void

at line 412
LoaderInterface getLoader()

Get the language line loader implementation.

Return Value

LoaderInterface

at line 422
string locale()

Get the default locale being used.

Return Value

string

at line 432
string getLocale()

Get the default locale being used.

Return Value

string

at line 443
void setLocale(string $locale)

Set the default locale.

Parameters

string $locale

Return Value

void

at line 453
string getFallback()

Get the fallback locale being used.

Return Value

string

at line 464
void setFallback(string $fallback)

Set the fallback locale being used.

Parameters

string $fallback

Return Value

void