TranslatorFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 18
c 1
b 0
f 0
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createTranslator() 0 16 2
A addPackageTranslations() 0 11 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
}