| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -global $acyWPLangCodes; | |
| 4 | -$acyWPLangCodes = [ | |
| 3 | +defined('ABSPATH') || die('Restricted Access'); | |
| 4 | + | |
| 5 | +global $acymWPLangCodes; | |
| 6 | +$acymWPLangCodes = [ | |
| 5 | 7 | 'af' => 'af-ZA', |
| 6 | 8 | 'ar' => 'ar-AA', |
| 7 | 9 | 'as' => 'as-AS', // Not sure |
| 8 | 10 | 'az' => 'az-AZ', // Not sure |
| @@ -36,10 +38,16 @@ | ||
| 36 | 38 | ]; |
| 37 | 39 | |
| 38 | 40 | global $acymLanguages; |
| 39 | 41 | |
| 40 | -function acym_translation($key, $jsSafe = false, $interpretBackSlashes = true, $textdomain = 'acymailing') | |
| 42 | +/** | |
| 43 | + * Display the translation based on a key | |
| 44 | + * | |
| 45 | + * @param bool $interpretBackSlashes Deprecated Interpret or not the \ like \t \n etc... | |
| 46 | + */ | |
| 47 | +function acym_translation(string $key, bool $jsSafe = false, bool $interpretBackSlashes = true, string $textdomain = 'acymailing'): string | |
| 41 | 48 | { |
| 49 | + // Return the key passed by default as it may be a random text instead of a key | |
| 42 | 50 | $translation = $key; |
| 43 | 51 | |
| 44 | 52 | global $acymLanguages; |
| 45 | 53 | acym_getLanguageTag(); |
| @@ -52,19 +60,23 @@ | ||
| 52 | 60 | break; |
| 53 | 61 | } |
| 54 | 62 | } |
| 55 | 63 | |
| 56 | - global $customTranslation; | |
| 64 | + global $acymailingCustomTranslation; | |
| 57 | 65 | acym_getCustomTranslation(); |
| 58 | 66 | |
| 59 | - if (!empty($customTranslation) && isset($customTranslation[$key])) { | |
| 60 | - $translation = $customTranslation[$key]; | |
| 67 | + if (!empty($acymailingCustomTranslation) && isset($acymailingCustomTranslation[$key])) { | |
| 68 | + $translation = $acymailingCustomTranslation[$key]; | |
| 61 | 69 | } elseif (!empty($acymailingEnglishText)) { |
| 70 | + // If there is an english translation, get the WordPress translation. By default, it returns the text passed, so the english translation | |
| 71 | + // phpcs:ignore WordPress.WP.I18n.MissingArgDomain,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain | |
| 62 | 72 | $translation = empty($textdomain) ? __($acymailingEnglishText) : __($acymailingEnglishText, $textdomain); |
| 63 | 73 | |
| 74 | + // If there is no translation on the WordPress side, and we're not in english, take the AcyMailing community translation | |
| 64 | 75 | if ($translation === $acymailingEnglishText && $acymLanguages['currentLanguage'] != ACYM_DEFAULT_LANGUAGE) { |
| 65 | 76 | foreach ($acymLanguages[$acymLanguages['currentLanguage']] as $fileContent) { |
| 66 | 77 | if (isset($fileContent[$key])) { |
| 78 | + // If a translation is found for the specified key, take it | |
| 67 | 79 | $translation = $fileContent[$key]; |
| 68 | 80 | break; |
| 69 | 81 | } |
| 70 | 82 | } |
| @@ -70,8 +82,9 @@ | ||
| 70 | 82 | } |
| 71 | 83 | } |
| 72 | 84 | } |
| 73 | 85 | |
| 86 | + // Escape the quotes if we're in a javascript context | |
| 74 | 87 | if ($jsSafe) { |
| 75 | 88 | $translation = str_replace('"', '\"', $translation); |
| 76 | 89 | } elseif ($interpretBackSlashes && strpos($translation, '\\') !== false) { |
| 77 | 90 | $translation = str_replace(['\\\\', '\t', '\n'], ["\\", "\t", "\n"], $translation); |
| @@ -77,8 +90,9 @@ | ||
| 77 | 90 | $translation = str_replace(['\\\\', '\t', '\n'], ["\\", "\t", "\n"], $translation); |
| 78 | 91 | } |
| 79 | 92 | |
| 80 | 93 | if ($translation == $key) { |
| 94 | + // phpcs:ignore WordPress.WP.I18n.MissingArgDomain,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain | |
| 81 | 95 | $translation = empty($textdomain) ? __($translation) : __($translation, $textdomain); |
| 82 | 96 | } |
| 83 | 97 | |
| 84 | 98 | return $translation; |
| @@ -83,9 +97,9 @@ | ||
| 83 | 97 | |
| 84 | 98 | return $translation; |
| 85 | 99 | } |
| 86 | 100 | |
| 87 | -function acym_setLanguage($lang) | |
| 101 | +function acym_setLanguage(string $lang): string | |
| 88 | 102 | { |
| 89 | 103 | global $acymLanguages; |
| 90 | 104 | $previousLanguage = $acymLanguages['currentLanguage']; |
| 91 | 105 | $acymLanguages['currentLanguage'] = $lang; |
| @@ -92,9 +106,12 @@ | ||
| 92 | 106 | |
| 93 | 107 | return $previousLanguage; |
| 94 | 108 | } |
| 95 | 109 | |
| 96 | -function acym_translationSprintf() | |
| 110 | +/** | |
| 111 | + * Display the according translation | |
| 112 | + */ | |
| 113 | +function acym_translationSprintf(): string | |
| 97 | 114 | { |
| 98 | 115 | $args = func_get_args(); |
| 99 | 116 | $args[0] = acym_translation($args[0]); |
| 100 | 117 | |
| @@ -100,11 +117,11 @@ | ||
| 100 | 117 | |
| 101 | 118 | return call_user_func_array('sprintf', $args); |
| 102 | 119 | } |
| 103 | 120 | |
| 104 | -function acym_getLanguages($uppercaseLangCode = false, $published = false) | |
| 121 | +function acym_getLanguages(bool $uppercaseLangCode = false, bool $published = false): array | |
| 105 | 122 | { |
| 106 | - global $acyWPLangCodes; | |
| 123 | + global $acymWPLangCodes; | |
| 107 | 124 | |
| 108 | 125 | $result = []; |
| 109 | 126 | |
| 110 | 127 | require_once ABSPATH.'wp-admin/includes/translation-install.php'; |
| @@ -111,9 +128,11 @@ | ||
| 111 | 128 | $wplanguages = wp_get_available_translations(); |
| 112 | 129 | $languages = get_available_languages(); |
| 113 | 130 | foreach ($languages as $oneLang) { |
| 114 | 131 | $wpLangCode = $oneLang; |
| 115 | - if (!empty($acyWPLangCodes[$oneLang])) $oneLang = $acyWPLangCodes[$oneLang]; | |
| 132 | + if (!empty($acymWPLangCodes[$oneLang])) { | |
| 133 | + $oneLang = $acymWPLangCodes[$oneLang]; | |
| 134 | + } | |
| 116 | 135 | $langTag = str_replace('_', '-', $oneLang); |
| 117 | 136 | |
| 118 | 137 | $lang = new stdClass(); |
| 119 | 138 | $lang->sef = empty($wplanguages[$oneLang]['iso'][1]) ? null : $wplanguages[$oneLang]['iso'][1]; |
| @@ -138,9 +157,9 @@ | ||
| 138 | 157 | |
| 139 | 158 | return $result; |
| 140 | 159 | } |
| 141 | 160 | |
| 142 | -function acym_getLanguageTag($simple = false) | |
| 161 | +function acym_getLanguageTag(bool $simple = false): string | |
| 143 | 162 | { |
| 144 | 163 | if (acym_isAdmin()) { |
| 145 | 164 | $currentLocale = get_user_locale(acym_currentUserId()); |
| 146 | 165 | } else { |
| @@ -146,9 +165,9 @@ | ||
| 146 | 165 | } else { |
| 147 | 166 | $currentLocale = get_locale(); |
| 148 | 167 | } |
| 149 | 168 | |
| 150 | - $currentLocale = convertWPLocaleToAcyLocale($currentLocale); | |
| 169 | + $currentLocale = acym_convertWPLocaleToAcyLocale($currentLocale); | |
| 151 | 170 | |
| 152 | 171 | global $acymLanguages; |
| 153 | 172 | if (!isset($acymLanguages['currentLanguage'])) { |
| 154 | 173 | $acymLanguages['currentLanguage'] = $currentLocale; |
| @@ -156,13 +175,23 @@ | ||
| 156 | 175 | |
| 157 | 176 | return $simple ? substr($acymLanguages['currentLanguage'], 0, 2) : $acymLanguages['currentLanguage']; |
| 158 | 177 | } |
| 159 | 178 | |
| 179 | +function acym_getSiteLanguageTag(bool $simple = false): string | |
| 180 | +{ | |
| 181 | + $currentLocale = acym_convertWPLocaleToAcyLocale(get_locale()); | |
| 182 | + | |
| 183 | + return $simple ? substr($currentLocale, 0, 2) : $currentLocale; | |
| 184 | +} | |
| 185 | + | |
| 160 | 186 | function acym_getCustomTranslation() |
| 161 | 187 | { |
| 162 | - global $customTranslation; | |
| 163 | - if (isset($customTranslation)) return; | |
| 164 | - $customTranslation = []; | |
| 188 | + global $acymailingCustomTranslation; | |
| 189 | + if (isset($acymailingCustomTranslation)) { | |
| 190 | + return; | |
| 191 | + } | |
| 192 | + | |
| 193 | + $acymailingCustomTranslation = []; | |
| 165 | 194 | $currentLanguage = acym_getLanguageTag(); |
| 166 | 195 | $filePath = ACYM_LANGUAGE.$currentLanguage.'.'.ACYM_LANGUAGE_FILE.'_custom.ini'; |
| 167 | 196 | if (file_exists($filePath)) { |
| 168 | 197 | $data = acym_fileGetContent($filePath); |
| @@ -173,20 +202,26 @@ | ||
| 173 | 202 | |
| 174 | 203 | $keyval = explode('=', $raw); |
| 175 | 204 | $key = array_shift($keyval); |
| 176 | 205 | |
| 177 | - $customTranslation[$key] = trim(implode('=', $keyval), "\"\r\n\t "); | |
| 206 | + $acymailingCustomTranslation[$key] = trim(implode('=', $keyval), "\"\r\n\t "); | |
| 178 | 207 | } |
| 179 | 208 | } |
| 180 | 209 | } |
| 181 | 210 | |
| 182 | -function acym_loadLanguageFile($extension, $basePath = null, $lang = null, $reload = false, $default = true) | |
| 211 | +function acym_loadLanguageFile(string $extension, ?string $basePath = null, ?string $lang = null, bool $reload = false, bool $default = true): void | |
| 183 | 212 | { |
| 184 | 213 | global $acymLanguages; |
| 185 | 214 | $currentLanguage = acym_getLanguageTag(); |
| 186 | - if (isset($acymLanguages[$currentLanguage][$extension]) && !$reload) return; | |
| 215 | + if (isset($acymLanguages[$currentLanguage][$extension]) && !$reload) { | |
| 216 | + return; | |
| 217 | + } | |
| 187 | 218 | |
| 188 | - $base = ACYM_LANGUAGE; | |
| 219 | + if (ACYM_PRODUCTION) { | |
| 220 | + $base = ACYM_LANGUAGE; | |
| 221 | + } | |
| 222 | + | |
| 223 | + | |
| 189 | 224 | $language = $currentLanguage; |
| 190 | 225 | |
| 191 | 226 | if (!file_exists($base.$language.'.'.$extension.'.ini')) { |
| 192 | 227 | $language = ACYM_DEFAULT_LANGUAGE; |
| @@ -228,24 +263,25 @@ | ||
| 228 | 263 | $acymLanguages[ACYM_DEFAULT_LANGUAGE][$extension][$key] = trim(implode('=', $keyval), "\"\r\n\t "); |
| 229 | 264 | } |
| 230 | 265 | } |
| 231 | 266 | |
| 232 | -function acym_getLanguagePath($basePath, $language = null) | |
| 267 | +function acym_getLanguagePath(string $basePath, ?string $language = null): string | |
| 233 | 268 | { |
| 234 | 269 | return rtrim(ACYM_LANGUAGE, DS); |
| 235 | 270 | } |
| 236 | 271 | |
| 237 | -function acym_languageOption($emailLanguage, $name) | |
| 272 | +function acym_languageOption(?string $emailLanguage, string $name): void | |
| 238 | 273 | { |
| 239 | - return ''; | |
| 240 | 274 | } |
| 241 | 275 | |
| 242 | -function convertWPLocaleToAcyLocale($locale) | |
| 276 | +function acym_convertWPLocaleToAcyLocale(string $locale): string | |
| 243 | 277 | { |
| 244 | 278 | if (strpos($locale, '-') !== false) return $locale; |
| 245 | 279 | |
| 246 | - global $acyWPLangCodes; | |
| 247 | - if (!empty($acyWPLangCodes[$locale])) return $acyWPLangCodes[$locale]; | |
| 280 | + global $acymWPLangCodes; | |
| 281 | + if (!empty($acymWPLangCodes[$locale])) { | |
| 282 | + return $acymWPLangCodes[$locale]; | |
| 283 | + } | |
| 248 | 284 | |
| 249 | 285 | if (strpos($locale, '_') === false) { |
| 250 | 286 | return $locale.'-'.strtoupper($locale); |
| 251 | 287 | } else { |
| @@ -252,17 +288,17 @@ | ||
| 252 | 288 | return str_replace('_', '-', $locale); |
| 253 | 289 | } |
| 254 | 290 | } |
| 255 | 291 | |
| 256 | -function acym_getCmsUserLanguage($userId = null) | |
| 292 | +function acym_getCmsUserLanguage(?int $userId = null): ?string | |
| 257 | 293 | { |
| 258 | 294 | if ($userId === null) $userId = acym_currentUserId(); |
| 259 | 295 | if (empty($userId)) return ''; |
| 260 | 296 | |
| 261 | - return convertWPLocaleToAcyLocale(get_user_locale($userId)); | |
| 297 | + return acym_convertWPLocaleToAcyLocale(get_user_locale($userId)); | |
| 262 | 298 | } |
| 263 | 299 | |
| 264 | -function acym_getTranslationTools() | |
| 300 | +function acym_getTranslationTools(): array | |
| 265 | 301 | { |
| 266 | 302 | $options = [ |
| 267 | 303 | (object)['value' => 'no', 'text' => 'ACYM_NO'], |
| 268 | 304 | ]; |