# ai-builder/2.1.7/includes/class-translation-switcher.php

AI Builder – Generate pages, blocks, images &amp; translate with AI, version 2.1.7. 160 lines.

- Page: https://pluginprobe.com/plugins/ai-builder/2.1.7/code/includes/class-translation-switcher.php
- Raw: https://pluginprobe.com/plugins/ai-builder/2.1.7/raw/includes/class-translation-switcher.php
- Modified: 2025-11-27T16:24:04+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ai-builder/2.1.7/code/includes/class-translation-switcher.php#L10-L20`.

```php
<?php

if (!defined('ABSPATH')) {
    exit;
}

class AIBUI_Translation_Switcher
{
    private $manager;

    public function __construct(AIBUI_Translation_Manager $manager)
    {
        $this->manager = $manager;

        add_action('wp_footer', array($this, 'render_switcher'), 20);
    }

    public function render_switcher()
    {
        if (!$this->manager->is_switcher_enabled()) {
            return;
        }

        $languages = $this->manager->get_available_languages();
        if (count($languages) <= 1) {
            return;
        }

        $current = $this->manager->get_current_language();
        $supported = AIBUI_Translation_Handler::SUPPORTED_LANGUAGES;
        $settings     = AIBUI_Translation_Settings::get_settings();
        $bg_color     = !empty($settings['switcher_bg']) ? $settings['switcher_bg'] : '#111827';
        $text_color   = !empty($settings['switcher_text']) ? $settings['switcher_text'] : '#ffffff';
        $min_height   = !empty($settings['switcher_min_height']) ? (int) $settings['switcher_min_height'] : 32;
        $min_width    = !empty($settings['switcher_min_width']) ? (int) $settings['switcher_min_width'] : 140;
        $alpha        = isset($settings['switcher_alpha']) ? (float) $settings['switcher_alpha'] : 0.9;
        if ($alpha < 0) {
            $alpha = 0;
        } elseif ($alpha > 1) {
            $alpha = 1;
        }

        // Convert hex to rgba with configurable alpha for transparency.
        $bg_rgba = sprintf('rgba(17, 24, 39, %.2f)', $alpha); // sensible default
        if (preg_match('/^#([0-9a-fA-F]{6})$/', $bg_color, $m)) {
            $hex = $m[1];
            $r   = hexdec(substr($hex, 0, 2));
            $g   = hexdec(substr($hex, 2, 2));
            $b   = hexdec(substr($hex, 4, 2));
            $bg_rgba = sprintf('rgba(%d, %d, %d, %.2f)', $r, $g, $b, $alpha);
        }

        $current_url = $this->get_current_url();
        ?>
        <div class="aibui-lang-switcher">
            <form method="get" class="aibui-lang-switcher__form">
                <label for="aibui-lang-select" class="screen-reader-text"><?php esc_html_e('Select language', 'ai-builder'); ?></label>
                <select id="aibui-lang-select" name="ai_lang" onchange="this.form.submit()">
                    <?php foreach ($languages as $lang_code) :
                        $label = $supported[$lang_code] ?? strtoupper($lang_code);
                        ?>
                        <option value="<?php echo esc_attr($lang_code); ?>" <?php selected($current, $lang_code); ?>>
                            <?php echo esc_html($label); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <?php
                // Preserve other query vars
                $query = $_GET;
                unset($query['ai_lang']);
                foreach ($query as $key => $value) {
                    if (is_array($value)) {
                        foreach ($value as $val) {
                            printf('<input type="hidden" name="%s[]" value="%s" />', esc_attr($key), esc_attr($val));
                        }
                    } else {
                        printf('<input type="hidden" name="%s" value="%s" />', esc_attr($key), esc_attr($value));
                    }
                }
                ?>
            </form>
        </div>
        <style>
            .aibui-lang-switcher {
                position: fixed;
                top: 0;
                right: 0;
                background: <?php echo esc_attr($bg_rgba); ?>;
                padding: 0px 14px 10px 14px;
                border-radius: 0 0 0 16px; /* only bottom-left corner rounded */
                z-index: 9999;
                min-height: <?php echo (int) $min_height; ?>px;
                min-width: <?php echo (int) $min_width; ?>px;
                display: flex;
                align-items: center;
                justify-content: center;
                cursor: pointer;
            }

            /* When logged-in admin bar is visible, offset the switcher */
            body.admin-bar .aibui-lang-switcher {
                top: 32px;
            }

            @media screen and (max-width: 782px) {
                body.admin-bar .aibui-lang-switcher {
                    top: 46px;
                }
            }

            .aibui-lang-switcher select {
                background: transparent;
                color: <?php echo esc_attr($text_color); ?>;
                border: none;
                border-radius: 0;
                padding: 4px 8px;
                font-weight: 600;
                outline: none;
                box-shadow: none;
                cursor: pointer;
            }

            .aibui-lang-switcher select option {
                color: #000;
            }
        </style>
        <script>
        (function(){
            var wrapper = document.querySelector('.aibui-lang-switcher');
            var select = document.getElementById('aibui-lang-select');
            if (wrapper && select) {
                wrapper.addEventListener('click', function(e) {
                    if (e.target !== select) {
                        // Programmatically open the select dropdown
                        if (typeof select.showPicker === 'function') {
                            select.showPicker();
                        } else {
                            // Fallback: focus and simulate mousedown for older browsers
                            select.focus();
                            var evt = new MouseEvent('mousedown', { bubbles: true, cancelable: true, view: window });
                            select.dispatchEvent(evt);
                        }
                    }
                });
            }
        })();
        </script>
        <?php
    }

    private function get_current_url()
    {
        $scheme = is_ssl() ? 'https' : 'http';
        $host = $_SERVER['HTTP_HOST'] ?? '';
        $uri = $_SERVER['REQUEST_URI'] ?? '';
        return $scheme . '://' . $host . $uri;
    }
}


```
