Class: Fastlane::Helper::Providers::DeepLProvider
- Inherits:
-
BaseProvider
- Object
- BaseProvider
- Fastlane::Helper::Providers::DeepLProvider
- 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
Class Method Summary collapse
-
.display_name ⇒ String
Returns the human-readable display name for the provider.
-
.optional_params ⇒ Hash
Returns a hash of optional parameter definitions for this provider.
-
.provider_name ⇒ String
Returns the provider identifier string.
-
.required_credentials ⇒ Array<Symbol>
Returns the list of required credential symbols for this provider.
Instance Method Summary collapse
-
#initialize(params) ⇒ DeepLProvider
constructor
Initializes the DeepL provider with configuration parameters.
-
#translate(text, source_locale, target_locale, glossary_terms: {}) ⇒ String?
Translates text from source locale to target locale using DeepL’s API.
-
#validate_config! ⇒ void
Validates the provider configuration.
Methods inherited from BaseProvider
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.
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_name ⇒ String
Returns the human-readable display name for the provider.
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_params ⇒ Hash
Returns a hash of optional parameter definitions for this provider.
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_name ⇒ String
Returns the provider identifier string.
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_credentials ⇒ Array<Symbol>
Returns the list of required credential symbols for this provider.
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.
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 = {} # Add formality if specified (not available for all languages) formality = @params[:formality].to_s.strip [: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 [:context] = context_parts.join(". ") unless context_parts.empty? # Make API call result = DeepL.translate(text, source_lang, target_lang, ) enforce_android_limit(result.text) rescue DeepL::Exceptions::RequestError => e UI.error "DeepL API error: #{e.}" nil rescue StandardError => e UI.error "DeepL provider error: #{e.}" 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 |