-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathMessageTranslator.php
More file actions
90 lines (78 loc) · 1.98 KB
/
Copy pathMessageTranslator.php
File metadata and controls
90 lines (78 loc) · 1.98 KB
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
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 27/01/2018
* Time: 20:50.
*/
declare(strict_types=1);
namespace Greenter\Validator;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class MessageTranslator.
*/
class MessageTranslator implements TranslatorInterface
{
/**
* @var ErrorCodeProviderInterface
*/
private $provider;
/**
* @var array
*/
private $items = [];
/**
* MessageTranslator constructor.
*
* @param ErrorCodeProviderInterface $provider
*/
public function __construct(ErrorCodeProviderInterface $provider)
{
$this->provider = $provider;
}
/**
* Add extra resources.
*
* @param array $items
*/
public function addResource(array $items)
{
$this->items = array_merge($this->items, $items);
}
/**
* Translates the given message.
*
* @param string $id The message id (may also be an object that can be cast to string)
* @param array $parameters An array of parameters for the message
* @param string|null $domain The domain for the message or null to use the default
* @param string|null $locale The locale or null to use the default
*
* @return string The translated string
*
* @throws \InvalidArgumentException If the locale contains invalid characters
*/
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
return $this->getValue($id);
}
/**
* Returns the default locale.
*
* @return string The locale
*/
public function getLocale(): string
{
return "en";
}
private function getValue(string $id)
{
$value = $this->provider->getValue($id);
if ($value) {
return $value;
}
if (isset($this->items[$id])) {
return $this->items[$id];
}
return $id;
}
}