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. |
| 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
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 translation using separate line and missing-key replacements.
Get the specified string translation value.
Ensure a translation value is a string.
Get the specified array translation value.
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.
Get the translation groups missing from the current execution.
Mark a translation group as missing from the current execution.
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 determine the applicable locale array.
Get the message selector instance.
Get the current locale being used.
Get the current locale being used.
Set the current locale.
Set the base locale used by the translator.
Ensure the locale is safe to use as part of a translation path.
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 561
array
parseKey(string $key)
Parse a key into namespace, group, and item.
in
NamespacedItemResolver at line 31
protected array
parseBasicSegments(array $segments)
Parse an array of basic segments.
in
NamespacedItemResolver at line 51
protected array
parseNamespacedSegments(string $key)
Parse an array of namespaced segments.
in
NamespacedItemResolver at line 74
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.
in
NamespacedItemResolver at line 82
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 26
protected string
firstClosureParameterType(Closure $closure)
Get the class name of the first parameter of the given Closure.
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.
in
ReflectsClosures at line 75
protected array
closureParameterTypes(Closure $closure)
Get the class names / types of the parameters of the given Closure.
in
ReflectsClosures at line 97
protected array
closureReturnTypes(Closure $closure)
Get the class names / types of the return type of the given Closure.
at line 108
__construct(Loader $loader, string $locale)
Create a new translator instance.
at line 120
bool
hasForLocale(string $key, string|null $locale = null)
Determine if a translation exists for a given locale.
at line 128
bool
has(string $key, string|null $locale = null, bool $fallback = true)
Determine if a translation exists.
at line 149
array|string
get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the translation for the given key.
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.
at line 215
string
string(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the specified string translation value.
at line 226
protected string
ensureStringTranslation(string $key, array|string $value)
Ensure a translation value is a string.
at line 244
array
array(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the specified array translation value.
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.
at line 287
protected string
localeForChoice(string $key, string|null $locale)
Get the proper locale for a choice operation.
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.
at line 317
protected array|string
makeReplacements(array|string $line, array $replace)
Make the place-holder replacements on a line.
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.
at line 392
void
load(string $namespace, string $group, string $locale)
Load the specified language group.
at line 426
protected bool
isLoaded(string $namespace, string $group, string $locale)
Determine if the given group has been loaded.
at line 434
protected MissingTranslationGroups|null
missingTranslationGroups()
Get the translation groups missing from the current execution.
at line 445
protected void
markMissingTranslationGroup(string $namespace, string $group, string $locale)
Mark a translation group as missing from the current execution.
at line 462
protected string
handleMissingTranslationKey(string $key, array $replace, string|null $locale, bool $fallback)
Handle a missing translation key.
at line 484
protected bool
shouldHandleMissingTranslationKeys()
Determine if missing translation keys should be handled in this coroutine.
at line 492
protected mixed
withoutMissingKeyHandling(callable $callback)
Run the callback without firing missing-key callbacks in this coroutine.
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.
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.
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.
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.
at line 575
protected array
localeArray(string|null $locale)
Get the array of locales to be checked.
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.
at line 598
MessageSelector
getSelector()
Get the message selector instance.
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.
at line 621
Loader
getLoader()
Get the language line loader implementation.
at line 629
string
locale()
Get the current locale being used.
at line 637
string
getLocale()
Get the current locale being used.
at line 647
void
setLocale(string $locale)
Set the current locale.
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.
at line 674
protected void
ensureValidLocale(string $locale)
Ensure the locale is safe to use as part of a translation path.
at line 685
string
getFallback()
Get the fallback locale being used.
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.
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.
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.
at line 747
static void
flushState()
Flush all static state.