class TRP_OpenRouter_Machine_Translator extends TRP_Machine_Translator { protected $api_key; protected $model; protected $settings; public function __construct($settings) { parent::__construct($settings); $this->settings = $settings; $this->api_key = isset($settings['trp_machine_translation_settings']['openrouter-api-key']) ? $settings['trp_machine_translation_settings']['openrouter-api-key'] : ''; $this->model = isset($settings['trp_machine_translation_settings']['openrouter-model']) ? $settings['trp_machine_translation_settings']['openrouter-model'] : ''; } public function get_api_key() { return isset($this->settings['trp_machine_translation_settings']['openrouter-api-key']) ? $this->settings['trp_machine_translation_settings']['openrouter-api-key'] : false; } public function get_model() { return isset($this->settings['trp_machine_translation_settings']['openrouter-model']) ? $settings['trp_machine_translation_settings']['openrouter-model'] : false; } public function test_request() { $is_error = false; $message = ''; $api_key = $this->get_api_key(); if (empty($api_key)) { return array( 'error' => true, 'message' => __('Please enter an OpenRouter API key.', 'translatepress-ai-addon') ); } // Make a test request to the OpenRouter API $response = wp_remote_get('https://openrouter.ai/api/v1/models', array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'HTTP-Referer' => get_site_url(), 'X-Title' => get_bloginfo('name') ) )); if (is_wp_error($response)) { $is_error = true; $message = $response->get_error_message(); } else { $status_code = wp_remote_retrieve_response_code($response); $body = json_decode(wp_remote_retrieve_body($response), true); if ($status_code !== 200) { $is_error = true; $message = isset($body['error']) ? $body['error'] : __('Invalid API key.', 'translatepress-ai-addon'); } else { $message = __('API key is valid.', 'translatepress-ai-addon'); } } return array( 'error' => $is_error, 'message' => $message ); } public function check_api_key_validity() { return $this->test_request(); } public function translate_array($strings, $target_language_code, $source_language_code = null) { $translation_engine = isset($this->settings['trp_machine_translation_settings']['translation-engine']) ? $this->settings['trp_machine_translation_settings']['translation-engine'] : ''; if ('openrouter' !== $translation_engine) { return array(); } $api_key = $this->get_api_key(); $model = $this->get_model(); if (empty($api_key) || empty($model)) { return array(); } $translated_strings = array(); $messages = array(); // System message to set context and translation instructions $messages[] = array( "role" => "system", "content" => "Act as a translator from $source_language_code to $target_language_code. Only include the translation and nothing else." ); // Add each string to translate as a user message foreach ($strings as $key => $string) { $messages[] = array( "role" => "user", "content" => $string ); } // Make request to OpenRouter API $response = wp_remote_post('https://openrouter.ai/api/v1/chat/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, 'HTTP-Referer' => get_site_url(), 'X-Title' => get_bloginfo('name') ), 'body' => json_encode(array( 'model' => $model, 'messages' => $messages )), 'timeout' => 200 )); if (is_wp_error($response)) { return array(); } $response_code = wp_remote_retrieve_response_code($response); if ($response_code !== 200) { return array(); } $body = json_decode(wp_remote_retrieve_body($response), true); if (!isset($body['choices']) || !is_array($body['choices'])) { return array(); } // Process each translation $i = 0; foreach ($strings as $key => $string) { if (isset($body['choices'][$i]['message']['content'])) { $translated_strings[$key] = $body['choices'][$i]['message']['content']; } else { $translated_strings[$key] = $string; } $i++; } return $translated_strings; } public function get_supported_languages() { // OpenRouter supports all languages return array('*'); } public function get_engine_specific_language_codes($languages) { return $languages; } public function check_formality() { // OpenRouter doesn't support formality yet return array(); } }