TranslatorFactory::addPackageTranslations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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