Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_I18n | |
0.00% |
0 / 56 |
|
0.00% |
0 / 6 |
306 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getLangCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLangResourceByCode | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| isLanguageRightToLeft | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getAvailableLangs | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
56 | |||
| getAvailableLangsByUsage | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2013-2022 (update and modification) Open Assessment Technologies SA; |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | use oat\generis\model\OntologyRdf; |
| 27 | |
| 28 | /** |
| 29 | * Internationalization helper. |
| 30 | * |
| 31 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
| 32 | */ |
| 33 | class tao_helpers_I18n |
| 34 | { |
| 35 | /** |
| 36 | * Short description of attribute AVAILABLE_LANGS_CACHEKEY |
| 37 | * |
| 38 | * @access private |
| 39 | * @var string |
| 40 | */ |
| 41 | public const AVAILABLE_LANGS_CACHEKEY = 'i18n_available_langs'; |
| 42 | |
| 43 | /** |
| 44 | * Short description of attribute availableLangs |
| 45 | * |
| 46 | * @access protected |
| 47 | * @var array |
| 48 | */ |
| 49 | protected static $availableLangs = []; |
| 50 | |
| 51 | /** |
| 52 | * Load the translation strings |
| 53 | * |
| 54 | * @throws Exception |
| 55 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 56 | */ |
| 57 | public static function init(common_ext_Extension $extension, ?string $langCode): void |
| 58 | { |
| 59 | // if the langCode is empty do nothing |
| 60 | if (empty($langCode)) { |
| 61 | throw new Exception("Language is not defined"); |
| 62 | } |
| 63 | |
| 64 | //init the ClearFw l10n tools |
| 65 | $translations = tao_models_classes_LanguageService::singleton()->getServerBundle($langCode); |
| 66 | l10n::init($translations); |
| 67 | |
| 68 | $serviceManager = \oat\oatbox\service\ServiceManager::getServiceManager(); |
| 69 | $extraPoService = $serviceManager->get(\oat\tao\model\i18n\ExtraPoService::SERVICE_ID); |
| 70 | $extraPoCount = $extraPoService->requirePos(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the current interface language for backwards compatibility |
| 75 | * |
| 76 | * @access public |
| 77 | * @return string |
| 78 | */ |
| 79 | public static function getLangCode() |
| 80 | { |
| 81 | return common_session_SessionManager::getSession()->getInterfaceLanguage(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the code of a resource |
| 86 | * |
| 87 | * @throws common_exception_Error|common_exception_InconsistentData |
| 88 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 89 | */ |
| 90 | public static function getLangResourceByCode(string $code): ?core_kernel_classes_Resource |
| 91 | { |
| 92 | $langs = self::getAvailableLangs(); |
| 93 | return isset($langs[$code]) ? new core_kernel_classes_Resource($langs[$code]['uri']) : null; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param unknown $code |
| 98 | * @return boolean |
| 99 | */ |
| 100 | public static function isLanguageRightToLeft($code) |
| 101 | { |
| 102 | $orientation = null; |
| 103 | $langs = self::getAvailableLangs(); |
| 104 | $orientation = isset($langs[$code]) |
| 105 | ? $langs[$code][tao_models_classes_LanguageService::PROPERTY_LANGUAGE_ORIENTATION] |
| 106 | : null; |
| 107 | return $orientation == tao_models_classes_LanguageService::INSTANCE_ORIENTATION_RTL; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * This method returns the languages available in TAO. |
| 112 | * |
| 113 | * @access public |
| 114 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 115 | * @param boolean $langName If set to true, an associative array where keys are language codes and values are |
| 116 | * language labels. If set to false (default), a simple array of language codes is |
| 117 | * returned. |
| 118 | * @return array |
| 119 | * @throws common_exception_InconsistentData |
| 120 | */ |
| 121 | private static function getAvailableLangs() |
| 122 | { |
| 123 | //get it into the api only once |
| 124 | if (count(self::$availableLangs) == 0) { |
| 125 | try { |
| 126 | self::$availableLangs = common_cache_FileCache::singleton()->get(self::AVAILABLE_LANGS_CACHEKEY); |
| 127 | } catch (common_cache_NotFoundException $e) { |
| 128 | $langClass = new core_kernel_classes_Class(tao_models_classes_LanguageService::CLASS_URI_LANGUAGES); |
| 129 | $valueProperty = new core_kernel_classes_Property(OntologyRdf::RDF_VALUE); |
| 130 | foreach ($langClass->getInstances() as $lang) { |
| 131 | $values = $lang->getPropertiesValues([ |
| 132 | OntologyRdf::RDF_VALUE, |
| 133 | tao_models_classes_LanguageService::PROPERTY_LANGUAGE_USAGES, |
| 134 | tao_models_classes_LanguageService::PROPERTY_LANGUAGE_ORIENTATION |
| 135 | ]); |
| 136 | if (count($values[OntologyRdf::RDF_VALUE]) != 1) { |
| 137 | throw new common_exception_InconsistentData('Error with value of language ' . $lang->getUri()); |
| 138 | } |
| 139 | $value = current($values[OntologyRdf::RDF_VALUE])->__toString(); |
| 140 | $usages = []; |
| 141 | foreach ($values[tao_models_classes_LanguageService::PROPERTY_LANGUAGE_USAGES] as $usage) { |
| 142 | $usages[] = $usage->getUri(); |
| 143 | } |
| 144 | if (count($values[tao_models_classes_LanguageService::PROPERTY_LANGUAGE_ORIENTATION]) != 1) { |
| 145 | common_Logger::w('Error with orientation of language ' . $lang->getUri()); |
| 146 | $orientation = tao_models_classes_LanguageService::INSTANCE_ORIENTATION_LTR; |
| 147 | } else { |
| 148 | $orientation = current( |
| 149 | $values[tao_models_classes_LanguageService::PROPERTY_LANGUAGE_ORIENTATION] |
| 150 | )->getUri(); |
| 151 | } |
| 152 | self::$availableLangs[$value] = [ |
| 153 | 'uri' => $lang->getUri(), |
| 154 | 'label' => $lang->getLabel(), |
| 155 | tao_models_classes_LanguageService::PROPERTY_LANGUAGE_USAGES => $usages, |
| 156 | tao_models_classes_LanguageService::PROPERTY_LANGUAGE_ORIENTATION => $orientation |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | uasort(self::$availableLangs, function ($item1, $item2) { |
| 161 | return strcasecmp($item1['label'], $item2['label']); |
| 162 | }); |
| 163 | |
| 164 | common_cache_FileCache::singleton()->put(self::$availableLangs, self::AVAILABLE_LANGS_CACHEKEY); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return self::$availableLangs; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get available languages from the knownledge base depending on a specific usage. |
| 173 | * |
| 174 | * By default, TAO considers two built-in usages: |
| 175 | * |
| 176 | * * GUI Language ('http://www.tao.lu/Ontologies/TAO.rdf#LanguageUsageGUI') |
| 177 | * * Data Language ('http://www.tao.lu/Ontologies/TAO.rdf#LanguageUsageData') |
| 178 | * |
| 179 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
| 180 | * @param core_kernel_classes_Resource $usage Resource usage An instance of tao:LanguagesUsages from the knowledge |
| 181 | * base. |
| 182 | * @return array An associative array of core_kernel_classes_Resource objects index by language code. |
| 183 | */ |
| 184 | public static function getAvailableLangsByUsage(core_kernel_classes_Resource $usage) |
| 185 | { |
| 186 | $returnValue = []; |
| 187 | |
| 188 | foreach (self::getAvailableLangs() as $code => $langData) { |
| 189 | if (in_array($usage->getUri(), $langData[tao_models_classes_LanguageService::PROPERTY_LANGUAGE_USAGES])) { |
| 190 | $lang = new core_kernel_classes_Resource($langData['uri']); |
| 191 | $returnValue[$code] = $lang->getLabel(); |
| 192 | } |
| 193 | } |
| 194 | return $returnValue; |
| 195 | } |
| 196 | } |