15, 'headers' => array( 'Authorization' => 'DeepL-Auth-Key ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => json_encode( array( 'text' => array( $text ), 'target_lang' => strtoupper( $target_lang ), ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['translations'][0]['text'] ) ) { return $body['translations'][0]['text']; } return new WP_Error( 'api_error', isset( $body['message'] ) ? $body['message'] : __( 'DeepL compilation failed.', 'af-translate' ) ); } /** * 2. GOOGLE TRANSLATE v2 API LOOP */ private static function call_google( $text, $target_lang, $settings ) { $api_key = isset( $settings['google_key'] ) ? trim( $settings['google_key'] ) : ''; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'Google Translate API key is empty.', 'af-translate' ) ); } $url = 'https://translation.googleapis.com/language/translate/v2?key=' . $api_key; $response = wp_remote_post( $url, array( 'timeout' => 15, 'body' => array( 'q' => $text, 'target' => $target_lang, 'format' => 'text', ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['data']['translations'][0]['translatedText'] ) ) { return html_entity_decode( $body['data']['translations'][0]['translatedText'], ENT_QUOTES, 'UTF-8' ); } return new WP_Error( 'api_error', isset( $body['error']['message'] ) ? $body['error']['message'] : __( 'Google Translate request failed.', 'af-translate' ) ); } /** * 3. LECTO AI API LOOP */ private static function call_lecto( $text, $target_lang, $settings ) { $api_key = isset( $settings['lecto_key'] ) ? trim( $settings['lecto_key'] ) : ''; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'Lecto AI API key is empty.', 'af-translate' ) ); } $endpoint = 'https://api.lecto.ai/v1/translate/text'; $response = wp_remote_post( $endpoint, array( 'timeout' => 15, 'headers' => array( 'X-API-Key' => $api_key, 'Content-Type' => 'application/json', ), 'body' => json_encode( array( 'texts' => array( $text ), 'to' => array( $target_lang ), ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['translations'][0]['to'][0]['translated'][0] ) ) { return $body['translations'][0]['to'][0]['translated'][0]; } return new WP_Error( 'api_error', isset( $body['message'] ) ? $body['message'] : __( 'Lecto AI execution payload failed.', 'af-translate' ) ); } /** * 4. MICROSOFT TRANSLATOR v3 API LOOP */ private static function call_microsoft( $text, $target_lang, $settings ) { $api_key = isset( $settings['microsoft_key'] ) ? trim( $settings['microsoft_key'] ) : ''; $region = isset( $settings['microsoft_region'] ) ? trim( $settings['microsoft_region'] ) : 'global'; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'Microsoft Translator API key is empty.', 'af-translate' ) ); } $url = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=' . $target_lang; $response = wp_remote_post( $url, array( 'timeout' => 15, 'headers' => array( 'Ocp-Apim-Subscription-Key' => $api_key, 'Ocp-Apim-Subscription-Region' => $region, 'Content-Type' => 'application/json', ), 'body' => json_encode( array( array( 'Text' => $text ), ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body[0]['translations'][0]['text'] ) ) { return $body[0]['translations'][0]['text']; } return new WP_Error( 'api_error', isset( $body['error']['message'] ) ? $body['error']['message'] : __( 'Microsoft Translator verification failed.', 'af-translate' ) ); } /** * 5. OPENAI CHAT COMPLETIONS API LOOP */ private static function call_openai( $text, $target_locale, $settings ) { $api_key = isset( $settings['openai_key'] ) ? trim( $settings['openai_key'] ) : ''; $model = ! empty( $settings['openai_model'] ) ? trim( $settings['openai_model'] ) : 'gpt-4o-mini'; $prompt = ! empty( $settings['openai_custom_prompt'] ) ? $settings['openai_custom_prompt'] : 'Translate this WordPress string to {target_language}. Keep code/HTML placeholders identical.'; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'OpenAI API key is empty.', 'af-translate' ) ); } $resolved_prompt = str_replace( '{target_language}', $target_locale, $prompt ); $endpoint = 'https://api.openai.com/v1/chat/completions'; $response = wp_remote_post( $endpoint, array( 'timeout' => 20, 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => json_encode( array( 'model' => sanitize_text_field( $model ), 'temperature' => 0.2, 'messages' => array( array( 'role' => 'system', 'content' => $resolved_prompt ), array( 'role' => 'user', 'content' => $text ), ), ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['choices'][0]['message']['content'] ) ) { return trim( $body['choices'][0]['message']['content'], '"\'' ); } return new WP_Error( 'api_error', isset( $body['error']['message'] ) ? $body['error']['message'] : __( 'OpenAI response compilation failed.', 'af-translate' ) ); } /** * 6. ANTHROPIC (CLAUDE) API LOOP */ private static function call_anthropic( $text, $target_locale, $settings ) { $api_key = isset( $settings['anthropic_key'] ) ? trim( $settings['anthropic_key'] ) : ''; $model = ! empty( $settings['anthropic_model'] ) ? trim( $settings['anthropic_model'] ) : 'claude-3-5-sonnet-latest'; $prompt = ! empty( $settings['anthropic_custom_prompt'] ) ? $settings['anthropic_custom_prompt'] : 'Translate this WordPress string to {target_language}. Keep code/HTML placeholders identical.'; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'Anthropic API key is empty.', 'af-translate' ) ); } $resolved_prompt = str_replace( '{target_language}', $target_locale, $prompt ); $endpoint = 'https://api.anthropic.com/v1/messages'; $response = wp_remote_post( $endpoint, array( 'timeout' => 25, 'headers' => array( 'x-api-key' => $api_key, 'anthropic-version' => '2023-06-01', 'content-type' => 'application/json', ), 'body' => json_encode( array( 'model' => sanitize_text_field( $model ), 'max_tokens' => 2000, 'temperature' => 0.1, 'system' => $resolved_prompt, 'messages' => array( array( 'role' => 'user', 'content' => array( array( 'type' => 'text', 'text' => $text ) ) ) ), ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['content'][0]['text'] ) ) { return trim( $body['content'][0]['text'] ); } return new WP_Error( 'api_error', isset( $body['error']['message'] ) ? $body['error']['message'] : __( 'Anthropic payload execution failed.', 'af-translate' ) ); } /** * 7. XAI (GROK) API LOOP */ private static function call_grok( $text, $target_locale, $settings ) { $api_key = isset( $settings['grok_key'] ) ? trim( $settings['grok_key'] ) : ''; $model = ! empty( $settings['grok_model'] ) ? trim( $settings['grok_model'] ) : 'grok-4'; $prompt = ! empty( $settings['grok_custom_prompt'] ) ? $settings['grok_custom_prompt'] : 'Translate this WordPress string to {target_language}. Keep code/HTML placeholders identical.'; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'Grok API key is empty.', 'af-translate' ) ); } $resolved_prompt = str_replace( '{target_language}', $target_locale, $prompt ); $endpoint = 'https://api.x.ai/v1/chat/completions'; $response = wp_remote_post( $endpoint, array( 'timeout' => 25, 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => json_encode( array( 'model' => sanitize_text_field( $model ), 'temperature' => 0.2, 'messages' => array( array( 'role' => 'system', 'content' => $resolved_prompt ), array( 'role' => 'user', 'content' => $text ), ), ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['choices'][0]['message']['content'] ) ) { return trim( $body['choices'][0]['message']['content'], '"\'' ); } return new WP_Error( 'api_error', isset( $body['error']['message'] ) ? $body['error']['message'] : __( 'Grok response validation failed.', 'af-translate' ) ); } /** * 8. GOOGLE GEMINI API LOOP */ private static function call_gemini( $text, $target_locale, $settings ) { $api_key = isset( $settings['gemini_key'] ) ? trim( $settings['gemini_key'] ) : ''; $model = ! empty( $settings['gemini_model'] ) ? trim( $settings['gemini_model'] ) : 'gemini-2.5-flash'; $prompt = ! empty( $settings['gemini_custom_prompt'] ) ? $settings['gemini_custom_prompt'] : 'Translate this WordPress string to {target_language}. Keep code/HTML placeholders identical.'; if ( empty( $api_key ) ) { return new WP_Error( 'missing_key', __( 'Gemini API key is empty.', 'af-translate' ) ); } $resolved_prompt = str_replace( '{target_language}', $target_locale, $prompt ); $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . sanitize_text_field( $model ) . ':generateContent?key=' . $api_key; $response = wp_remote_post( $endpoint, array( 'timeout' => 20, 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => json_encode( array( 'contents' => array( array( 'parts' => array( array( 'text' => $text ) ) ) ), 'systemInstruction' => array( 'parts' => array( array( 'text' => $resolved_prompt ) ) ), 'generationConfig' => array( 'temperature' => 0.2 ) ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['candidates'][0]['content']['parts'][0]['text'] ) ) { return trim( $body['candidates'][0]['content']['parts'][0]['text'] ); } return new WP_Error( 'api_error', isset( $body['error']['message'] ) ? $body['error']['message'] : __( 'Gemini execution loop failed.', 'af-translate' ) ); } }