1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace steevanb\DevBundle\Translation; |
4
|
|
|
|
5
|
|
|
use steevanb\DevBundle\Exception\TranslationNotFoundException; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator; |
7
|
|
|
use Symfony\Component\Config\ConfigCacheInterface; |
8
|
|
|
|
9
|
|
|
class Translator extends BaseTranslator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string $id |
13
|
|
|
* @param string $locale |
14
|
|
|
* @param string $domain |
15
|
|
|
* @throws \Exception |
16
|
|
|
*/ |
17
|
|
|
protected function assertTranslationExists($id, $locale, $domain = 'messages') |
18
|
|
|
{ |
19
|
|
|
$allowFallbacks = $this->container->getParameter('translator.allow_fallbacks'); |
20
|
|
|
if ($this->getCatalogue($locale)->has($id, $domain, $allowFallbacks) === false) { |
21
|
|
|
$locale = ($locale === null) ? $this->getLocale() : $locale; |
22
|
|
|
throw new TranslationNotFoundException($id, $locale, $domain); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string|null $locale |
28
|
|
|
* @return MessageCatalogue |
29
|
|
|
*/ |
30
|
|
|
public function getCatalogue($locale = null) |
31
|
|
|
{ |
32
|
|
|
return parent::getCatalogue($locale); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $locale |
37
|
|
|
* @param ConfigCacheInterface $cache |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function dumpCatalogue($locale, ConfigCacheInterface $cache) |
41
|
|
|
{ |
42
|
|
|
parent::dumpCatalogue($locale, $cache); |
|
|
|
|
43
|
|
|
|
44
|
|
|
// now, change MessageCatalogue namespace in cache |
45
|
|
|
$content = file_get_contents($cache->getPath()); |
46
|
|
|
$use = 'use Symfony\\Component\\Translation\\MessageCatalogue;'; |
47
|
|
|
if (strpos($content, $use) !== 7) { |
48
|
|
|
throw new \Exception('Invalid MessageCatalogue cache format in file "' . $cache->getPath() . '".'); |
49
|
|
|
} |
50
|
|
|
$newUse = 'use steevanb\\DevBundle\\Translation\\MessageCatalogue;'; |
51
|
|
|
$newContent = substr_replace($content, $newUse, 7, strlen($use)); |
52
|
|
|
$cache->write($newContent, $this->catalogues[$locale]->getResources()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $id |
57
|
|
|
* @param array $parameters |
58
|
|
|
* @param string $domain |
59
|
|
|
* @param string $locale |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function trans($id, array $parameters = array(), $domain = null, $locale = null) |
63
|
|
|
{ |
64
|
|
|
$this->assertTranslationExists($id, $locale, $domain); |
65
|
|
|
|
66
|
|
|
return parent::trans($id, $parameters, $domain, $locale); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $id |
71
|
|
|
* @param int $number |
72
|
|
|
* @param array $parameters |
73
|
|
|
* @param string $domain |
74
|
|
|
* @param string $locale |
75
|
|
|
* @return string |
76
|
|
|
* @throws \Exception |
77
|
|
|
*/ |
78
|
|
|
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) |
79
|
|
|
{ |
80
|
|
|
$this->assertTranslationExists($id, $locale, $domain); |
81
|
|
|
|
82
|
|
|
return parent::transChoice($id, $number, $parameters, $domain, $locale); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.