Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.45% |
21 / 22 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
LanguageInitializerTrait | |
95.45% |
21 / 22 |
|
50.00% |
1 / 2 |
6 | |
0.00% |
0 / 1 |
getTextDomains | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
addLanguageToTranslator | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 |
1 | <?php |
2 | |
3 | /** |
4 | * Logic for initializing a language within a translator used by VuFind. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2019. |
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 Translator |
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 Main Site |
28 | */ |
29 | |
30 | namespace VuFind\I18n\Translator; |
31 | |
32 | use Laminas\I18n\Translator\TranslatorInterface; |
33 | use VuFind\I18n\Locale\LocaleSettings; |
34 | |
35 | use function strlen; |
36 | |
37 | /** |
38 | * Logic for initializing a language within a translator used by VuFind. |
39 | * |
40 | * @category VuFind |
41 | * @package Translator |
42 | * @author Demian Katz <demian.katz@villanova.edu> |
43 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
44 | * @link https://vufind.org Main Site |
45 | */ |
46 | trait LanguageInitializerTrait |
47 | { |
48 | /** |
49 | * Look up all text domains. |
50 | * |
51 | * @return array |
52 | */ |
53 | protected function getTextDomains() |
54 | { |
55 | $base = APPLICATION_PATH; |
56 | $local = LOCAL_OVERRIDE_DIR; |
57 | $languagePathParts = ["$base/languages"]; |
58 | if (strlen($local) > 0) { |
59 | $languagePathParts[] = "$local/languages"; |
60 | } |
61 | $languagePathParts[] = "$base/themes/*/languages"; |
62 | |
63 | $domains = []; |
64 | foreach ($languagePathParts as $current) { |
65 | $places = glob($current . '/*', GLOB_ONLYDIR | GLOB_NOSORT); |
66 | $domains = array_merge($domains, array_map('basename', $places)); |
67 | } |
68 | |
69 | return array_unique($domains); |
70 | } |
71 | |
72 | /** |
73 | * Configure a translator to support the requested language. |
74 | * |
75 | * @param TranslatorInterface $translator Translator |
76 | * @param LocaleSettings $settings Locale settings |
77 | * @param string $language Language to set up |
78 | * |
79 | * @return void |
80 | */ |
81 | protected function addLanguageToTranslator( |
82 | TranslatorInterface $translator, |
83 | LocaleSettings $settings, |
84 | string $language |
85 | ): void { |
86 | // Don't double-initialize languages: |
87 | if ($settings->isLocaleInitialized($language)) { |
88 | return; |
89 | } |
90 | $settings->markLocaleInitialized($language); |
91 | |
92 | // If we got this far, we need to set everything up: |
93 | $translator->addTranslationFile('ExtendedIni', null, 'default', $language); |
94 | foreach ($this->getTextDomains() as $domain) { |
95 | // Set up text domains using the domain name as the filename; |
96 | // this will help the ExtendedIni loader dynamically locate |
97 | // the appropriate files. |
98 | $translator->addTranslationFile( |
99 | 'ExtendedIni', |
100 | $domain, |
101 | $domain, |
102 | $language |
103 | ); |
104 | } |
105 | } |
106 | } |