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