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 564
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 30
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 44
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 61
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 72
static void
flushMacros()
Flush the existing macros.
Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 83
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 108
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 109
__construct(Loader $loader, string $locale)
Create a new translator instance.
at line 121
bool
hasForLocale(string $key, string|null $locale = null)
Determine if a translation exists for a given locale.
at line 129
bool
has(string $key, string|null $locale = null, bool $fallback = true)
Determine if a translation exists.
at line 150
array|string
get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the translation for the given key.
at line 160
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 216
string
string(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the specified string translation value.
at line 227
protected string
ensureStringTranslation(string $key, array|string $value)
Ensure a translation value is a string.
at line 245
array
array(string $key, array $replace = [], string|null $locale = null, bool $fallback = true)
Get the specified array translation value.
at line 261
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 288
protected string
localeForChoice(string $key, string|null $locale)
Get the proper locale for a choice operation.
at line 298
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 318
protected array|string
makeReplacements(array|string $line, array $replace)
Make the place-holder replacements on a line.
at line 370
void
addLines(array $lines, string $locale, string $namespace = '*')
Add translation lines to the given locale.
The lines are stored on the shared Translator instance and affect every subsequent lookup in the worker.
at line 394
void
load(string $namespace, string $group, string $locale)
Load the specified language group.
at line 428
protected bool
isLoaded(string $namespace, string $group, string $locale)
Determine if the given group has been loaded.
at line 436
protected MissingTranslationGroups|null
missingTranslationGroups()
Get the translation groups missing from the current execution.
at line 447
protected void
markMissingTranslationGroup(string $namespace, string $group, string $locale)
Mark a translation group as missing from the current execution.
at line 464
protected string
handleMissingTranslationKey(string $key, array $replace, string|null $locale, bool $fallback)
Handle a missing translation key.
at line 486
protected bool
shouldHandleMissingTranslationKeys()
Determine if missing translation keys should be handled in this coroutine.
at line 494
protected mixed
withoutMissingKeyHandling(callable $callback)
Run the callback without firing missing-key callbacks in this coroutine.
at line 520
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.
The callback is stored on the shared Translator instance and affects every subsequent missing-key lookup in the worker.
at line 534
void
addNamespace(string $namespace, string $hint)
Add a new namespace to the loader.
When the loader is the application's worker-shared loader, the namespace affects every subsequent translation lookup in the worker.
at line 545
void
addPath(string $path)
Add a new path to the loader.
When the loader is the application's worker-shared loader, the path affects every subsequent translation lookup in the worker.
at line 556
void
addJsonPath(string $path)
Add a new JSON path to the loader.
When the loader is the application's worker-shared loader, the path affects every subsequent translation lookup in the worker.
at line 578
protected array
localeArray(string|null $locale)
Get the array of locales to be checked.
at line 593
void
determineLocalesUsing(callable $callback)
Specify a callback that should be invoked to determine the applicable locale array.
The callback is stored on the shared Translator instance and affects every subsequent locale resolution in the worker.
at line 602
MessageSelector
getSelector()
Get the message selector instance.
at line 617
void
setSelector(MessageSelector $selector)
Set the message selector instance.
The selector is stored on the shared Translator instance and affects every subsequent choice() call in the worker.
at line 626
Loader
getLoader()
Get the language line loader implementation.
at line 634
string
locale()
Get the current locale being used.
at line 642
string
getLocale()
Get the current locale being used.
at line 652
void
setLocale(string $locale)
Set the current locale.
at line 667
void
setBaseLocale(string $locale)
Set the base locale used by the translator.
The locale is shared by the worker's Translator instance and affects requests without a coroutine-local locale override.
at line 680
protected void
ensureValidLocale(string $locale)
Ensure the locale is safe to use as part of a translation path.
at line 691
string
getFallback()
Get the fallback locale being used.
at line 703
void
setFallback(string $fallback)
Set the fallback locale being used.
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 717
void
setLoaded(array $loaded)
Set the loaded translation groups.
The groups are stored on the shared Translator instance and affect every subsequent translation lookup in the worker.
at line 736
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.
The handler is stored on the shared Translator instance and affects every subsequent replacement in the worker.
at line 756
static void
flushState()
Flush all static state.