-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Translator.php
172 lines (143 loc) · 4.47 KB
/
Translator.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\CoreBundle\Translation;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\System;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface as ContractsTranslatorInterface;
class Translator implements TranslatorInterface, ContractsTranslatorInterface, TranslatorBagInterface
{
// Reserved translation domains for the Contao bundles
private const CONTAO_BUNDLES = [
'contao_calendar',
'contao_comments',
'contao_faq',
'contao_installation',
'contao_listing',
'contao_manager',
'contao_news',
'contao_newsletter',
];
/**
* @var TranslatorInterface|TranslatorBagInterface
*/
private $translator;
/**
* @var ContaoFramework
*/
private $framework;
public function __construct(TranslatorInterface $translator, ContaoFramework $framework)
{
$this->translator = $translator;
$this->framework = $framework;
}
/**
* {@inheritdoc}
*
* Gets the translation from Contao’s $GLOBALS['TL_LANG'] array if the message
* domain starts with "contao_". The locale parameter is ignored in this case.
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null): string
{
// Forward to the default translator
if (null === $domain || 0 !== strncmp($domain, 'contao_', 7) || \in_array($domain, self::CONTAO_BUNDLES, true)) {
return $this->translator->trans($id, $parameters, $domain, $locale);
}
if (null === $locale) {
$locale = $this->translator->getLocale();
}
$this->framework->initialize();
$this->loadLanguageFile(substr($domain, 7), $locale);
$translated = $this->getFromGlobals($id);
if (null === $translated) {
return $id;
}
if (!empty($parameters)) {
$translated = vsprintf($translated, $parameters);
}
return $translated;
}
/**
* {@inheritdoc}
*/
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null): string
{
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
/**
* {@inheritdoc}
*/
public function setLocale($locale): ?string
{
return $this->translator->setLocale($locale);
}
/**
* {@inheritdoc}
*/
public function getLocale(): string
{
return $this->translator->getLocale();
}
/**
* {@inheritdoc}
*/
public function getCatalogue($locale = null): MessageCatalogueInterface
{
return $this->translator->getCatalogue($locale);
}
/**
* Returns the collected messages of the decorated translator.
*/
public function getCollectedMessages(): array
{
if (method_exists($this->translator, 'getCollectedMessages')) {
return $this->translator->getCollectedMessages();
}
return [];
}
/**
* Returns the fallback locales of the decorated translator.
*/
public function getFallbackLocales(): array
{
if (method_exists($this->translator, 'getFallbackLocales')) {
return $this->translator->getFallbackLocales();
}
return [];
}
/**
* Returns the labels from $GLOBALS['TL_LANG'] based on a message ID like "MSC.view".
*/
private function getFromGlobals(string $id): ?string
{
// Split the ID into chunks allowing escaped dots (\.) and backslashes (\\)
preg_match_all('/(?:\\\\[\\\\.]|[^.])++/', $id, $matches);
$parts = preg_replace('/\\\\([\\\\.])/', '$1', $matches[0]);
$item = &$GLOBALS['TL_LANG'];
foreach ($parts as $part) {
if (!isset($item[$part])) {
return null;
}
$item = &$item[$part];
}
return $item;
}
/**
* Loads a Contao framework language file.
*/
private function loadLanguageFile(string $name, string $locale = null): void
{
/** @var System $system */
$system = $this->framework->getAdapter(System::class);
$system->loadLanguageFile($name, $locale);
}
}