-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPotentiallyTranslatedString.php
101 lines (87 loc) · 2.02 KB
/
PotentiallyTranslatedString.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?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|float|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;
}
}