Translator
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
Parse a key into namespace, group, and item.
Parse an array of basic segments.
Parse an array of namespaced segments.
Set the parsed value of a key.
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically handle calls to the class.
Get the class name of the first parameter of the given Closure.
Get the class names of the first parameter of the given Closure, including union types.
Get the class names / types of the parameters of the given Closure.
Get the class names / types of the return type of the given Closure.
Determine if a translation exists for a given locale.
Determine if a translation exists.
Get the translation for the given key.
Get the proper locale for a choice operation.
Retrieve a language line out the loaded array.
Make the place-holder replacements on a line.
Add translation lines to the given locale.
Load the specified language group.
Determine if the given group has been loaded.
Handle a missing translation key.
Determine if missing translation keys should be handled in this coroutine.
Run the callback without firing missing-key callbacks in this coroutine.
Register a callback that is responsible for handling missing translation keys.
Add a new namespace to the loader.
Add a new path to the loader.
Add a new JSON path to the loader.
Get the array of locales to be checked.
Specify a callback that should be invoked to determined the applicable locale array.
Get the message selector instance.
Get the default locale being used.
Get the default locale being used.
Set the default locale.
Get the fallback locale being used.
Set the fallback locale being used.
Set the loaded translation groups.
Add a handler to be executed in order to format a given class to a string during translation replacements.
Flush all static state.
Details
at line 383
array
parseKey(string $key)
Parse a key into namespace, group, and item.
in
NamespacedItemResolver at line 46
protected array
parseBasicSegments(array $segments)
Parse an array of basic segments.
in
NamespacedItemResolver at line 66
protected array
parseNamespacedSegments(string $key)
Parse an array of namespaced segments.
in
NamespacedItemResolver at line 86
void
setParsedKey(string $key, array $parsed)
Set the parsed value of a key.
in
NamespacedItemResolver at line 94
void
flushParsedKeys()
Flush the cache of parsed keys.
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.
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.
in
Macroable at line 57
static bool
hasMacro(string $name)
Check if macro is registered.
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.
in
Macroable at line 78
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 103
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
in
ReflectsClosures at line 25
protected string
firstClosureParameterType(Closure $closure)
Get the class name of the first parameter of the given Closure.
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.
in
ReflectsClosures at line 74
protected array
closureParameterTypes(Closure $closure)
Get the class names / types of the parameters of the given Closure.
in
ReflectsClosures at line 94
protected array
closureReturnTypes(Closure $closure)
Get the class names / types of the return type of the given Closure.
at line 76
__construct(Loader $loader, string $locale)
Create a new translator instance.
at line 86
bool
hasForLocale(string $key, string|null $locale = null)
Determine if a translation exists for a given locale.
at line 94
bool
has(string $key, string|null $locale = null, bool $fallback = true)
Determine if a translation exists.
at line 115
mixed
get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the translation for the given key.
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.
at line 194
protected string
localeForChoice(string $key, string|null $locale)
Get the proper locale for a choice operation.
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.
at line 227
protected string
makeReplacements(string $line, array $replace)
Make the place-holder replacements on a line.
at line 266
void
addLines(array $lines, string $locale, string $namespace = '*')
Add translation lines to the given locale.
at line 278
void
load(string $namespace, string $group, string $locale)
Load the specified language group.
at line 295
protected bool
isLoaded(string $namespace, string $group, string $locale)
Determine if the given group has been loaded.
at line 303
protected string
handleMissingTranslationKey(string $key, array $replace, string|null $locale, bool $fallback)
Handle a missing translation key.
at line 325
protected bool
shouldHandleMissingTranslationKeys()
Determine if missing translation keys should be handled in this coroutine.
at line 333
protected mixed
withoutMissingKeyHandling(callable $callback)
Run the callback without firing missing-key callbacks in this coroutine.
at line 349
Translator
handleMissingKeysUsing(callable|null $callback)
Register a callback that is responsible for handling missing translation keys.
at line 359
void
addNamespace(string $namespace, string $hint)
Add a new namespace to the loader.
at line 367
void
addPath(string $path)
Add a new path to the loader.
at line 375
void
addJsonPath(string $path)
Add a new JSON path to the loader.
at line 397
protected array
localeArray(string|null $locale)
Get the array of locales to be checked.
at line 409
void
determineLocalesUsing(callable $callback)
Specify a callback that should be invoked to determined the applicable locale array.
at line 417
MessageSelector
getSelector()
Get the message selector instance.
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.
at line 440
Loader
getLoader()
Get the language line loader implementation.
at line 448
string
locale()
Get the default locale being used.
at line 456
string
getLocale()
Get the default locale being used.
at line 466
void
setLocale(string $locale)
Set the default locale.
at line 478
string
getFallback()
Get the fallback locale being used.
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.
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.
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.
at line 525
static void
flushState()
Flush all static state.