|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Translatable\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Twig_Extension; |
|
6
|
|
|
use Twig_SimpleFilter; |
|
7
|
|
|
|
|
8
|
|
|
class TranslationExtension extends Twig_Extension |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
protected $em; |
|
|
|
|
|
|
11
|
|
|
protected $request; |
|
12
|
|
|
protected $defaultLocale; |
|
13
|
|
|
protected $short_functions; |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
public function __construct($em, $request_stack, $default, $container) |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
$this->em = $em; |
|
18
|
|
|
$this->request = $request_stack->getCurrentRequest(); |
|
19
|
|
|
$this->defaultLocale = $default; |
|
20
|
|
|
$this->short_functions = $container->hasParameter('sludio_helper.script.short_functions') && $container->getParameter('sludio_helper.script.short_functions', false); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getName() |
|
24
|
|
|
{ |
|
25
|
|
|
return 'sludio_helper.twig.translate_extension'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
View Code Duplication |
public function getFilters() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$array = [ |
|
31
|
|
|
new Twig_SimpleFilter('sludio_var', [ |
|
32
|
|
|
$this, |
|
33
|
|
|
'getVar', |
|
34
|
|
|
]), |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
$short_array = [ |
|
38
|
|
|
new Twig_SimpleFilter('var', [ |
|
39
|
|
|
$this, |
|
40
|
|
|
'getVar', |
|
41
|
|
|
]), |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
if ($this->short_functions) { |
|
45
|
|
|
return array_merge($array, $short_array); |
|
46
|
|
|
} else { |
|
47
|
|
|
return $array; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getVar($type, $object, $original = false, $locale = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($object && is_object($object)) { |
|
54
|
|
|
$hl = $this->request ? $this->request->cookies->get('hl') : $this->defaultLocale; |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$new_locale = $locale; |
|
57
|
|
|
if (!$locale) { |
|
58
|
|
|
$new_locale = $this->request ? $this->request->get('_locale') : $hl; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$trans = $object->getVariableByLocale($type, $new_locale, $original); |
|
62
|
|
|
$class = get_class($object); |
|
63
|
|
|
$class = str_replace('Proxies\__CG__\\', '', $class); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
return $trans; |
|
66
|
|
|
} else { |
|
67
|
|
|
return $type; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.