Class: Fastlane::Helper::Providers::DeepLProvider

Inherits:
BaseProvider
  • Object
show all
Defined in:
lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb

Overview

Provider implementation for DeepL translation API. DeepL is a purpose-built neural machine translation service (not an LLM) known for high-quality European language translations.

Constant Summary collapse

DEFAULT_TIMEOUT =

Default request timeout in seconds

30
FREE_KEY_SUFFIX =

Free DeepL API keys end with ‘:fx’ and use a different endpoint

':fx'.freeze
API_HOST_PAID =

API endpoints for different key types

'https://api.deepl.com'.freeze
API_HOST_FREE =
'https://api-free.deepl.com'.freeze

Constants inherited from BaseProvider

BaseProvider::ANDROID_CHAR_LIMIT

Instance Attribute Summary

Attributes inherited from BaseProvider

#config_errors, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseProvider

#valid?

Constructor Details

#initialize(params) ⇒ DeepLProvider

Initializes the DeepL provider with configuration parameters. Configures the DeepL gem with the API authentication key. Automatically detects free vs paid keys and uses the appropriate endpoint.

Parameters:

  • params (Hash)

    Configuration parameters for the provider



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 65

def initialize(params)
  super(params)

  api_key = params[:api_token].to_s
  return if api_key.nil? || api_key.empty?

  host = api_key.end_with?(FREE_KEY_SUFFIX) ? API_HOST_FREE : API_HOST_PAID

  DeepL.configure do |config|
    config.auth_key = api_key
    config.host = host
  end
end

Class Method Details

.display_nameString

Returns the human-readable display name for the provider.

Returns:

  • (String)

    Human-readable name



24
25
26
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 24

def self.display_name
  'DeepL'
end

.optional_paramsHash

Returns a hash of optional parameter definitions for this provider.

Returns:

  • (Hash)

    Optional parameter definitions



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 38

def self.optional_params
  {
    request_timeout: {
      default: DEFAULT_TIMEOUT,
      env: 'DEEPL_REQUEST_TIMEOUT',
      description: 'Request timeout in seconds'
    },
    formality: {
      default: 'default',
      env: 'DEEPL_FORMALITY',
      description: 'Formality level: default, more, or less'
    }
  }
end

.provider_nameString

Returns the provider identifier string.

Returns:

  • (String)

    Provider identifier



17
18
19
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 17

def self.provider_name
  'deepl'
end

.required_credentialsArray<Symbol>

Returns the list of required credential symbols for this provider.

Returns:

  • (Array<Symbol>)

    Array of required credential keys



31
32
33
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 31

def self.required_credentials
  [:api_token]
end

Instance Method Details

#translate(text, source_locale, target_locale, glossary_terms: {}) ⇒ String?

Translates text from source locale to target locale using DeepL’s API.

Parameters:

  • text (String)

    The text to translate

  • source_locale (String)

    Source language code (e.g., ‘en-US’, ‘de-DE’)

  • target_locale (String)

    Target language code (e.g., ‘es’, ‘fr’)

  • glossary_terms (Hash) (defaults to: {})

    Optional glossary { source_term => target_translation }

Returns:

  • (String, nil)

    Translated text or nil on error



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 94

def translate(text, source_locale, target_locale, glossary_terms: {})
  source_lang = normalize_source_locale(source_locale)
  target_lang = normalize_target_locale(target_locale)

  # Build options hash
  options = {}

  # Add formality if specified (not available for all languages)
  formality = @params[:formality].to_s.strip
  options[:formality] = formality unless formality.empty? || formality == 'default'

  # Build context from user-provided context and glossary terms
  context_parts = []
  context_parts << @params[:context] if @params[:context] && !@params[:context].empty?

  if glossary_terms && !glossary_terms.empty?
    glossary_str = glossary_terms.map { |s, t| "#{s} = #{t}" }.join("; ")
    context_parts << "Glossary: #{glossary_str}"
  end

  options[:context] = context_parts.join(". ") unless context_parts.empty?

  # Make API call
  result = DeepL.translate(text, source_lang, target_lang, options)

  enforce_android_limit(result.text)
rescue DeepL::Exceptions::RequestError => e
  UI.error "DeepL API error: #{e.message}"
  nil
rescue StandardError => e
  UI.error "DeepL provider error: #{e.message}"
  nil
end

#validate_config!void

This method returns an undefined value.

Validates the provider configuration. Ensures that the required api_token credential is present.



83
84
85
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/deepl_provider.rb', line 83

def validate_config!
  require_credential(:api_token)
end