| @@ -1,0 +1,315 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +defined('ABSPATH') || die('Restricted Access'); | |
| 4 | + | |
| 5 | +global $acymWPLangCodes; | |
| 6 | +$acymWPLangCodes = [ | |
| 7 | + 'af' => 'af-ZA', | |
| 8 | + 'ar' => 'ar-AA', | |
| 9 | + 'as' => 'as-AS', // Not sure | |
| 10 | + 'az' => 'az-AZ', // Not sure | |
| 11 | + 'bo' => 'bo-BO', // Not sure | |
| 12 | + 'ca' => 'ca-ES', | |
| 13 | + 'cy' => 'cy-GB', | |
| 14 | + 'el' => 'el-GR', | |
| 15 | + 'eo' => 'eo-XX', | |
| 16 | + 'et' => 'et-EE', | |
| 17 | + 'eu' => 'eu-ES', | |
| 18 | + 'fi' => 'fi-FI', | |
| 19 | + 'gd' => 'gd-GD', // Not sure | |
| 20 | + 'gu' => 'gu-GU', // Not sure | |
| 21 | + 'hr' => 'hr-HR', | |
| 22 | + 'hy' => 'hy-AM', | |
| 23 | + 'ja' => 'ja-JP', | |
| 24 | + 'kk' => 'kk-KK', // Not sure | |
| 25 | + 'km' => 'km-KH', | |
| 26 | + 'lo' => 'lo-LO', // Not sure | |
| 27 | + 'lv' => 'lv-LV', | |
| 28 | + 'mn' => 'mn-MN', // Not sure | |
| 29 | + 'mr' => 'mr-MR', // Not sure | |
| 30 | + 'ps' => 'ps-PS', // Not sure | |
| 31 | + 'sq' => 'sq-AL', | |
| 32 | + 'te' => 'te-TE', | |
| 33 | + 'th' => 'th-TH', | |
| 34 | + 'tl' => 'tl-TL', // Not sure | |
| 35 | + 'uk' => 'uk-UA', | |
| 36 | + 'ur' => 'ur-PK', // Not sure | |
| 37 | + 'vi' => 'vi-VN', | |
| 38 | +]; | |
| 39 | + | |
| 40 | +global $acymLanguages; | |
| 41 | + | |
| 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 | |
| 48 | +{ | |
| 49 | + // Return the key passed by default as it may be a random text instead of a key | |
| 50 | + $translation = $key; | |
| 51 | + | |
| 52 | + global $acymLanguages; | |
| 53 | + acym_getLanguageTag(); | |
| 54 | + if (!isset($acymLanguages[$acymLanguages['currentLanguage']])) acym_loadLanguage(); | |
| 55 | + | |
| 56 | + $acymailingEnglishText = ''; | |
| 57 | + foreach ($acymLanguages[ACYM_DEFAULT_LANGUAGE] as $fileContent) { | |
| 58 | + if (isset($fileContent[$key])) { | |
| 59 | + $acymailingEnglishText = $fileContent[$key]; | |
| 60 | + break; | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | + global $acymailingCustomTranslation; | |
| 65 | + acym_getCustomTranslation(); | |
| 66 | + | |
| 67 | + if (!empty($acymailingCustomTranslation) && isset($acymailingCustomTranslation[$key])) { | |
| 68 | + $translation = $acymailingCustomTranslation[$key]; | |
| 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 | |
| 72 | + $translation = empty($textdomain) ? __($acymailingEnglishText) : __($acymailingEnglishText, $textdomain); | |
| 73 | + | |
| 74 | + // If there is no translation on the WordPress side, and we're not in english, take the AcyMailing community translation | |
| 75 | + if ($translation === $acymailingEnglishText && $acymLanguages['currentLanguage'] != ACYM_DEFAULT_LANGUAGE) { | |
| 76 | + foreach ($acymLanguages[$acymLanguages['currentLanguage']] as $fileContent) { | |
| 77 | + if (isset($fileContent[$key])) { | |
| 78 | + // If a translation is found for the specified key, take it | |
| 79 | + $translation = $fileContent[$key]; | |
| 80 | + break; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + // Escape the quotes if we're in a javascript context | |
| 87 | + if ($jsSafe) { | |
| 88 | + $translation = str_replace('"', '\"', $translation); | |
| 89 | + } elseif ($interpretBackSlashes && strpos($translation, '\\') !== false) { | |
| 90 | + $translation = str_replace(['\\\\', '\t', '\n'], ["\\", "\t", "\n"], $translation); | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ($translation == $key) { | |
| 94 | + // phpcs:ignore WordPress.WP.I18n.MissingArgDomain,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain | |
| 95 | + $translation = empty($textdomain) ? __($translation) : __($translation, $textdomain); | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $translation; | |
| 99 | +} | |
| 100 | + | |
| 101 | +function acym_setLanguage(string $lang): string | |
| 102 | +{ | |
| 103 | + global $acymLanguages; | |
| 104 | + $previousLanguage = $acymLanguages['currentLanguage']; | |
| 105 | + $acymLanguages['currentLanguage'] = $lang; | |
| 106 | + | |
| 107 | + return $previousLanguage; | |
| 108 | +} | |
| 109 | + | |
| 110 | +/** | |
| 111 | + * Display the according translation | |
| 112 | + */ | |
| 113 | +function acym_translationSprintf(): string | |
| 114 | +{ | |
| 115 | + $args = func_get_args(); | |
| 116 | + $args[0] = acym_translation($args[0]); | |
| 117 | + | |
| 118 | + return call_user_func_array('sprintf', $args); | |
| 119 | +} | |
| 120 | + | |
| 121 | +function acym_getLanguages(bool $uppercaseLangCode = false, bool $published = false): array | |
| 122 | +{ | |
| 123 | + global $acymWPLangCodes; | |
| 124 | + | |
| 125 | + $result = []; | |
| 126 | + | |
| 127 | + require_once ABSPATH.'wp-admin/includes/translation-install.php'; | |
| 128 | + $wplanguages = wp_get_available_translations(); | |
| 129 | + $languages = get_available_languages(); | |
| 130 | + foreach ($languages as $oneLang) { | |
| 131 | + $wpLangCode = $oneLang; | |
| 132 | + if (!empty($acymWPLangCodes[$oneLang])) { | |
| 133 | + $oneLang = $acymWPLangCodes[$oneLang]; | |
| 134 | + } | |
| 135 | + $langTag = str_replace('_', '-', $oneLang); | |
| 136 | + | |
| 137 | + $lang = new stdClass(); | |
| 138 | + $lang->sef = empty($wplanguages[$oneLang]['iso'][1]) ? null : $wplanguages[$oneLang]['iso'][1]; | |
| 139 | + $lang->language = $uppercaseLangCode ? $langTag : strtolower($langTag); | |
| 140 | + $lang->name = empty($wplanguages[$wpLangCode]) ? $langTag : $wplanguages[$wpLangCode]['native_name']; | |
| 141 | + $lang->exists = file_exists(ACYM_LANGUAGE.$langTag.'.'.ACYM_LANGUAGE_FILE.'.ini'); | |
| 142 | + $lang->content = true; | |
| 143 | + | |
| 144 | + $result[$langTag] = $lang; | |
| 145 | + } | |
| 146 | + | |
| 147 | + if (!in_array('en-US', array_keys($result))) { | |
| 148 | + $lang = new stdClass(); | |
| 149 | + $lang->sef = 'en'; | |
| 150 | + $lang->language = 'en-us'; | |
| 151 | + $lang->name = 'English (United States)'; | |
| 152 | + $lang->exists = file_exists(ACYM_LANGUAGE.'en-US.'.ACYM_LANGUAGE_FILE.'.ini'); | |
| 153 | + $lang->content = true; | |
| 154 | + | |
| 155 | + $result['en-US'] = $lang; | |
| 156 | + } | |
| 157 | + | |
| 158 | + return $result; | |
| 159 | +} | |
| 160 | + | |
| 161 | +function acym_getLanguageTag(bool $simple = false): string | |
| 162 | +{ | |
| 163 | + if (acym_isAdmin()) { | |
| 164 | + $currentLocale = get_user_locale(acym_currentUserId()); | |
| 165 | + } else { | |
| 166 | + $currentLocale = get_locale(); | |
| 167 | + } | |
| 168 | + | |
| 169 | + $currentLocale = acym_convertWPLocaleToAcyLocale($currentLocale); | |
| 170 | + | |
| 171 | + global $acymLanguages; | |
| 172 | + if (!isset($acymLanguages['currentLanguage'])) { | |
| 173 | + $acymLanguages['currentLanguage'] = $currentLocale; | |
| 174 | + } | |
| 175 | + | |
| 176 | + return $simple ? substr($acymLanguages['currentLanguage'], 0, 2) : $acymLanguages['currentLanguage']; | |
| 177 | +} | |
| 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 | + | |
| 186 | +function acym_getCustomTranslation() | |
| 187 | +{ | |
| 188 | + global $acymailingCustomTranslation; | |
| 189 | + if (isset($acymailingCustomTranslation)) { | |
| 190 | + return; | |
| 191 | + } | |
| 192 | + | |
| 193 | + $acymailingCustomTranslation = []; | |
| 194 | + $currentLanguage = acym_getLanguageTag(); | |
| 195 | + $filePath = ACYM_LANGUAGE.$currentLanguage.'.'.ACYM_LANGUAGE_FILE.'_custom.ini'; | |
| 196 | + if (file_exists($filePath)) { | |
| 197 | + $data = acym_fileGetContent($filePath); | |
| 198 | + $data = str_replace('"_QQ_"', '"', $data); | |
| 199 | + $separate = explode("\n", $data); | |
| 200 | + foreach ($separate as $raw) { | |
| 201 | + if (strpos($raw, '=') === false) continue; | |
| 202 | + | |
| 203 | + $keyval = explode('=', $raw); | |
| 204 | + $key = array_shift($keyval); | |
| 205 | + | |
| 206 | + $acymailingCustomTranslation[$key] = trim(implode('=', $keyval), "\"\r\n\t "); | |
| 207 | + } | |
| 208 | + } | |
| 209 | +} | |
| 210 | + | |
| 211 | +function acym_loadLanguageFile(string $extension, ?string $basePath = null, ?string $lang = null, bool $reload = false, bool $default = true): void | |
| 212 | +{ | |
| 213 | + global $acymLanguages; | |
| 214 | + $currentLanguage = acym_getLanguageTag(); | |
| 215 | + if (isset($acymLanguages[$currentLanguage][$extension]) && !$reload) { | |
| 216 | + return; | |
| 217 | + } | |
| 218 | + | |
| 219 | + if (ACYM_PRODUCTION) { | |
| 220 | + $base = ACYM_LANGUAGE; | |
| 221 | + } | |
| 222 | + | |
| 223 | + | |
| 224 | + $language = $currentLanguage; | |
| 225 | + | |
| 226 | + if (!file_exists($base.$language.'.'.$extension.'.ini')) { | |
| 227 | + $language = ACYM_DEFAULT_LANGUAGE; | |
| 228 | + if (!file_exists($base.$language.'.'.$extension.'.ini')) { | |
| 229 | + $base = ACYM_FOLDER.'language'.DS; | |
| 230 | + $language = $currentLanguage; | |
| 231 | + if (!file_exists($base.$language.'.'.$extension.'.ini')) { | |
| 232 | + $language = ACYM_DEFAULT_LANGUAGE; | |
| 233 | + if (!file_exists($base.$language.'.'.$extension.'.ini')) return; | |
| 234 | + } | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + $data = acym_fileGetContent($base.$language.'.'.$extension.'.ini'); | |
| 239 | + $data = str_replace('"_QQ_"', '"', $data); | |
| 240 | + $separate = explode("\n", $data); | |
| 241 | + $storeExtension = $extension === ACYM_LANGUAGE_FILE.'_custom' ? ACYM_LANGUAGE_FILE : $extension; | |
| 242 | + foreach ($separate as $raw) { | |
| 243 | + if (strpos($raw, '=') === false) continue; | |
| 244 | + | |
| 245 | + $keyval = explode('=', $raw); | |
| 246 | + $key = array_shift($keyval); | |
| 247 | + | |
| 248 | + $acymLanguages[$acymLanguages['currentLanguage']][$storeExtension][$key] = trim(implode('=', $keyval), "\"\r\n\t "); | |
| 249 | + } | |
| 250 | + | |
| 251 | + if (!empty($acymLanguages[ACYM_DEFAULT_LANGUAGE])) return; | |
| 252 | + | |
| 253 | + $data = acym_fileGetContent($base.ACYM_DEFAULT_LANGUAGE.'.'.$extension.'.ini'); | |
| 254 | + $data = str_replace('"_QQ_"', '"', $data); | |
| 255 | + $separate = explode("\n", $data); | |
| 256 | + | |
| 257 | + foreach ($separate as $raw) { | |
| 258 | + if (strpos($raw, '=') === false) continue; | |
| 259 | + | |
| 260 | + $keyval = explode('=', $raw); | |
| 261 | + $key = array_shift($keyval); | |
| 262 | + | |
| 263 | + $acymLanguages[ACYM_DEFAULT_LANGUAGE][$extension][$key] = trim(implode('=', $keyval), "\"\r\n\t "); | |
| 264 | + } | |
| 265 | +} | |
| 266 | + | |
| 267 | +function acym_getLanguagePath(string $basePath, ?string $language = null): string | |
| 268 | +{ | |
| 269 | + return rtrim(ACYM_LANGUAGE, DS); | |
| 270 | +} | |
| 271 | + | |
| 272 | +function acym_languageOption(?string $emailLanguage, string $name): void | |
| 273 | +{ | |
| 274 | +} | |
| 275 | + | |
| 276 | +function acym_convertWPLocaleToAcyLocale(string $locale): string | |
| 277 | +{ | |
| 278 | + if (strpos($locale, '-') !== false) return $locale; | |
| 279 | + | |
| 280 | + global $acymWPLangCodes; | |
| 281 | + if (!empty($acymWPLangCodes[$locale])) { | |
| 282 | + return $acymWPLangCodes[$locale]; | |
| 283 | + } | |
| 284 | + | |
| 285 | + if (strpos($locale, '_') === false) { | |
| 286 | + return $locale.'-'.strtoupper($locale); | |
| 287 | + } else { | |
| 288 | + return str_replace('_', '-', $locale); | |
| 289 | + } | |
| 290 | +} | |
| 291 | + | |
| 292 | +function acym_getCmsUserLanguage(?int $userId = null): ?string | |
| 293 | +{ | |
| 294 | + if ($userId === null) $userId = acym_currentUserId(); | |
| 295 | + if (empty($userId)) return ''; | |
| 296 | + | |
| 297 | + return acym_convertWPLocaleToAcyLocale(get_user_locale($userId)); | |
| 298 | +} | |
| 299 | + | |
| 300 | +function acym_getTranslationTools(): array | |
| 301 | +{ | |
| 302 | + $options = [ | |
| 303 | + (object)['value' => 'no', 'text' => 'ACYM_NO'], | |
| 304 | + ]; | |
| 305 | + | |
| 306 | + $polylangOption = (object)['value' => 'polylang', 'text' => 'Polylang']; | |
| 307 | + if (!acym_isExtensionActive('polylang/polylang.php') && !acym_isExtensionActive('polylang-pro/polylang.php')) $polylangOption->disable = true; | |
| 308 | + $options[] = $polylangOption; | |
| 309 | + | |
| 310 | + $wpmlOption = (object)['value' => 'wpml', 'text' => 'WPML']; | |
| 311 | + if (!acym_isExtensionActive('sitepress-multilingual-cms/sitepress.php')) $wpmlOption->disable = true; | |
| 312 | + $options[] = $wpmlOption; | |
| 313 | + | |
| 314 | + return $options; | |
| 315 | +} | |