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

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

- Page: https://pluginprobe.com/plugins/ai-builder/2.2.3/code/includes/class-translation-switcher.php
- Raw: https://pluginprobe.com/plugins/ai-builder/2.2.3/raw/includes/class-translation-switcher.php
- Modified: 2025-11-30T10:16:52+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.2.3/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;
    }

    /**
     * Render callback for the Language Switcher block.
     */
    public function render_block($attributes = array(), $content = '', $block = null)
    {
        if (!$this->manager->is_switcher_enabled()) {
            return '';
        }

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

        $settings   = AIBUI_Translation_Settings::get_settings();
        $supported  = AIBUI_Translation_Handler::SUPPORTED_LANGUAGES;
        $current    = $this->manager->get_current_language();
        $select_id  = 'aibui-lang-select-' . wp_generate_uuid4();

        $bg_color   = !empty($settings['switcher_bg']) ? $settings['switcher_bg'] : '#5686c9';
        $text_color = !empty($settings['switcher_text']) ? $settings['switcher_text'] : '#ffffff';
        $min_height = !empty($settings['switcher_min_height']) ? (int) $settings['switcher_min_height'] : 30;
        $min_width  = !empty($settings['switcher_min_width']) ? (int) $settings['switcher_min_width'] : 90;
        $alpha      = isset($settings['switcher_alpha']) ? (float) $settings['switcher_alpha'] : 0.9;
        $alpha      = max(0, min(1, $alpha));

        $bg_rgba = sprintf('rgba(17, 24, 39, %.2f)', $alpha);
        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);
        }

        $attr_bg = isset($attributes['backgroundColor']) && $attributes['backgroundColor'] !== ''
            ? sanitize_text_field($attributes['backgroundColor'])
            : '';
        $attr_text = isset($attributes['textColor']) && $attributes['textColor'] !== ''
            ? sanitize_text_field($attributes['textColor'])
            : '';
        $attr_min_width = isset($attributes['minWidth']) && is_numeric($attributes['minWidth'])
            ? (int) $attributes['minWidth']
            : null;
        $attr_min_height = isset($attributes['minHeight']) && is_numeric($attributes['minHeight'])
            ? (int) $attributes['minHeight']
            : null;
        $default_radius = 22;
        $attr_radius = isset($attributes['borderRadius']) && is_numeric($attributes['borderRadius'])
            ? (int) $attributes['borderRadius']
            : null;

        $style_vars = array(
            '--aibui-switcher-bg' => $attr_bg !== '' ? $attr_bg : $bg_rgba,
            '--aibui-switcher-text' => $attr_text !== '' ? $attr_text : $text_color,
            '--aibui-switcher-min-height' => ($attr_min_height !== null ? $attr_min_height : $min_height) . 'px',
            '--aibui-switcher-min-width' => ($attr_min_width !== null ? $attr_min_width : $min_width) . 'px',
            '--aibui-switcher-radius' => ($attr_radius !== null ? $attr_radius : $default_radius) . 'px',
        );

        $style_attr = '';
        foreach ($style_vars as $var => $value) {
            if ($value !== '' && $value !== null) {
                $style_attr .= $var . ':' . $value . ';';
            }
        }

        $classes = 'aibui-lang-switcher-block aibui-lang-switcher-block--no-label';

        $wrapper_attributes = function_exists('get_block_wrapper_attributes')
            ? get_block_wrapper_attributes(array(
                'class' => trim($classes),
                'style' => $style_attr,
            ))
            : sprintf('class="%s" style="%s"', esc_attr(trim($classes)), esc_attr($style_attr));

        ob_start();
        ?>
        <div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
            <form method="get" class="aibui-lang-switcher__form">
                <label for="<?php echo esc_attr($select_id); ?>" class="screen-reader-text">
                    <?php esc_html_e('Select language', 'ai-builder'); ?>
                </label>
                <select id="<?php echo esc_attr($select_id); ?>" 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 $this->render_preserved_query_fields(); ?>
            </form>
        </div>
        <?php
        return ob_get_clean();
    }

    private function get_ordered_languages()
    {
        $available = $this->manager->get_available_languages();
        $default   = $this->manager->get_default_language();

        $available = array_values(array_unique(array_filter((array) $available)));

        if ($default && in_array($default, $available, true)) {
            $available = array_values(array_diff($available, array($default)));
            array_unshift($available, $default);
        } elseif ($default) {
            array_unshift($available, $default);
        }

        return $available;
    }

    private function render_preserved_query_fields()
    {
        $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(wp_unslash($val)));
                }
            } else {
                printf('<input type="hidden" name="%s" value="%s" />', esc_attr($key), esc_attr(wp_unslash($value)));
            }
        }
    }
}


```
