Localization
Trait Localization.
Embed default and locale translators and translation base methods.
Table of Contents
Properties
- $localTranslator : TranslatorInterface|null
- Specific translator of the current instance.
Methods
- executeWithLocale() : mixed
- Set the current locale to the given, execute the passed function, reset the locale to previous one, then return the result of the closure (or null if the closure was void).
- getAvailableLocales() : array<string|int, mixed>
- Returns the list of internally available locales and already loaded custom locales.
- getAvailableLocalesInfo() : array<string|int, Language>
- Returns list of Language object for each available locale. This object allow you to get the ISO name, native name, region and variant of the locale.
- getFallbackLocale() : string|null
- Get the fallback locale.
- getLocale() : string
- Get the current translator locale.
- getLocalTranslator() : TranslatorInterface
- Get the translator of the current instance or the default if none set.
- getTranslationMessage() : string
- Returns raw translation message for a given key.
- getTranslationMessageWith() : string|Closure|null
- Returns raw translation message for a given key.
- hasLocalTranslator() : bool
- Return true if the current instance has its own translator.
- locale() : $this|string
- Get/set the locale for the current instance.
- localeHasDiffOneDayWords() : bool
- Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow).
- localeHasDiffSyntax() : bool
- Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after).
- localeHasDiffTwoDayWords() : bool
- Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow).
- localeHasPeriodSyntax() : bool
- Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X).
- localeHasShortUnits() : bool
- Returns true if the given locale is internally supported and has short-units support.
- setFallbackLocale() : void
- Set the fallback locale.
- setLocale() : void
- Set the current translator locale and indicate if the source locale file exists.
- setLocalTranslator() : self
- Set the translator for the current instance.
- translate() : string
- Translate using translation string or callback available.
- translateNumber() : string
- Returns the alternative number for a given integer if available in the current locale.
- translateTimeString() : string
- Translate a time string from a locale to an other.
- translateTimeStringTo() : string
- Translate a time string from the current locale (`$date->locale()`) to an other.
- translateWith() : string
- Translate using translation string or callback available.
- getLocaleAwareTranslator() : LocaleAwareInterface|null
- Throw an error if passed object is not LocaleAwareInterface.
- getTranslatorLocale() : string|null
- Get the locale of a given translator.
- cleanWordFromTranslationString() : string
- Return the word cleaned from its translation codes.
- getFromCatalogue() : mixed
- getTranslationArray() : array<string|int, string>
- Get an array of translations based on the current date.
- replaceOrdinalWords() : string
- translateWordsByKeys() : array<string|int, string>
- Translate a list of words.
Properties
$localTranslator
Specific translator of the current instance.
protected
TranslatorInterface|null
$localTranslator
= null
Methods
executeWithLocale()
Set the current locale to the given, execute the passed function, reset the locale to previous one, then return the result of the closure (or null if the closure was void).
public
static executeWithLocale(string $locale, callable $func) : mixed
Parameters
- $locale : string
-
locale ex. en
- $func : callable
getAvailableLocales()
Returns the list of internally available locales and already loaded custom locales.
public
static getAvailableLocales() : array<string|int, mixed>
(It will ignore custom translator dynamic loading.)
Return values
array<string|int, mixed>getAvailableLocalesInfo()
Returns list of Language object for each available locale. This object allow you to get the ISO name, native name, region and variant of the locale.
public
static getAvailableLocalesInfo() : array<string|int, Language>
Return values
array<string|int, Language>getFallbackLocale()
Get the fallback locale.
public
static getFallbackLocale() : string|null
Tags
Return values
string|nullgetLocale()
Get the current translator locale.
public
static getLocale() : string
Return values
stringgetLocalTranslator()
Get the translator of the current instance or the default if none set.
public
getLocalTranslator() : TranslatorInterface
Return values
TranslatorInterfacegetTranslationMessage()
Returns raw translation message for a given key.
public
getTranslationMessage(string $key[, string|null $locale = null ][, string|null $default = null ][, TranslatorInterface $translator = null ]) : string
Parameters
- $key : string
-
key to find
- $locale : string|null = null
-
current locale used if null
- $default : string|null = null
-
default value if translation returns the key
- $translator : TranslatorInterface = null
-
an optional translator to use
Return values
stringgetTranslationMessageWith()
Returns raw translation message for a given key.
public
static getTranslationMessageWith(TranslatorInterface|null $translator, string $key[, string|null $locale = null ][, string|null $default = null ]) : string|Closure|null
Parameters
- $translator : TranslatorInterface|null
-
the translator to use
- $key : string
-
key to find
- $locale : string|null = null
-
current locale used if null
- $default : string|null = null
-
default value if translation returns the key
Return values
string|Closure|nullhasLocalTranslator()
Return true if the current instance has its own translator.
public
hasLocalTranslator() : bool
Return values
boollocale()
Get/set the locale for the current instance.
public
locale([string|null $locale = null ], string ...$fallbackLocales) : $this|string
Parameters
- $locale : string|null = null
- $fallbackLocales : string
Return values
$this|stringlocaleHasDiffOneDayWords()
Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow).
public
static localeHasDiffOneDayWords(string $locale) : bool
Support is considered enabled if the 3 words are translated in the given locale.
Parameters
- $locale : string
-
locale ex. en
Return values
boollocaleHasDiffSyntax()
Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after).
public
static localeHasDiffSyntax(string $locale) : bool
Support is considered enabled if the 4 sentences are translated in the given locale.
Parameters
- $locale : string
-
locale ex. en
Return values
boollocaleHasDiffTwoDayWords()
Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow).
public
static localeHasDiffTwoDayWords(string $locale) : bool
Support is considered enabled if the 2 words are translated in the given locale.
Parameters
- $locale : string
-
locale ex. en
Return values
boollocaleHasPeriodSyntax()
Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X).
public
static localeHasPeriodSyntax(string $locale) : bool
Support is considered enabled if the 4 sentences are translated in the given locale.
Parameters
- $locale : string
-
locale ex. en
Return values
boollocaleHasShortUnits()
Returns true if the given locale is internally supported and has short-units support.
public
static localeHasShortUnits(string $locale) : bool
Support is considered enabled if either year, day or hour has a short variant translated.
Parameters
- $locale : string
-
locale ex. en
Return values
boolsetFallbackLocale()
Set the fallback locale.
public
static setFallbackLocale(string $locale) : void
Parameters
- $locale : string
Tags
setLocale()
Set the current translator locale and indicate if the source locale file exists.
public
static setLocale(string $locale) : void
Pass 'auto' as locale to use the closest language to the current LC_TIME locale.
Parameters
- $locale : string
-
locale ex. en
setLocalTranslator()
Set the translator for the current instance.
public
setLocalTranslator(TranslatorInterface $translator) : self
Parameters
- $translator : TranslatorInterface
Return values
selftranslate()
Translate using translation string or callback available.
public
translate(string $key[, array<string|int, mixed> $parameters = [] ][, string|int|float|null $number = null ][, TranslatorInterface|null $translator = null ][, bool $altNumbers = false ]) : string
Parameters
- $key : string
-
key to find
- $parameters : array<string|int, mixed> = []
-
replacement parameters
- $number : string|int|float|null = null
-
number if plural
- $translator : TranslatorInterface|null = null
-
an optional translator to use
- $altNumbers : bool = false
-
pass true to use alternative numbers
Return values
stringtranslateNumber()
Returns the alternative number for a given integer if available in the current locale.
public
translateNumber(int $number) : string
Parameters
- $number : int
Return values
stringtranslateTimeString()
Translate a time string from a locale to an other.
public
static translateTimeString(string $timeString[, string|null $from = null ][, string|null $to = null ][, int $mode = CarbonInterface::TRANSLATE_ALL ]) : string
Parameters
- $timeString : string
-
date/time/duration string to translate (may also contain English)
- $from : string|null = null
-
input locale of the $timeString parameter (
Carbon::getLocale()
by default) - $to : string|null = null
-
output locale of the result returned (
"en"
by default) - $mode : int = CarbonInterface::TRANSLATE_ALL
-
specify what to translate with options:
- CarbonInterface::TRANSLATE_ALL (default)
- CarbonInterface::TRANSLATE_MONTHS
- CarbonInterface::TRANSLATE_DAYS
- CarbonInterface::TRANSLATE_UNITS
- CarbonInterface::TRANSLATE_MERIDIEM You can use pipe to group: CarbonInterface::TRANSLATE_MONTHS | CarbonInterface::TRANSLATE_DAYS
Return values
stringtranslateTimeStringTo()
Translate a time string from the current locale (`$date->locale()`) to an other.
public
translateTimeStringTo(string $timeString[, string|null $to = null ]) : string
Parameters
- $timeString : string
-
time string to translate
- $to : string|null = null
-
output locale of the result returned ("en" by default)
Return values
stringtranslateWith()
Translate using translation string or callback available.
public
static translateWith(TranslatorInterface $translator, string $key[, array<string|int, mixed> $parameters = [] ][, int|float|null $number = null ]) : string
Parameters
- $translator : TranslatorInterface
-
an optional translator to use
- $key : string
-
key to find
- $parameters : array<string|int, mixed> = []
-
replacement parameters
- $number : int|float|null = null
-
number if plural
Return values
stringgetLocaleAwareTranslator()
Throw an error if passed object is not LocaleAwareInterface.
protected
static getLocaleAwareTranslator([LocaleAwareInterface|null $translator = null ]) : LocaleAwareInterface|null
Parameters
- $translator : LocaleAwareInterface|null = null
Return values
LocaleAwareInterface|nullgetTranslatorLocale()
Get the locale of a given translator.
protected
getTranslatorLocale([mixed $translator = null ]) : string|null
If null or omitted, current local translator is used. If no local translator is in use, current global translator is used.
Parameters
- $translator : mixed = null
Return values
string|nullcleanWordFromTranslationString()
Return the word cleaned from its translation codes.
private
static cleanWordFromTranslationString(string $word) : string
Parameters
- $word : string
Return values
stringgetFromCatalogue()
private
static getFromCatalogue(mixed $translator, MessageCatalogueInterface $catalogue, string $id[, string $domain = 'messages' ]) : mixed
Parameters
- $translator : mixed
- $catalogue : MessageCatalogueInterface
- $id : string
- $domain : string = 'messages'
getTranslationArray()
Get an array of translations based on the current date.
private
static getTranslationArray(callable $translation, int $length, string $timeString) : array<string|int, string>
Parameters
- $translation : callable
- $length : int
- $timeString : string
Return values
array<string|int, string>replaceOrdinalWords()
private
static replaceOrdinalWords(string $timeString, array<string|int, mixed> $ordinalWords) : string
Parameters
- $timeString : string
- $ordinalWords : array<string|int, mixed>
Return values
stringtranslateWordsByKeys()
Translate a list of words.
private
static translateWordsByKeys(array<string|int, string> $keys, array<string|int, string> $messages, string $key) : array<string|int, string>
Parameters
- $keys : array<string|int, string>
-
keys to translate.
- $messages : array<string|int, string>
-
messages bag handling translations.
- $key : string
-
'to' (to get the translation) or 'from' (to get the detection RegExp pattern).