Translator
extends Translator
in package
Table of Contents
- $aliases : array<string|int, string>
- List of locales aliases.
- $catalogues : array<string|int, MessageCatalogueInterface>
- $directories : array<string|int, string>
- List of custom directories that contain translation files.
- $initializing : bool
- Set to true while constructing.
- $messages : array<string|int, mixed>
- List of custom localized messages.
- $singletons : array<string|int, mixed>
- Translator singletons for each language.
- $cacheDir : string
- $cacheVary : mixed
- $configCacheFactory : ConfigCacheFactoryInterface|null
- $debug : bool
- $fallbackLocales : array<string|int, mixed>
- $formatter : MessageFormatterInterface
- $hasIntlFormatter : mixed
- $loaders : array<string|int, LoaderInterface>
- $locale : string
- $parentLocales : array<string|int, mixed>|null
- $resources : array<string|int, mixed>
- __construct() : mixed
- __debugInfo() : array<string|int, mixed>
- Show locale on var_dump().
- addDirectory() : $this
- Add a directory to the list translation files are searched in.
- addLoader() : mixed
- Adds a Loader.
- addResource() : mixed
- Adds a Resource.
- get() : static
- Return a singleton instance of Translator.
- getAvailableLocales() : array<string|int, mixed>
- Returns the list of internally available locales and already loaded custom locales.
- getCatalogue() : MessageCatalogueInterface
- Gets the catalogue by locale.
- getDirectories() : array<string|int, mixed>
- Returns the list of directories translation files are searched in.
- getLocale() : string
- Returns the current locale.
- getLocalesFiles() : array<string|int, mixed>
- Returns the list of files matching a given locale prefix (or all if empty).
- getMessages() : array<string|int, mixed>
- Get messages of a locale, if none given, return all the languages.
- removeDirectory() : $this
- Remove a directory from the list translation files are searched in.
- resetMessages() : bool
- Reset messages of a locale (all locale if no locale passed).
- setConfigCacheFactory() : mixed
- setDirectories() : $this
- Set list of directories translation files are searched in.
- setFallbackLocales() : mixed
- Sets the fallback locales.
- setLocale() : bool
- Set the current translator locale and indicate if the source locale file exists
- setMessages() : $this
- Set messages of a locale and take file first if present.
- setTranslations() : $this
- Set messages of the current locale and take file first if present.
- trans() : string
- Returns the translation.
- transChoice() : string
- Translates the given choice message by choosing a translation according to a number.
- assertValidLocale() : mixed
- Asserts that the locale is valid, throws an Exception if not.
- computeFallbackLocales() : mixed
- getLoaders() : array<string|int, mixed>
- Gets the loaders.
- initializeCatalogue() : mixed
- loadCatalogue() : mixed
- loadMessagesFromFile() : bool
- Init messages language from matching file in Lang directory.
- compareChunkLists() : mixed
- dumpCatalogue() : void
- getAllMessages() : array<string|int, mixed>
- getCatalogueCachePath() : string
- getConfigCacheFactory() : ConfigCacheFactoryInterface
- Provides the ConfigCache factory implementation, falling back to a default implementation if necessary.
- getFallbackContent() : string
- initializeCacheCatalogue() : void
- loadFallbackCatalogues() : void
Properties
$aliases
List of locales aliases.
protected
array<string|int, string>
$aliases
= ['me' => 'sr_Latn_ME', 'scr' => 'sh']
$catalogues
protected
array<string|int, MessageCatalogueInterface>
$catalogues
= []
$directories
List of custom directories that contain translation files.
protected
array<string|int, string>
$directories
= []
$initializing
Set to true while constructing.
protected
bool
$initializing
= false
$messages
List of custom localized messages.
protected
array<string|int, mixed>
$messages
= []
$singletons
Translator singletons for each language.
protected
static array<string|int, mixed>
$singletons
= []
$cacheDir
private
string
$cacheDir
$cacheVary
private
mixed
$cacheVary
$configCacheFactory
private
ConfigCacheFactoryInterface|null
$configCacheFactory
$debug
private
bool
$debug
$fallbackLocales
private
array<string|int, mixed>
$fallbackLocales
= []
$formatter
private
MessageFormatterInterface
$formatter
$hasIntlFormatter
private
mixed
$hasIntlFormatter
$loaders
private
array<string|int, LoaderInterface>
$loaders
= []
$locale
private
string
$locale
$parentLocales
private
array<string|int, mixed>|null
$parentLocales
$resources
private
array<string|int, mixed>
$resources
= []
Methods
__construct()
public
__construct(mixed $locale[, MessageFormatterInterface $formatter = null ][, mixed $cacheDir = null ][, mixed $debug = false ]) : mixed
Parameters
- $locale : mixed
- $formatter : MessageFormatterInterface = null
- $cacheDir : mixed = null
- $debug : mixed = false
Return values
mixed —__debugInfo()
Show locale on var_dump().
public
__debugInfo() : array<string|int, mixed>
Return values
array<string|int, mixed> —addDirectory()
Add a directory to the list translation files are searched in.
public
addDirectory(string $directory) : $this
Parameters
- $directory : string
-
new directory
Return values
$this —addLoader()
Adds a Loader.
public
addLoader(string $format, LoaderInterface $loader) : mixed
Parameters
- $format : string
-
The name of the loader (@see addResource())
- $loader : LoaderInterface
Return values
mixed —addResource()
Adds a Resource.
public
addResource(string $format, mixed $resource, string $locale[, string $domain = null ]) : mixed
Parameters
- $format : string
-
The name of the loader (@see addLoader())
- $resource : mixed
-
The resource name
- $locale : string
-
The locale
- $domain : string = null
-
The domain
Tags
Return values
mixed —get()
Return a singleton instance of Translator.
public
static get([string|null $locale = null ]) : static
Parameters
- $locale : string|null = null
-
optional initial locale ("en" - english by default)
Return values
static —getAvailableLocales()
Returns the list of internally available locales and already loaded custom locales.
public
getAvailableLocales([string $prefix = '' ]) : array<string|int, mixed>
(It will ignore custom translator dynamic loading.)
Parameters
- $prefix : string = ''
-
prefix required to filter result
Return values
array<string|int, mixed> —getCatalogue()
Gets the catalogue by locale.
public
getCatalogue([mixed $locale = null ]) : MessageCatalogueInterface
Parameters
- $locale : mixed = null
-
The locale or null to use the default
Return values
MessageCatalogueInterface —getDirectories()
Returns the list of directories translation files are searched in.
public
getDirectories() : array<string|int, mixed>
Return values
array<string|int, mixed> —getLocale()
Returns the current locale.
public
getLocale() : string
Return values
string —The locale
getLocalesFiles()
Returns the list of files matching a given locale prefix (or all if empty).
public
getLocalesFiles([string $prefix = '' ]) : array<string|int, mixed>
Parameters
- $prefix : string = ''
-
prefix required to filter result
Return values
array<string|int, mixed> —getMessages()
Get messages of a locale, if none given, return all the languages.
public
getMessages([string|null $locale = null ]) : array<string|int, mixed>
Parameters
- $locale : string|null = null
Return values
array<string|int, mixed> —removeDirectory()
Remove a directory from the list translation files are searched in.
public
removeDirectory(string $directory) : $this
Parameters
- $directory : string
-
directory path
Return values
$this —resetMessages()
Reset messages of a locale (all locale if no locale passed).
public
resetMessages([string|null $locale = null ]) : bool
Remove custom messages and reload initial messages from matching file in Lang directory.
Parameters
- $locale : string|null = null
Return values
bool —setConfigCacheFactory()
public
setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory) : mixed
Parameters
- $configCacheFactory : ConfigCacheFactoryInterface
Return values
mixed —setDirectories()
Set list of directories translation files are searched in.
public
setDirectories(array<string|int, mixed> $directories) : $this
Parameters
- $directories : array<string|int, mixed>
-
new directories list
Return values
$this —setFallbackLocales()
Sets the fallback locales.
public
setFallbackLocales(array<string|int, mixed> $locales) : mixed
Parameters
- $locales : array<string|int, mixed>
-
The fallback locales
Tags
Return values
mixed —setLocale()
Set the current translator locale and indicate if the source locale file exists
public
setLocale(string $locale) : bool
Parameters
- $locale : string
-
locale ex. en
Return values
bool —setMessages()
Set messages of a locale and take file first if present.
public
setMessages(string $locale, array<string|int, mixed> $messages) : $this
Parameters
- $locale : string
- $messages : array<string|int, mixed>
Return values
$this —setTranslations()
Set messages of the current locale and take file first if present.
public
setTranslations(array<string|int, mixed> $messages) : $this
Parameters
- $messages : array<string|int, mixed>
Return values
$this —trans()
Returns the translation.
public
trans(string $id[, array<string|int, mixed> $parameters = [] ][, string $domain = null ][, string $locale = null ]) : string
Parameters
- $id : string
- $parameters : array<string|int, mixed> = []
- $domain : string = null
- $locale : string = null
Return values
string —transChoice()
Translates the given choice message by choosing a translation according to a number.
public
transChoice(mixed $id, mixed $number[, array<string|int, mixed> $parameters = [] ][, mixed $domain = null ][, mixed $locale = null ]) : string
Parameters
- $id : mixed
-
The message id (may also be an object that can be cast to string)
- $number : mixed
-
The number to use to find the index of the message
- $parameters : array<string|int, mixed> = []
-
An array of parameters for the message
- $domain : mixed = null
-
The domain for the message or null to use the default
- $locale : mixed = null
-
The locale or null to use the default
Tags
Return values
string —The translated string
assertValidLocale()
Asserts that the locale is valid, throws an Exception if not.
protected
assertValidLocale(string $locale) : mixed
Parameters
- $locale : string
-
Locale to tests
Tags
Return values
mixed —computeFallbackLocales()
protected
computeFallbackLocales(mixed $locale) : mixed
Parameters
- $locale : mixed
Return values
mixed —getLoaders()
Gets the loaders.
protected
getLoaders() : array<string|int, mixed>
Return values
array<string|int, mixed> —LoaderInterface[]
initializeCatalogue()
protected
initializeCatalogue(string $locale) : mixed
Parameters
- $locale : string
Return values
mixed —loadCatalogue()
protected
loadCatalogue(string $locale) : mixed
Parameters
- $locale : string
Return values
mixed —loadMessagesFromFile()
Init messages language from matching file in Lang directory.
protected
loadMessagesFromFile(string $locale) : bool
Parameters
- $locale : string
Return values
bool —compareChunkLists()
private
static compareChunkLists(mixed $referenceChunks, mixed $chunks) : mixed
Parameters
- $referenceChunks : mixed
- $chunks : mixed
Return values
mixed —dumpCatalogue()
private
dumpCatalogue(string $locale, ConfigCacheInterface $cache) : void
Parameters
- $locale : string
- $cache : ConfigCacheInterface
Return values
void —getAllMessages()
private
getAllMessages(MessageCatalogueInterface $catalogue) : array<string|int, mixed>
Parameters
- $catalogue : MessageCatalogueInterface
Return values
array<string|int, mixed> —getCatalogueCachePath()
private
getCatalogueCachePath(string $locale) : string
Parameters
- $locale : string
Return values
string —getConfigCacheFactory()
Provides the ConfigCache factory implementation, falling back to a default implementation if necessary.
private
getConfigCacheFactory() : ConfigCacheFactoryInterface
Return values
ConfigCacheFactoryInterface —getFallbackContent()
private
getFallbackContent(MessageCatalogue $catalogue) : string
Parameters
- $catalogue : MessageCatalogue
Return values
string —initializeCacheCatalogue()
private
initializeCacheCatalogue(string $locale) : void
Parameters
- $locale : string
Return values
void —loadFallbackCatalogues()
private
loadFallbackCatalogues(string $locale) : void
Parameters
- $locale : string