1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oskar van Velden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\Controller; |
8
|
|
|
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
|
|
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerAware; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class TranslationController |
17
|
|
|
* |
18
|
|
|
* @package Zicht\Bundle\FrameworkExtraBundle\Controller |
19
|
|
|
*/ |
20
|
|
|
class TranslationController extends ContainerAware |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Wrapper for the translator-service |
24
|
|
|
* |
25
|
|
|
* The id's and domain are optional. When withheld the controller will return all the translations for the locale and (optional) the domain |
26
|
|
|
* If id's are passed, they should be passed in the query-parameters, ie: |
27
|
|
|
* |
28
|
|
|
* One id: |
29
|
|
|
* /translate/nl/messages?id=eticket.can_not_be_rendered_no_barcode |
30
|
|
|
* |
31
|
|
|
* Multiple id's: |
32
|
|
|
* /translate/nl/messages?id[]=eticket.can_not_be_rendered_no_barcode&id[]=eticket.col1&id[]=eticket.copy_warning&id[]=form_label.form.email |
33
|
|
|
* |
34
|
|
|
* @param Request $request |
35
|
|
|
* @param string $locale |
36
|
|
|
* @param string $domain |
37
|
|
|
* @return JsonResponse |
38
|
|
|
* |
39
|
|
|
* @Route("/translate/{locale}/{domain}") |
40
|
|
|
* @Route("/translate/{locale}") |
41
|
|
|
*/ |
42
|
|
|
public function translateAction(Request $request, $locale, $domain = null) |
43
|
|
|
{ |
44
|
|
|
$queryIds = $request->query->get('id'); |
45
|
|
|
|
46
|
|
|
if ($domain) { |
47
|
|
|
if ($queryIds) { |
48
|
|
|
if (!is_array($queryIds)) { |
49
|
|
|
$queryIds = [$queryIds]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$translations = $this->getTranslationsForDomainAndIds($locale, $domain, $queryIds); |
53
|
|
|
} else { |
54
|
|
|
$translations = $this->getTranslationsForDomain($locale, $domain); |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$translations = $this->getTranslationsForLocale($locale); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new JsonResponse( |
61
|
|
|
$translations |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve all translations for the provided ids (within the provided locale and domain) |
67
|
|
|
* |
68
|
|
|
* @param string $locale |
69
|
|
|
* @param string $domain |
70
|
|
|
* @param array $ids |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
private function getTranslationsForDomainAndIds($locale, $domain, $ids) |
74
|
|
|
{ |
75
|
|
|
/** @var Translator $translator */ |
76
|
|
|
$translator = $this->container->get('translator'); |
77
|
|
|
|
78
|
|
|
$translations = []; |
79
|
|
|
foreach ($ids as $id) { |
80
|
|
|
$translations[$id] = $translator->trans($id, [], $domain, $locale); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $translations; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve all translations for the provided locale and domain |
88
|
|
|
* |
89
|
|
|
* @param string $locale |
90
|
|
|
* @param string $domain |
91
|
|
|
* @return array |
92
|
|
|
* @throws \Exception |
93
|
|
|
*/ |
94
|
|
|
private function getTranslationsForDomain($locale, $domain) |
95
|
|
|
{ |
96
|
|
|
$allMessages = $this->getTranslationsForLocale($locale); |
97
|
|
|
|
98
|
|
|
if (!array_key_exists($domain, $allMessages)) { |
99
|
|
|
throw new \Exception('Domain ' . $domain . ' not found in the translations for locale ' . $locale); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $allMessages[$domain]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieve all translations for the provided locale |
107
|
|
|
* |
108
|
|
|
* @param string $locale |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
private function getTranslationsForLocale($locale) |
112
|
|
|
{ |
113
|
|
|
/** @var Translator $translator */ |
114
|
|
|
$translator = $this->container->get('translator'); |
115
|
|
|
|
116
|
|
|
return $translator->getMessages($locale); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths