|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jenschude <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace JaySDe\HandlebarsBundle\Helper; |
|
7
|
|
|
|
|
8
|
|
|
use LightnCandy\SafeString; |
|
9
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
10
|
|
|
|
|
11
|
|
|
class TranslationHelper implements HelperInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var TranslatorInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $translator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $defaultNamespace; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $interpolationPrefix; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $interpolationSuffix; |
|
32
|
|
|
|
|
33
|
10 |
|
public function __construct( |
|
34
|
|
|
TranslatorInterface $translator = null, |
|
35
|
|
|
$defaultNamespace, |
|
36
|
|
|
$interpolationPrefix, |
|
37
|
|
|
$interpolationSuffix |
|
38
|
|
|
) { |
|
39
|
10 |
|
$this->translator = $translator; |
|
40
|
10 |
|
$this->defaultNamespace = $defaultNamespace; |
|
41
|
10 |
|
$this->interpolationPrefix = $interpolationPrefix; |
|
42
|
10 |
|
$this->interpolationSuffix = $interpolationSuffix; |
|
43
|
10 |
|
} |
|
44
|
|
|
|
|
45
|
10 |
|
public function handle($context, $options) |
|
46
|
|
|
{ |
|
47
|
10 |
|
$options = isset($options['hash']) ? $options['hash'] : []; |
|
48
|
10 |
|
if (strstr($context, ':')) { |
|
49
|
7 |
|
list($bundle, $context) = explode(':', $context, 2); |
|
50
|
7 |
|
$options['bundle'] = $bundle; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
10 |
|
$bundle = $this->getOptionValue($options, 'bundle', $this->defaultNamespace); |
|
54
|
10 |
|
$locale = $this->getOptionValue($options, 'locale'); |
|
55
|
10 |
|
$count = $this->getOptionValue($options, 'count'); |
|
56
|
|
|
|
|
57
|
10 |
|
$args = $this->transformOptions($options); |
|
58
|
|
|
|
|
59
|
10 |
|
return new SafeString($this->trans($context, $args, $count, $bundle, $locale)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
10 |
|
private function trans($context, $args, $count, $bundle, $locale) |
|
63
|
|
|
{ |
|
64
|
10 |
|
if (is_null($count)) { |
|
65
|
7 |
|
$trans = $this->translator->trans($context, $args, $bundle, $locale); |
|
66
|
|
|
} else { |
|
67
|
3 |
|
$trans = $this->translator->transChoice($context, $count, $args, $bundle, $locale); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
return $trans; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
10 |
|
private function transformOptions($options) |
|
74
|
|
|
{ |
|
75
|
10 |
|
$args = []; |
|
76
|
10 |
|
foreach ($options as $key => $value) { |
|
77
|
8 |
|
$key = $this->interpolationPrefix.$key.$this->interpolationSuffix; |
|
78
|
8 |
|
$args[$key] = $value; |
|
79
|
|
|
} |
|
80
|
10 |
|
return $args; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
10 |
|
private function getOptionValue($options, $key, $default = null) |
|
84
|
|
|
{ |
|
85
|
10 |
|
return isset($options[$key]) ? $options[$key] : $default; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|