Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
67.86% |
19 / 28 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| I18nManager | |
67.86% |
19 / 28 |
|
20.00% |
1 / 5 |
16.78 | |
0.00% |
0 / 1 |
| setEnabledLanguages | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
2.09 | |||
| setLocaleForLanguage | |
55.56% |
5 / 9 |
|
0.00% |
0 / 1 |
5.40 | |||
| setLocalForAdminArea | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getEnabledLocales | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| getEnabledLanguages | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Utility; |
| 5 | |
| 6 | use Cake\Core\Configure; |
| 7 | use Cake\I18n\I18n; |
| 8 | |
| 9 | /** |
| 10 | * Class I18nManager |
| 11 | * |
| 12 | * This utility class provides internationalization helper functions. |
| 13 | * It uses the SettingsManager class to retrieve language settings. |
| 14 | */ |
| 15 | class I18nManager |
| 16 | { |
| 17 | /** |
| 18 | * @var array $locales |
| 19 | * |
| 20 | * An associative array mapping language codes to their respective locale identifiers. |
| 21 | */ |
| 22 | public static array $locales = [ |
| 23 | 'de' => 'de_DE', // German (Germany) |
| 24 | 'fr' => 'fr_FR', // French (France) |
| 25 | 'es' => 'es_ES', // Spanish (Spain) |
| 26 | 'it' => 'it_IT', // Italian (Italy) |
| 27 | 'pt' => 'pt_PT', // Portuguese (Portugal) |
| 28 | 'nl' => 'nl_NL', // Dutch (Netherlands) |
| 29 | 'pl' => 'pl_PL', // Polish (Poland) |
| 30 | 'ru' => 'ru_RU', // Russian (Russia) |
| 31 | 'sv' => 'sv_SE', // Swedish (Sweden) |
| 32 | 'da' => 'da_DK', // Danish (Denmark) |
| 33 | 'fi' => 'fi_FI', // Finnish (Finland) |
| 34 | 'no' => 'no_NO', // Norwegian (Norway) |
| 35 | 'el' => 'el_GR', // Greek (Greece) |
| 36 | 'tr' => 'tr_TR', // Turkish (Turkey) |
| 37 | 'cs' => 'cs_CZ', // Czech (Czech Republic) |
| 38 | 'hu' => 'hu_HU', // Hungarian (Hungary) |
| 39 | 'ro' => 'ro_RO', // Romanian (Romania) |
| 40 | 'sk' => 'sk_SK', // Slovak (Slovakia) |
| 41 | 'sl' => 'sl_SI', // Slovenian (Slovenia) |
| 42 | 'bg' => 'bg_BG', // Bulgarian (Bulgaria) |
| 43 | 'hr' => 'hr_HR', // Croatian (Croatia) |
| 44 | 'et' => 'et_EE', // Estonian (Estonia) |
| 45 | 'lv' => 'lv_LV', // Latvian (Latvia) |
| 46 | 'lt' => 'lt_LT', // Lithuanian (Lithuania) |
| 47 | 'uk' => 'uk_UA', // Ukrainian (Ukraine) |
| 48 | ]; |
| 49 | |
| 50 | /** |
| 51 | * @var array $languages |
| 52 | * |
| 53 | * An associative array mapping language codes to their respective language names. |
| 54 | */ |
| 55 | public static array $languages = [ |
| 56 | 'de' => 'German', |
| 57 | 'fr' => 'French', |
| 58 | 'es' => 'Spanish', |
| 59 | 'it' => 'Italian', |
| 60 | 'pt' => 'Portuguese', |
| 61 | 'nl' => 'Dutch', |
| 62 | 'pl' => 'Polish', |
| 63 | 'ru' => 'Russian', |
| 64 | 'sv' => 'Swedish', |
| 65 | 'da' => 'Danish', |
| 66 | 'fi' => 'Finnish', |
| 67 | 'no' => 'Norwegian', |
| 68 | 'el' => 'Greek', |
| 69 | 'tr' => 'Turkish', |
| 70 | 'cs' => 'Czech', |
| 71 | 'hu' => 'Hungarian', |
| 72 | 'ro' => 'Romanian', |
| 73 | 'sk' => 'Slovak', |
| 74 | 'sl' => 'Slovenian', |
| 75 | 'bg' => 'Bulgarian', |
| 76 | 'hr' => 'Croatian', |
| 77 | 'et' => 'Estonian', |
| 78 | 'lv' => 'Latvian', |
| 79 | 'lt' => 'Lithuanian', |
| 80 | 'uk' => 'Ukrainian', |
| 81 | ]; |
| 82 | |
| 83 | /** |
| 84 | * Set the enabled languages in the application configuration. |
| 85 | * |
| 86 | * This method checks if the 'settings' table exists in the database to ensure |
| 87 | * that the necessary tables have been loaded, which is crucial during the early |
| 88 | * stages of a fresh installation when the database might not yet be fully set up. |
| 89 | * If the table exists, it retrieves the enabled locales using the `getEnabledLocales()` |
| 90 | * method, extracts the language codes, merges them with the default languages, |
| 91 | * and sets the merged languages in the application configuration. If the table |
| 92 | * does not exist, it defaults to setting English ('en') as the only enabled language. |
| 93 | * |
| 94 | * This check is necessary because `I18nManager::setEnabledLanguages()` is called |
| 95 | * in the bootstrap of Application.php, and at this point, the database may not |
| 96 | * have had the default tables loaded. |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | public static function setEnabledLanguages(): void |
| 101 | { |
| 102 | if (DatabaseUtility::tableExists('settings')) { |
| 103 | $defaultLanguages = ['en']; |
| 104 | $enabledLanguages = array_keys(self::getEnabledLocales()); |
| 105 | $mergedLanguages = array_merge($defaultLanguages, $enabledLanguages); |
| 106 | Configure::write('I18n.languages', $mergedLanguages); |
| 107 | } else { |
| 108 | Configure::write('I18n.languages', ['en']); |
| 109 | I18n::setLocale('en_GB'); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Set the locale based on the provided language code. |
| 115 | * |
| 116 | * This method retrieves the enabled locales using the `getEnabledLocales()` |
| 117 | * method, finds the matching locale for the provided language code, and sets |
| 118 | * the locale using `I18n::setLocale()`. If no matching locale is found, it |
| 119 | * sets the locale to 'en_GB'. |
| 120 | * |
| 121 | * @param string $language The language code to set the locale for. |
| 122 | * @return void |
| 123 | */ |
| 124 | public static function setLocaleForLanguage(string $language): void |
| 125 | { |
| 126 | $locales = self::getEnabledLocales(); |
| 127 | |
| 128 | $matchedLocale = null; |
| 129 | foreach ($locales as $lang => $locale) { |
| 130 | if ($language == $lang) { |
| 131 | $matchedLocale = $locale; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!empty($matchedLocale)) { |
| 137 | I18n::setLocale($matchedLocale); |
| 138 | } else { |
| 139 | I18n::setLocale('en_GB'); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Set the locale for the admin area based on the settings. |
| 145 | * |
| 146 | * This method retrieves the admin locale from the settings using the `SettingsManager` |
| 147 | * class. If an admin locale is found, it sets the locale using `I18n::setLocale()`. |
| 148 | * |
| 149 | * @return void |
| 150 | */ |
| 151 | public static function setLocalForAdminArea(): void |
| 152 | { |
| 153 | $adminLocale = SettingsManager::read('i18n.locale', null); |
| 154 | if (!empty($adminLocale)) { |
| 155 | I18n::setLocale($adminLocale); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the enabled locales from the settings. |
| 161 | * |
| 162 | * This method retrieves the translation settings using the `SettingsManager` |
| 163 | * class, filters the enabled locales, and returns an array where the keys are |
| 164 | * the language codes and the values are the corresponding locales. |
| 165 | * |
| 166 | * @return array An array of enabled locales, with language codes as keys and locales as values. |
| 167 | */ |
| 168 | public static function getEnabledLocales(): array |
| 169 | { |
| 170 | $locales = SettingsManager::read('Translations', []); |
| 171 | $enabledLocales = []; |
| 172 | foreach ($locales as $locale => $enabled) { |
| 173 | if ($enabled) { |
| 174 | $enabledLocales[substr($locale, 0, 2)] = $locale; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return $enabledLocales; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the enabled languages based on the enabled locales. |
| 183 | * |
| 184 | * This method uses the enabled locales to determine which languages are enabled |
| 185 | * and returns an array of these languages. |
| 186 | * |
| 187 | * @return array An array of enabled languages. |
| 188 | */ |
| 189 | public static function getEnabledLanguages(): array |
| 190 | { |
| 191 | $locales = self::getEnabledLocales(); |
| 192 | $languages = array_intersect_key(self::$languages, $locales); |
| 193 | |
| 194 | return $languages; |
| 195 | } |
| 196 | } |