PotentiallyTranslatedString.php
TLDR
This file defines the PotentiallyTranslatedString
class in the Illuminate\Translation
namespace. This class represents a string that may be translated and provides methods for translating the string and retrieving the original or translated versions.
Classes
PotentiallyTranslatedString
The PotentiallyTranslatedString
class represents a string that may be translated. It has the following properties:
-
$string
: The original string that may be translated. -
$translation
: The translated string, ornull
if no translation has been performed. -
$translator
: An instance of theIlluminate\Contracts\Translation\Translator
interface that may perform the translation.
The class provides the following methods:
-
__construct($string, $translator)
: Creates a newPotentiallyTranslatedString
instance with the specified original string and translator. -
translate($replace = [], $locale = null)
: Translates the string using the translator and optional replacement values and locale. Returns$this
for method chaining. -
translateChoice($number, array $replace = [], $locale = null)
: Translates the string based on a count using the translator and optional replacement values and locale. Returns$this
for method chaining. -
original()
: Returns the original string. -
__toString()
: Returns the potentially translated string. If a translation exists, it is returned; otherwise, the original string is returned. -
toString()
: Alias for__toString()
.
<?php
namespace Illuminate\Translation;
use Stringable;
class PotentiallyTranslatedString implements Stringable
{
/**
* The string that may be translated.
*
* @var string
*/
protected $string;
/**
* The translated string.
*
* @var string|null
*/
protected $translation;
/**
* The validator that may perform the translation.
*
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;
/**
* Create a new potentially translated string.
*
* @param string $string
* @param \Illuminate\Contracts\Translation\Translator $translator
*/
public function __construct($string, $translator)
{
$this->string = $string;
$this->translator = $translator;
}
/**
* Translate the string.
*
* @param array $replace
* @param string|null $locale
* @return $this
*/
public function translate($replace = [], $locale = null)
{
$this->translation = $this->translator->get($this->string, $replace, $locale);
return $this;
}
/**
* Translates the string based on a count.
*
* @param \Countable|int|array $number
* @param array $replace
* @param string|null $locale
* @return $this
*/
public function translateChoice($number, array $replace = [], $locale = null)
{
$this->translation = $this->translator->choice($this->string, $number, $replace, $locale);
return $this;
}
/**
* Get the original string.
*
* @return string
*/
public function original()
{
return $this->string;
}
/**
* Get the potentially translated string.
*
* @return string
*/
public function __toString()
{
return $this->translation ?? $this->string;
}
/**
* Get the potentially translated string.
*
* @return string
*/
public function toString()
{
return (string) $this;
}
}