| 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_getCustomTranslation() |
| 180 |
{ |
| 181 |
global $acymailingCustomTranslation; |
| 182 |
if (isset($acymailingCustomTranslation)) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
$acymailingCustomTranslation = []; |
| 187 |
$currentLanguage = acym_getLanguageTag(); |
| 188 |
$filePath = ACYM_LANGUAGE.$currentLanguage.'.'.ACYM_LANGUAGE_FILE.'_custom.ini'; |
| 189 |
if (file_exists($filePath)) { |
| 190 |
$data = acym_fileGetContent($filePath); |
| 191 |
$data = str_replace('"_QQ_"', '"', $data); |
| 192 |
$separate = explode("\n", $data); |
| 193 |
foreach ($separate as $raw) { |
| 194 |
if (strpos($raw, '=') === false) continue; |
| 195 |
|
| 196 |
$keyval = explode('=', $raw); |
| 197 |
$key = array_shift($keyval); |
| 198 |
|
| 199 |
$acymailingCustomTranslation[$key] = trim(implode('=', $keyval), "\"\r\n\t "); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
function acym_loadLanguageFile(string $extension, ?string $basePath = null, ?string $lang = null, bool $reload = false, bool $default = true): void |
| 205 |
{ |
| 206 |
global $acymLanguages; |
| 207 |
$currentLanguage = acym_getLanguageTag(); |
| 208 |
if (isset($acymLanguages[$currentLanguage][$extension]) && !$reload) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
if (ACYM_PRODUCTION) { |
| 213 |
$base = ACYM_LANGUAGE; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
$language = $currentLanguage; |
| 218 |
|
| 219 |
if (!file_exists($base.$language.'.'.$extension.'.ini')) { |
| 220 |
$language = ACYM_DEFAULT_LANGUAGE; |
| 221 |
if (!file_exists($base.$language.'.'.$extension.'.ini')) { |
| 222 |
$base = ACYM_FOLDER.'language'.DS; |
| 223 |
$language = $currentLanguage; |
| 224 |
if (!file_exists($base.$language.'.'.$extension.'.ini')) { |
| 225 |
$language = ACYM_DEFAULT_LANGUAGE; |
| 226 |
if (!file_exists($base.$language.'.'.$extension.'.ini')) return; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
$data = acym_fileGetContent($base.$language.'.'.$extension.'.ini'); |
| 232 |
$data = str_replace('"_QQ_"', '"', $data); |
| 233 |
$separate = explode("\n", $data); |
| 234 |
$storeExtension = $extension === ACYM_LANGUAGE_FILE.'_custom' ? ACYM_LANGUAGE_FILE : $extension; |
| 235 |
foreach ($separate as $raw) { |
| 236 |
if (strpos($raw, '=') === false) continue; |
| 237 |
|
| 238 |
$keyval = explode('=', $raw); |
| 239 |
$key = array_shift($keyval); |
| 240 |
|
| 241 |
$acymLanguages[$acymLanguages['currentLanguage']][$storeExtension][$key] = trim(implode('=', $keyval), "\"\r\n\t "); |
| 242 |
} |
| 243 |
|
| 244 |
if (!empty($acymLanguages[ACYM_DEFAULT_LANGUAGE])) return; |
| 245 |
|
| 246 |
$data = acym_fileGetContent($base.ACYM_DEFAULT_LANGUAGE.'.'.$extension.'.ini'); |
| 247 |
$data = str_replace('"_QQ_"', '"', $data); |
| 248 |
$separate = explode("\n", $data); |
| 249 |
|
| 250 |
foreach ($separate as $raw) { |
| 251 |
if (strpos($raw, '=') === false) continue; |
| 252 |
|
| 253 |
$keyval = explode('=', $raw); |
| 254 |
$key = array_shift($keyval); |
| 255 |
|
| 256 |
$acymLanguages[ACYM_DEFAULT_LANGUAGE][$extension][$key] = trim(implode('=', $keyval), "\"\r\n\t "); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
function acym_getLanguagePath(string $basePath, ?string $language = null): string |
| 261 |
{ |
| 262 |
return rtrim(ACYM_LANGUAGE, DS); |
| 263 |
} |
| 264 |
|
| 265 |
function acym_languageOption(?string $emailLanguage, string $name): void |
| 266 |
{ |
| 267 |
} |
| 268 |
|
| 269 |
function acym_convertWPLocaleToAcyLocale(string $locale): string |
| 270 |
{ |
| 271 |
if (strpos($locale, '-') !== false) return $locale; |
| 272 |
|
| 273 |
global $acymWPLangCodes; |
| 274 |
if (!empty($acymWPLangCodes[$locale])) { |
| 275 |
return $acymWPLangCodes[$locale]; |
| 276 |
} |
| 277 |
|
| 278 |
if (strpos($locale, '_') === false) { |
| 279 |
return $locale.'-'.strtoupper($locale); |
| 280 |
} else { |
| 281 |
return str_replace('_', '-', $locale); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
function acym_getCmsUserLanguage(?int $userId = null): ?string |
| 286 |
{ |
| 287 |
if ($userId === null) $userId = acym_currentUserId(); |
| 288 |
if (empty($userId)) return ''; |
| 289 |
|
| 290 |
return acym_convertWPLocaleToAcyLocale(get_user_locale($userId)); |
| 291 |
} |
| 292 |
|
| 293 |
function acym_getTranslationTools(): array |
| 294 |
{ |
| 295 |
$options = [ |
| 296 |
(object)['value' => 'no', 'text' => 'ACYM_NO'], |
| 297 |
]; |
| 298 |
|
| 299 |
$polylangOption = (object)['value' => 'polylang', 'text' => 'Polylang']; |
| 300 |
if (!acym_isExtensionActive('polylang/polylang.php') && !acym_isExtensionActive('polylang-pro/polylang.php')) $polylangOption->disable = true; |
| 301 |
$options[] = $polylangOption; |
| 302 |
|
| 303 |
$wpmlOption = (object)['value' => 'wpml', 'text' => 'WPML']; |
| 304 |
if (!acym_isExtensionActive('sitepress-multilingual-cms/sitepress.php')) $wpmlOption->disable = true; |
| 305 |
$options[] = $wpmlOption; |
| 306 |
|
| 307 |
return $options; |
| 308 |
} |
| 309 |
|