Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
JsTranslations | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
mapValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * JsTranslations helper for passing translation text to Javascript |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package View_Helpers |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\View\Helper\Root; |
31 | |
32 | /** |
33 | * JsTranslations helper for passing translation text to Javascript |
34 | * |
35 | * @category VuFind |
36 | * @package View_Helpers |
37 | * @author Demian Katz <demian.katz@villanova.edu> |
38 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
39 | * @link https://vufind.org/wiki/development Wiki |
40 | */ |
41 | class JsTranslations extends AbstractJsStrings |
42 | { |
43 | /** |
44 | * Translate helper |
45 | * |
46 | * @var Translate |
47 | */ |
48 | protected $translate; |
49 | |
50 | /** |
51 | * Translate + escape helper |
52 | * |
53 | * @var TransEsc |
54 | */ |
55 | protected $transEsc; |
56 | |
57 | /** |
58 | * Constructor |
59 | * |
60 | * @param Translate $translate Translate helper |
61 | * @param TransEsc $transEsc Translate + escape helper |
62 | * @param string $varName Variable name to store translations |
63 | */ |
64 | public function __construct( |
65 | Translate $translate, |
66 | TransEsc $transEsc, |
67 | $varName = 'vufindString' |
68 | ) { |
69 | parent::__construct($varName); |
70 | $this->translate = $translate; |
71 | $this->transEsc = $transEsc; |
72 | } |
73 | |
74 | /** |
75 | * Translate string |
76 | * |
77 | * @param string|array $translation String to translate |
78 | * @param string $key JSON object key |
79 | * |
80 | * @return string |
81 | */ |
82 | protected function mapValue($translation, string $key): string |
83 | { |
84 | $translateFunc |
85 | = str_ends_with($key, '_html') || str_ends_with($key, '_unescaped') |
86 | ? $this->translate : $this->transEsc; |
87 | |
88 | // $translation could be a string or an array of parameters; this code |
89 | // normalizes it into a parameter list for the translator. |
90 | return ($translateFunc)(...((array)$translation)); |
91 | } |
92 | } |