TranslatorRegistry
in package
Constructs and stores instances of translators that can be retrieved by name and locale.
Table of Contents
Constants
- FALLBACK_LOADER = '_fallback'
- Fallback loader name.
Properties
- $_cacher : CacheEngineInterface)|null
- A CacheEngine object that is used to remember translator across requests.
- $_defaultFormatter : string
- The name of the default formatter to use for newly created translators from the fallback loader
- $_loaders : array<string|int, callable>
- A list of loader functions indexed by domain name. Loaders are callables that are invoked as a default for building translation packages where none can be found for the combination of translator name and locale.
- $_useFallback : bool
- Use fallback-domain for translation loaders.
- $formatters : FormatterLocator
- A formatter locator.
- $locale : string
- The current locale code.
- $packages : PackageLocator
- A package locator.
- $registry : array<string, array<string, Translator>>
- A registry to retain translator objects.
Methods
- __construct() : mixed
- Constructor.
- defaultFormatter() : string
- Sets the name of the default messages formatter to use for future translator instances.
- get() : Translator|null
- Gets a translator from the registry by package for a locale.
- getFormatters() : FormatterLocator
- An object of type FormatterLocator
- getLocale() : string
- Returns the default locale code.
- getPackages() : PackageLocator
- Returns the translator packages
- registerLoader() : void
- Registers a loader function for a package name that will be used as a fallback in case no package with that name can be found.
- setCacher() : void
- Sets the CacheEngine instance used to remember translators across requests.
- setFallbackPackage() : Package
- Set fallback domain for package.
- setLoaderFallback() : callable
- Set domain fallback for loader.
- setLocale() : void
- Sets the default locale code.
- useFallback() : void
- Set if the default domain fallback is used.
- _getTranslator() : Translator
- Gets a translator from the registry by package for a locale.
- createInstance() : Translator
- Create translator instance.
Constants
FALLBACK_LOADER
Fallback loader name.
public
string
FALLBACK_LOADER
= '_fallback'
Properties
$_cacher
A CacheEngine object that is used to remember translator across requests.
protected
CacheEngineInterface)|null
$_cacher
$_defaultFormatter
The name of the default formatter to use for newly created translators from the fallback loader
protected
string
$_defaultFormatter
= 'default'
$_loaders
A list of loader functions indexed by domain name. Loaders are callables that are invoked as a default for building translation packages where none can be found for the combination of translator name and locale.
protected
array<string|int, callable>
$_loaders
= []
$_useFallback
Use fallback-domain for translation loaders.
protected
bool
$_useFallback
= true
$formatters
A formatter locator.
protected
FormatterLocator
$formatters
$locale
The current locale code.
protected
string
$locale
$packages
A package locator.
protected
PackageLocator
$packages
$registry
A registry to retain translator objects.
protected
array<string, array<string, Translator>>
$registry
= []
Methods
__construct()
Constructor.
public
__construct(PackageLocator $packages, FormatterLocator $formatters, string $locale) : mixed
Parameters
- $packages : PackageLocator
-
The package locator.
- $formatters : FormatterLocator
-
The formatter locator.
- $locale : string
-
The default locale code to use.
defaultFormatter()
Sets the name of the default messages formatter to use for future translator instances.
public
defaultFormatter([string|null $name = null ]) : string
If called with no arguments, it will return the currently configured value.
Parameters
- $name : string|null = null
-
The name of the formatter to use.
Return values
string —The name of the formatter.
get()
Gets a translator from the registry by package for a locale.
public
get(string $name[, string|null $locale = null ]) : Translator|null
Parameters
- $name : string
-
The translator package to retrieve.
- $locale : string|null = null
-
The locale to use; if empty, uses the default locale.
Tags
Return values
Translator|null —A translator object.
getFormatters()
An object of type FormatterLocator
public
getFormatters() : FormatterLocator
Return values
FormatterLocatorgetLocale()
Returns the default locale code.
public
getLocale() : string
Return values
stringgetPackages()
Returns the translator packages
public
getPackages() : PackageLocator
Return values
PackageLocatorregisterLoader()
Registers a loader function for a package name that will be used as a fallback in case no package with that name can be found.
public
registerLoader(string $name, callable $loader) : void
Loader callbacks will get as first argument the package name and the locale as the second argument.
Parameters
- $name : string
-
The name of the translator package to register a loader for
- $loader : callable
-
A callable object that should return a Package
setCacher()
Sets the CacheEngine instance used to remember translators across requests.
public
setCacher(CacheInterface|CacheEngineInterface $cacher) : void
Parameters
- $cacher : CacheInterface|CacheEngineInterface
-
The cacher instance.
setFallbackPackage()
Set fallback domain for package.
public
setFallbackPackage(string $name, Package $package) : Package
Parameters
- $name : string
-
The name of the package.
- $package : Package
-
Package instance
Return values
PackagesetLoaderFallback()
Set domain fallback for loader.
public
setLoaderFallback(string $name, callable $loader) : callable
Parameters
- $name : string
-
The name of the loader domain
- $loader : callable
-
invokable loader
Return values
callable —loader
setLocale()
Sets the default locale code.
public
setLocale(string $locale) : void
Parameters
- $locale : string
-
The new locale code.
useFallback()
Set if the default domain fallback is used.
public
useFallback([bool $enable = true ]) : void
Parameters
- $enable : bool = true
-
flag to enable or disable fallback
_getTranslator()
Gets a translator from the registry by package for a locale.
protected
_getTranslator(string $name, string $locale) : Translator
Parameters
- $name : string
-
The translator package to retrieve.
- $locale : string
-
The locale to use; if empty, uses the default locale.
Return values
Translator —A translator object.
createInstance()
Create translator instance.
protected
createInstance(string $name, string $locale) : Translator
Parameters
- $name : string
-
The translator package to retrieve.
- $locale : string
-
The locale to use; if empty, uses the default locale.
Return values
Translator —A translator object.