Class: BetterTranslate::Providers::AnthropicProvider

Inherits:
BaseHttpProvider show all
Defined in:
lib/better_translate/providers/anthropic_provider.rb

Overview

Anthropic Claude translation provider

Uses claude-haiku-4-5 model for fast, efficient translations.

Examples:

Basic usage

config = Configuration.new
config.anthropic_key = ENV['ANTHROPIC_API_KEY']
provider = AnthropicProvider.new(config)
result = provider.translate_text("Hello", "it", "Italian")
#=> "Ciao"

Constant Summary collapse

API_URL =

Anthropic API endpoint

"https://api.anthropic.com/v1/messages"
MODEL =

Model to use for translations

"claude-haiku-4-5"
API_VERSION =

API version

"2023-06-01"

Instance Attribute Summary

Attributes inherited from BaseHttpProvider

#cache, #config, #rate_limiter

Instance Method Summary collapse

Methods inherited from BaseHttpProvider

#build_cache_key, #calculate_backoff, #handle_response, #http_client, #initialize, #log_retry, #make_request, #with_cache

Constructor Details

This class inherits a constructor from BetterTranslate::Providers::BaseHttpProvider

Instance Method Details

#build_messages(text, target_lang_name) ⇒ Hash (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build messages for Anthropic API

Parameters:

  • text (String)

    Text to translate

  • target_lang_name (String)

    Target language name

Returns:

  • (Hash)

    Messages hash



81
82
83
84
85
86
87
88
89
90
# File 'lib/better_translate/providers/anthropic_provider.rb', line 81

def build_messages(text, target_lang_name)
  system_message = build_system_message(target_lang_name)

  {
    system: system_message,
    messages: [
      { role: "user", content: text }
    ]
  }
end

#build_system_message(target_lang_name) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build system message with optional context

Parameters:

  • target_lang_name (String)

    Target language name

Returns:

  • (String)

    System message



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/better_translate/providers/anthropic_provider.rb', line 98

def build_system_message(target_lang_name)
  base_message = "You are a professional translator. Translate the following text to #{target_lang_name}. " \
                 "Return ONLY the translated text, without any explanations or additional text. " \
                 "Words like VARIABLE_0, VARIABLE_1, etc. are placeholders and must be kept unchanged in the translation."

  if config.translation_context && !config.translation_context.empty?
    base_message += "\n\nContext: #{config.translation_context}"
  end

  base_message
end

#extract_translation(response) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract translation from API response

Parameters:

  • response (Faraday::Response)

    HTTP response

Returns:

  • (String)

    Translated text

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/better_translate/providers/anthropic_provider.rb', line 140

def extract_translation(response)
  parsed = JSON.parse(response.body)
  translation = parsed.dig("content", 0, "text")

  raise TranslationError, "No translation in response" if translation.nil? || translation.empty?

  translation.strip
rescue JSON::ParserError => e
  raise TranslationError.new(
    "Failed to parse Anthropic response",
    context: { error: e.message, body: response.body }
  )
end

#make_messages_request(message_data) ⇒ Faraday::Response (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Make messages request to Anthropic API

Parameters:

  • message_data (Hash)

    Message data with system and messages

Returns:

  • (Faraday::Response)

    HTTP response



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/better_translate/providers/anthropic_provider.rb', line 116

def make_messages_request(message_data)
  body = {
    model: MODEL,
    max_tokens: 1024,
    system: message_data[:system],
    messages: message_data[:messages]
  }

  headers = {
    "Content-Type" => "application/json",
    "x-api-key" => config.anthropic_key || "",
    "anthropic-version" => API_VERSION
  }

  make_request(:post, API_URL, body: body, headers: headers)
end

#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>

Translate multiple texts in a batch

Examples:

provider.translate_batch(["Hello", "World"], "it", "Italian")
#=> ["Ciao", "Mondo"]

Parameters:

  • texts (Array<String>)

    Texts to translate

  • target_lang_code (String)

    Target language code

  • target_lang_name (String)

    Target language name

Returns:

  • (Array<String>)

    Translated texts



68
69
70
# File 'lib/better_translate/providers/anthropic_provider.rb', line 68

def translate_batch(texts, target_lang_code, target_lang_name)
  texts.map { |text| translate_text(text, target_lang_code, target_lang_name) }
end

#translate_text(text, target_lang_code, target_lang_name) ⇒ String

Translate a single text

Examples:

provider.translate_text("Hello world", "it", "Italian")
#=> "Ciao mondo"

Parameters:

  • text (String)

    Text to translate

  • target_lang_code (String)

    Target language code

  • target_lang_name (String)

    Target language name

Returns:

  • (String)

    Translated text

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/better_translate/providers/anthropic_provider.rb', line 39

def translate_text(text, target_lang_code, target_lang_name)
  Validator.validate_text!(text)
  Validator.validate_language_code!(target_lang_code)

  cache_key = build_cache_key(text, target_lang_code)

  with_cache(cache_key) do
    messages = build_messages(text, target_lang_name)
    response = make_messages_request(messages)
    extract_translation(response)
  end
rescue ApiError => e
  raise TranslationError.new(
    "Failed to translate text with Anthropic: #{e.message}",
    context: { text: text, target_lang: target_lang_code, original_error: e }
  )
end