= 70300) { $setCookieResult = setcookie('ai_translate_lang', $lang, [ 'expires' => $expire, 'path' => '/', 'domain' => $domain, // Always set domain (even if empty) for consistency 'secure' => $secure, 'httponly' => false, // Allow JavaScript to read cookie as fallback 'samesite' => 'Lax', // Lax works better with redirects than None ]); // Fallback: if domain attribute failed (some browsers reject), try without domain if (!$setCookieResult) { $setCookieResult = setcookie('ai_translate_lang', $lang, [ 'expires' => $expire, 'path' => '/', 'secure' => $secure, 'httponly' => false, 'samesite' => 'Lax', ]); } } else { $setCookieResult = setcookie('ai_translate_lang', $lang, $expire, '/', $domain, $secure, false); if (!$setCookieResult) { $setCookieResult = setcookie('ai_translate_lang', $lang, $expire, '/', '', $secure, false); } } // Always update $_COOKIE immediately so it's available for the rest of the request $_COOKIE['ai_translate_lang'] = $lang; } /** * Current language (cached). * * @return string|null */ public static function current() { return self::detect(); } /** * Reset cached language detection (force re-detection on next call). */ public static function reset() { self::$current = null; } /** * Explicitly set the current language (bypass detection). * * Also syncs WordPress text direction so is_rtl() / body_class('rtl') / * language_attributes() and theme rtl.css loading work for ar/he. * * @param string|null $lang * @return string|null */ public static function set_current($lang) { self::$current = $lang !== null ? strtolower(sanitize_key((string) $lang)) : null; self::sync_wp_text_direction(self::$current); return self::$current; } /** * Force WP_Locale text_direction to match the active AI Translate language. * * Filtering `locale` alone is not enough: without an installed language pack * `_x( 'ltr', 'text direction' )` stays LTR, so is_rtl() remains false and * themes never load rtl.css / never get body.rtl — leaving only a bare * dir="rtl" on from the output buffer (half-applied RTL). * * Also syncs WP_Styles::$text_direction: that object is constructed early * (before the URL language is known) and wp_print_styles() uses *its* copy, * not is_rtl(), to decide whether to print style-rtl.css / bootstrap-rtl.css. * * @param string|null $lang Language code. * @return void */ public static function sync_wp_text_direction($lang) { $dir = self::is_rtl($lang) ? 'rtl' : 'ltr'; $GLOBALS['text_direction'] = $dir; global $wp_locale; if (isset($wp_locale) && is_object($wp_locale) && property_exists($wp_locale, 'text_direction')) { $wp_locale->text_direction = $dir; } // Keep the style queueer's direction in sync so rtl "replace" stylesheets print. if (function_exists('wp_styles')) { $styles = wp_styles(); if (is_object($styles) && property_exists($styles, 'text_direction')) { $styles->text_direction = $dir; } } } /** * Default language. * * Falls back to WordPress site language and persists the choice * so the plugin works immediately after activation. * * @return string */ public static function default() { $default = AI_Translate_Core::default_language(); if ($default !== '') { return $default; } $locale = get_locale(); $default = substr($locale, 0, 2); $settings = AI_Translate_Core::settings(); if (!is_array($settings)) { $settings = []; } $settings['default_language'] = $default; update_option('ai_translate_settings', $settings); return $default; } /** * Whether a language code is written right-to-left. * Covers the RTL languages offered by AI_Translate_Core::get_available_languages() * (currently Arabic and Hebrew); extend when a new RTL language is added there. * * @param string|null $lang Language code (e.g. 'ar', 'he'). * @return bool */ public static function is_rtl($lang) { return in_array(strtolower(trim((string) $lang)), ['ar', 'he'], true); } /** * Language codes that use a non-Latin script. * Single source of truth for cache validation, retry detection, and content checks. * Keep in sync when adding a non-Latin language to get_available_languages(). * * @return array */ public static function non_latin_codes(): array { return ['zh', 'ja', 'ko', 'ar', 'he', 'th', 'ka', 'bg', 'el', 'hi', 'uk', 'ru', 'kk', 'mk', 'sr']; } /** * Whether a language code uses a non-Latin script. * * @param string|null $lang * @return bool */ public static function is_non_latin($lang): bool { return in_array(strtolower(trim((string) $lang)), self::non_latin_codes(), true); } /** * Determine whether current request must be exempt from translation and URL rewriting. * Rules: skip admin/AJAX/REST/feeds and skip when current language equals default. * * @return bool */ public static function is_exempt_request() { if (is_admin() || wp_doing_ajax() || wp_is_json_request() || is_feed()) { return true; } $lang = self::current(); $default = self::default(); if ($lang === null || $default === null) { return true; } return strtolower($lang) === strtolower($default); } /** * Should translate into the given language? * Only translates if language is enabled OR detectable (to prevent unauthorized translation). * * @param string|null $lang * @return bool */ public static function should_translate($lang) { $default = self::default(); if ($lang === null || $default === null) { return false; } // Don't translate default language if (strtolower($lang) === strtolower($default)) { return false; } // Only translate if language is enabled OR detectable (prevent unauthorized translation) $enabled = self::enabled(); $detectable = self::detectable(); $langLower = strtolower($lang); $isEnabled = in_array($langLower, array_map('strtolower', $enabled), true); $isDetectable = in_array($langLower, array_map('strtolower', $detectable), true); // Only translate if language is explicitly enabled or detectable return $isEnabled || $isDetectable; } /** * Enabled languages for switcher. * * @return string[] */ public static function enabled() { return array_values(array_unique(array_map('strval', AI_Translate_Core::enabled_languages()))); } /** * Detectable languages for auto-detection. * * @return string[] */ public static function detectable() { return array_values(array_unique(array_map('strval', AI_Translate_Core::detectable_languages()))); } }