Completed
Push — master ( e04a88...42a5e8 )
by Craig
07:17
created

GettextExtension::_np()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 6
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreBundle\Twig\Extension;
13
14
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
15
use Zikula\Common\Translator\TranslatorInterface;
16
17
/**
18
 * GettextExtension class.
19
 */
20
class GettextExtension extends \Twig_Extension
21
{
22
    /**
23
     * @var TranslatorInterface
24
     */
25
    private $translator;
26
27
    /**
28
     * @var ZikulaHttpKernelInterface
29
     */
30
    private $kernel;
31
32
    /**
33
     * GettextExtension constructor.
34
     * @param TranslatorInterface $translator
35
     * @param ZikulaHttpKernelInterface $kernel
36
     */
37
    public function __construct(TranslatorInterface $translator, ZikulaHttpKernelInterface $kernel)
38
    {
39
        $this->translator = $translator;
40
        $this->kernel = $kernel;
41
    }
42
43
    /**
44
     * Returns a list of functions to add to the existing list.
45
     *
46
     * @return array An array of functions
47
     */
48
    public function getFunctions()
49
    {
50
        return [
51
            new \Twig_SimpleFunction('__', [$this, '__'], ['needs_context' => true]),
52
            new \Twig_SimpleFunction('_n', [$this, '_n'], ['needs_context' => true]),
53
            new \Twig_SimpleFunction('__f', [$this, '__f'], ['needs_context' => true]),
54
            new \Twig_SimpleFunction('_fn', [$this, '_fn'], ['needs_context' => true]),
55
        ];
56
    }
57
58
    /**
59
     * @see __()
60
     */
61
    public function __(array $context, $message, $domain = null, $locale = null)
62
    {
63
        $domain = isset($domain) ? $domain : $this->determineTranslationDomainFromContext($context);
64
65
        return $this->translator->__(/** @Ignore */$message, $domain, $locale);
66
    }
67
68
    /**
69
     * @see __f()
70
     */
71
    public function __f(array $context, $message, $params, $domain = null, $locale = null)
72
    {
73
        $domain = isset($domain) ? $domain : $this->determineTranslationDomainFromContext($context);
74
75
        return $this->translator->__f(/** @Ignore */$message, $params, $domain, $locale);
76
    }
77
78
    /**
79
     * @see _n()
80
     */
81
    public function _n(array $context, $singular, $plural, $count, $domain = null, $locale = null)
82
    {
83
        $domain = isset($domain) ? $domain : $this->determineTranslationDomainFromContext($context);
84
85
        return $this->translator->_n(/** @Ignore */$singular, $plural, $count, $domain, $locale);
86
    }
87
88
    /**
89
     * @see _fn()
90
     */
91
    public function _fn(array $context, $singular, $plural, $count, $params, $domain = null, $locale = null)
92
    {
93
        $domain = isset($domain) ? $domain : $this->determineTranslationDomainFromContext($context);
94
95
        return $this->translator->_fn(/** @Ignore */$singular, $plural, $count, $params, $domain, $locale);
96
    }
97
98
    /**
99
     * @param array $context
100
     * @param string $default
101
     * @return string
102
     */
103
    private function determineTranslationDomainFromContext(array $context, $default = 'zikula')
104
    {
105
        if (isset($context['domain'])) {
106
            return $context['domain'];
107
        }
108
        if (isset($context['app'])) {
109
            /** @var \Symfony\Bridge\Twig\AppVariable $app */
110
            $app = $context['app'];
111
            $bundleName = $app->getRequest()->attributes->get('_zkBundle');
112
            if (!empty($bundleName)) {
113
                return $this->kernel->getBundle($bundleName)->getTranslationDomain();
114
            }
115
        }
116
117
        return $default;
118
    }
119
}
120