Completed
Push — odev ( a25a56...21e788 )
by De Cramer
02:29
created

Translations::getTranslations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use Symfony\Component\Translation\Translator;
6
7
8
/**
9
 * Class Translations
10
 *
11
 * @package eXpansion\Framework\Core\Services;
12
 * @author oliver de Cramer <[email protected]>
13
 */
14
class Translations
15
{
16
    /** @var Translator */
17
    protected $translator;
18
19
    /** @var string[] List of supported locales. */
20
    protected $supportedLocales;
21
22
    /**
23
     * Translations constructor.
24
     *
25
     * @param $translator
26
     * @param array $supportedLocales
27
     */
28 8
    public function __construct($translator, array $supportedLocales)
29
    {
30 8
        $this->translator = $translator;
31 8
        $this->supportedLocales = $supportedLocales;
32 8
    }
33
34
    /**
35
     * Get translated message.
36
     *
37
     * @param string $id
38
     * @param array $parameters
39
     * @param $locale
40
     *
41
     * @return mixed
42
     */
43 4
    public function getTranslation($id, $parameters = [], $locale = null)
44
    {
45 4
        return $this->translator->trans($id, $parameters, null, $locale);
46
    }
47
48
    /**
49
     * Get list of translations.
50
     *
51
     * @TODO optimize by preparing the messages before.
52
     *
53
     * @param $id
54
     * @param $parameters
55
     *
56
     * @return array
57
     */
58 2
    public function getTranslations($id, $parameters)
59
    {
60 2
        $messages = [];
61
62 2
        foreach ($this->supportedLocales as $locale)
63
        {
64 2
            $message = $this->getTranslation($id, $parameters, $locale);
65 2
            $messages[] = array(
66 2
                "Lang" => lcfirst($locale),
67 2
                "Text" => $message,
68
            );
69
        }
70
71 2
        return $messages;
72
    }
73
}