|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2018 SURFnet B.V. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Twig\Extensions\Extension; |
|
20
|
|
|
|
|
21
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
22
|
|
|
use Twig\Extension\AbstractExtension; |
|
23
|
|
|
use Twig\TwigFilter; |
|
24
|
|
|
|
|
25
|
|
|
final class RemoteVetting extends AbstractExtension |
|
26
|
|
|
{ |
|
27
|
|
|
private const TRANSLATION_TYPE_SCHACHOME = 'schachome'; |
|
28
|
|
|
private const TRANSLATION_TYPE_ATTRIBUTES = 'attributes'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $translationFile; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var array|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $translations = null; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $translationFile) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->translationFile = $translationFile; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getFilters() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
new TwigFilter('trans_rv_attribute', [$this, 'translateAttributeKey']), |
|
48
|
|
|
new TwigFilter('trans_rv_schachome', [$this, 'translateSchachome']), |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function translateAttributeKey($attributeName, $locale) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return $this->getTranslation(self::TRANSLATION_TYPE_ATTRIBUTES, $locale, $attributeName); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
public function translateSchachome($schachome, $locale) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getTranslation(self::TRANSLATION_TYPE_SCHACHOME, $locale, $schachome); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getTranslation(string $type, string $locale, string $key) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$this->loadTranslationCache(); |
|
66
|
|
|
|
|
67
|
|
|
if (!array_key_exists($type, $this->translations) || |
|
68
|
|
|
!array_key_exists($key, $this->translations[$type]) || |
|
69
|
|
|
!array_key_exists($locale, $this->translations[$type][$key]) |
|
70
|
|
|
) { |
|
71
|
|
|
return $key; |
|
72
|
|
|
} |
|
73
|
|
|
return $this->translations[$type][$key][$locale]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function loadTranslationCache() |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->translations === null) { |
|
79
|
|
|
$content = file_get_contents($this->translationFile); |
|
80
|
|
|
$this->translations = json_decode($content, true); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.