Passed
Push — dev ( dc6ac3...67fd32 )
by Romain
06:56
created

LocalizationService::doLocalize()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 12
nop 2
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2017
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Service;
18
19
use CuyZ\Notiz\Service\Traits\SelfInstantiateTrait;
20
use CuyZ\Notiz\Support\NotizConstants;
21
use TYPO3\CMS\Core\SingletonInterface;
22
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
23
24
/**
25
 * Wrapper around the TYPO3 localization utility to change the default behaviour
26
 * for the localization.
27
 *
28
 * A new syntax is introduced to shorten the localization path.
29
 *
30
 * Moreover, if the given key does not correspond to an existing localization
31
 * entry it is returned unmodified.
32
 *
33
 * ---
34
 *
35
 * The shorten syntax looks like:
36
 *
37
 * `[path to localization file]:[localization key]`
38
 *
39
 * - The path to the localization file is relative to the registered extensions'
40
 *   private resources folder, without the file extension needed (`.xlf`).
41
 *
42
 * - The localization key is the same as in a classical localization path.
43
 *
44
 * For example, giving...
45
 *
46
 * `Events/MyEvent:some_event.label`
47
 *
48
 * ...will actually fetch:
49
 *
50
 * `LLL:EXT:my_extension/Resources/Private/Language/Events/MyEvent.xlf:some_event.label`
51
 *
52
 * To register a new extension key to be used in the shorten version, put the
53
 * following code in your `ext_localconf.php` file:
54
 *
55
 * `\CuyZ\Notiz\Service\LocalizationService::get()->addExtensionKey('my_extension');`
56
 */
57
class LocalizationService implements SingletonInterface
58
{
59
    use SelfInstantiateTrait;
60
61
    /**
62
     * Extension keys that will be used for the shorten syntax version.
63
     *
64
     * @var array
65
     */
66
    protected $extensionKeys = [
67
        NotizConstants::EXTENSION_KEY,
68
    ];
69
70
    /**
71
     * Static proxy to the method `doLocalize()`.
72
     *
73
     * @param string $key
74
     * @param array $arguments
75
     * @return string
76
     */
77
    public static function localize($key, array $arguments = [])
78
    {
79
        return self::get()->doLocalize($key, $arguments);
80
    }
81
82
    /**
83
     * See class description for more information.
84
     *
85
     * @param string $path
86
     * @param array $arguments
87
     * @return string
88
     */
89
    public function doLocalize($path, array $arguments = [])
90
    {
91
        if (false === strpos($path, ':')) {
92
            $possiblePaths = $this->getPossiblePaths('locallang', $path);
93
        } elseif (strpos($path, 'LLL:') === 0) {
94
            $possiblePaths = [$path];
95
        } else {
96
            // Getting the file path and the localization key.
97
            list($file, $key) = explode(':', $path);
98
99
            // Deleting possible `/` at the beginning of the file path.
100
            if (strpos($file, '/') === 0) {
101
                $file = substr($file, 1, strlen($file));
102
            }
103
104
            $possiblePaths = $this->getPossiblePaths($file, $key);
105
        }
106
107
        /*
108
         * Looping on all the possible localization entries. The first found
109
         * entry is returned.
110
         */
111
        foreach ($possiblePaths as $possiblePath) {
112
            $value = $this->localizeInternal($possiblePath, $arguments);
113
114
            if ($value && $value !== $possiblePath) {
115
                return $value;
116
            }
117
        }
118
119
        // If no localization entry was found, the given is returned.
120
        return $path;
121
    }
122
123
    /**
124
     * Adds an extension key that will be used for the shorten version of the
125
     * localization path syntax.
126
     *
127
     * See class description for more information.
128
     *
129
     * @param string $extensionKey
130
     */
131
    public function addExtensionKey($extensionKey)
132
    {
133
        if (!in_array($extensionKey, $this->extensionKeys)) {
134
            $this->extensionKeys[] = $extensionKey;
135
        }
136
    }
137
138
    /**
139
     * @param string $file
140
     * @param string $key
141
     * @return array
142
     */
143
    protected function getPossiblePaths($file, $key)
144
    {
145
        return array_map(
146
            function ($extensionKey) use ($file, $key) {
147
                return 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/' . $file . '.xlf:' . $key;
148
            },
149
            $this->extensionKeys
150
        );
151
    }
152
153
    /**
154
     * Calls the TYPO3 localization service.
155
     *
156
     * @param string $key
157
     * @param array $arguments
158
     * @return string
159
     */
160
    protected function localizeInternal($key, array $arguments)
161
    {
162
        return LocalizationUtility::translate(
163
            $key,
164
            NotizConstants::EXTENSION_KEY,
165
            $arguments
166
        );
167
    }
168
}
169