/
TranslationExtension.php
151 lines (137 loc) · 4.75 KB
/
TranslationExtension.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
<?php
/**
* This file is part of NoiseLabs-SmartyBundle
*
* NoiseLabs-SmartyBundle is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* NoiseLabs-SmartyBundle is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with NoiseLabs-SmartyBundle; if not, see
* <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011-2016 Vítor Brandão
*
* @category NoiseLabs
* @package SmartyBundle
* @copyright (C) 2011-2016 Vítor Brandão <vitor@noiselabs.org>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL-3
* @link http://www.noiselabs.org
*/
namespace NoiseLabs\Bundle\SmartyBundle\Extension;
use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\BlockPlugin;
use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\ModifierPlugin;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Provides integration of the Translation component with Smarty[Bundle].
*
* @author Vítor Brandão <vitor@noiselabs.org>
*/
class TranslationExtension extends AbstractExtension
{
protected $translator;
/**
* Constructor.
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* Return the translator instance.
*/
public function getTranslator()
{
return $this->translator;
}
/**
* {@inheritdoc}
*/
public function getPlugins()
{
return array(
new BlockPlugin('trans', $this, 'transBlock'),
new ModifierPlugin('trans', $this, 'transModifier'),
new BlockPlugin('transchoice', $this, 'transchoiceBlock'),
new ModifierPlugin('transchoice', $this, 'transchoiceModifier')
);
}
/**
* Block plugin for 'trans'.
*
* @see TranslatorInterface::trans()
*
* @param array $params Parameters to pass to the translator
* @param string $message Message to translate
*/
public function transBlock(array $params = array(), $message = null, \Smarty_Internal_Template $template, &$repeat)
{
// only output on the closing tag
if (!$repeat && isset($message)) {
$params = array_merge(array(
'vars' => array(),
'domain' => 'messages',
'locale' => null,
), $params);
return $this->translator->trans($message, $params['vars'], $params['domain'], $params['locale']);
}
}
/**
* Modifier plugin for 'trans'.
*
* @see TranslatorInterface::trans()
*
* Usage in template context:
* <code>
* {"text to be translated"|trans}
* </code>
*/
public function transModifier($message, array $parameters = array(), $domain = 'messages', $locale = null)
{
return $this->translator->trans($message, $parameters, $domain, $locale);
}
/**
* Block plugin for 'transchoice'.
*
* @param string $message Message to translate
*/
public function transchoiceBlock(array $params = array(), $message = null, \Smarty_Internal_Template $template, &$repeat)
{
// only output on the closing tag
if (!$repeat && isset($message)) {
$params = array_merge(array(
'count' => null,
'vars' => array(),
'domain' => 'messages',
'locale' => null,
), $params);
// Replace [123] with {123}
if ($template->smarty->left_delimiter == '{' || $template->smarty->right_delimiter == '}') {
$message = preg_replace("/\[([0-9]*)\] (.*?)/i", '{$1} $2', $message);
}
return $this->translator->transchoice($message, $params['count'], array_merge(array('%count%' => $params['count']), $params['vars']), $params['domain'], $params['locale']);
}
}
/**
* Modifier plugin for 'transchoice'.
*/
public function transchoiceModifier($message, $count, array $parameters = array(), $domain = 'messages', $locale = null)
{
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $parameters), $domain, $locale);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'translator';
}
}