Completed
Push — dev-master ( 3f66ae...92266b )
by Derek Stephen
01:52
created

TranslatorFactory::addPackageTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.8666
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Bone\Service;
4
5
use Bone\I18n\I18nRegistrationInterface;
6
use Locale;
7
use Zend\I18n\Translator\Loader\Gettext;
8
use Zend\I18n\Translator\Translator;
9
10
class TranslatorFactory
11
{
12
    /**
13
     * @param array $config
14
     * @return Translator
15
     */
16 13
    public function createTranslator(array $config, $domain = 'default')
17
    {
18 13
        $translator = new Translator();
19
20 13
        foreach ($config['supported_locales'] as $locale) {
21 13
            $file = $config['translations_dir'] . '/' . $locale .  '/' . $locale . '.mo';
22 13
            $translator->addTranslationFile(
23 13
                $config['type'],
24 13
                $file,
25 13
                $domain,
26 13
                $locale
27
            );
28
        }
29 13
        $translator->setLocale(Locale::getDefault());
30
31 13
        return $translator;
32
    }
33
34
    /**
35
     * @param Translator $translator
36
     * @param I18nRegistrationInterface $package
37
     * @param string $locale
38
     * @return Translator
39
     */
40
    public function addPackageTranslations(Translator $translator, I18nRegistrationInterface $package, string $locale)
41
    {
42
        $dir = $package->getTranslationsDirectory();
43
        $translator->addTranslationFile(
44
            Gettext::class,
45
            $dir . '/' . $locale . '/' . $locale . '.mo',
46
            'user',
47
            $locale
48
        );
49
50
        return $translator;
51
    }
52
}