Translator
extends Translator
in package
Table of Contents
- $catalogues : array<string|int, MessageCatalogueInterface>
- $messages : array<string|int, mixed>
- List of custom localized messages.
- $singleton : static
- Singleton for Translator.
- $cacheDir : string
- $configCacheFactory : ConfigCacheFactoryInterface|null
- $debug : bool
- $fallbackLocales : array<string|int, mixed>
- $formatter : MessageFormatterInterface
- $loaders : array<string|int, LoaderInterface>
- $locale : string
- $resources : array<string|int, mixed>
- __construct() : mixed
- addLoader() : mixed
- Adds a Loader.
- addResource() : mixed
- Adds a Resource.
- get() : static
- Return a singleton instance of Translator.
- getCatalogue() : MessageCatalogueInterface
- Gets the catalogue by locale.
- getFallbackLocales() : array<string|int, mixed>
- Gets the fallback locales.
- getLocale() : string
- Returns the current locale.
- getMessages() : array<string|int, mixed>
- Get messages of a locale, if none given, return all the languages.
- resetMessages() : bool
- Reset messages of a locale (all locale if no locale passed).
- setConfigCacheFactory() : mixed
- 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.
- trans() : string
- Translates the given message.
- 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.
- doLoadCatalogue() : mixed
- dumpCatalogue() : mixed
- getCatalogueCachePath() : mixed
- getConfigCacheFactory() : ConfigCacheFactoryInterface
- Provides the ConfigCache factory implementation, falling back to a default implementation if necessary.
- getFallbackContent() : mixed
- initializeCacheCatalogue() : mixed
- loadFallbackCatalogues() : mixed
Properties
$catalogues
protected
array<string|int, MessageCatalogueInterface>
$catalogues
= []
$messages
List of custom localized messages.
protected
static array<string|int, mixed>
$messages
= array()
$singleton
Singleton for Translator.
protected
static static
$singleton
$cacheDir
private
string
$cacheDir
$configCacheFactory
private
ConfigCacheFactoryInterface|null
$configCacheFactory
$debug
private
bool
$debug
$fallbackLocales
private
array<string|int, mixed>
$fallbackLocales
= []
$formatter
private
MessageFormatterInterface
$formatter
$loaders
private
array<string|int, LoaderInterface>
$loaders
= []
$locale
private
string
$locale
$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
-
The locale
- $formatter : MessageFormatterInterface = null
-
The message formatter
- $cacheDir : mixed = null
-
The directory to use for the cache
- $debug : mixed = false
-
Use cache in debug mode ?
Return values
mixed —addLoader()
Adds a Loader.
public
addLoader(string $format, LoaderInterface $loader) : mixed
Parameters
- $format : string
-
The name of the loader (@see addResource())
- $loader : LoaderInterface
-
A LoaderInterface instance
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 —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 —getFallbackLocales()
Gets the fallback locales.
public
getFallbackLocales() : array<string|int, mixed>
Return values
array<string|int, mixed> —The fallback locales
getLocale()
Returns the current locale.
public
getLocale() : string
Return values
string —The locale
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> —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 —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 —trans()
Translates the given message.
public
trans(mixed $id[, 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)
- $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
Return values
string —The translated 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
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 —doLoadCatalogue()
private
doLoadCatalogue(mixed $locale) : mixed
Parameters
- $locale : mixed
Return values
mixed —dumpCatalogue()
private
dumpCatalogue(mixed $locale, ConfigCacheInterface $cache) : mixed
Parameters
- $locale : mixed
- $cache : ConfigCacheInterface
Return values
mixed —getCatalogueCachePath()
private
getCatalogueCachePath(mixed $locale) : mixed
Parameters
- $locale : mixed
Return values
mixed —getConfigCacheFactory()
Provides the ConfigCache factory implementation, falling back to a default implementation if necessary.
private
getConfigCacheFactory() : ConfigCacheFactoryInterface
Return values
ConfigCacheFactoryInterface —$configCacheFactory
getFallbackContent()
private
getFallbackContent(MessageCatalogue $catalogue) : mixed
Parameters
- $catalogue : MessageCatalogue
Return values
mixed —initializeCacheCatalogue()
private
initializeCacheCatalogue(string $locale) : mixed
Parameters
- $locale : string
Return values
mixed —loadFallbackCatalogues()
private
loadFallbackCatalogues(mixed $locale) : mixed
Parameters
- $locale : mixed