I18n::parseTranslations()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 18
rs 9.2222
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package TYPO3
7
 */
8
9
10
namespace Aimeos\Aimeos\Base;
11
12
13
/**
14
 * Aimeos translation class
15
 *
16
 * @package TYPO3
17
 */
18
class I18n
19
{
20
    private static $i18n = [];
21
22
23
    /**
24
     * Creates new translation objects.
25
     *
26
     * @param array $i18nPaths Paths to the translation directories
27
     * @param array $langIds List of two letter ISO language IDs
28
     * @param array $local List of local translation entries overwriting the standard ones
29
     * @return array List of translation objects implementing \Aimeos\Base\Translation\Iface
30
     */
31
    public static function get(array $i18nPaths, array $languageIds, array $local = []) : array
32
    {
33
        $i18nList = [];
34
35
        foreach ($languageIds as $langid) {
36
            if (in_array($langid, ['', 'default'])) { continue; }
37
38
            if (!isset(self::$i18n[$langid])) {
39
                $i18n = new \Aimeos\Base\Translation\Gettext($i18nPaths, $langid);
40
41
                if ((bool) \Aimeos\Aimeos\Base::getExtConfig('useAPC', false) === true) {
42
                    $i18n = new \Aimeos\Base\Translation\Decorator\APC($i18n, \Aimeos\Aimeos\Base::getExtConfig('apcPrefix', 't3:'));
43
                }
44
45
                self::$i18n[$langid] = $i18n;
46
            }
47
48
            $i18nList[$langid] = self::$i18n[$langid];
49
50
            if (isset($local[$langid])) {
51
                $translations = self::parseTranslations((array) $local[$langid]);
52
                $i18nList[$langid] = new \Aimeos\Base\Translation\Decorator\Memory($i18nList[$langid], $translations);
53
            }
54
        }
55
56
        return $i18nList;
57
    }
58
59
60
    /**
61
     * Parses TypoScript configuration string.
62
     *
63
     * @param array $entries User-defined translation entries via TypoScript
64
     * @return array Associative list of translation domain and original string / list of tranlations
65
     */
66
    protected static function parseTranslations(array $entries) : array
67
    {
68
        $translations = [];
69
70
        foreach ($entries as $entry) {
71
            if (isset($entry['domain']) && isset($entry['string']) && isset($entry['trans'])) {
72
                $string = str_replace(['\\n', '\\'], ["\n", ''], $entry['string']);
73
                $trans = [];
74
75
                foreach ((array) $entry['trans'] as $str) {
76
                    $trans[] = str_replace(['\\n', '\\'], ["\n", ''], $str);
77
                }
78
79
                $translations[$entry['domain']][$string] = $trans;
80
            }
81
        }
82
83
        return $translations;
84
    }
85
}
86