deepl-rb 3.8.0 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +71 -53
  3. data/.rubocop.yml +26 -0
  4. data/CHANGELOG.md +450 -17
  5. data/Gemfile +4 -0
  6. data/README.md +1119 -152
  7. data/Rakefile +18 -5
  8. data/VERSION +1 -1
  9. data/deepl-rb.gemspec +31 -3
  10. data/lib/deepl/admin_client.rb +183 -0
  11. data/lib/deepl/api.rb +30 -6
  12. data/lib/deepl/app_info.rb +20 -0
  13. data/lib/deepl/client.rb +969 -0
  14. data/lib/deepl/configuration.rb +56 -10
  15. data/lib/deepl/constants/admin_usage_group_by.rb +13 -0
  16. data/lib/deepl/constants/custom_tag_usage_aggregate_by.rb +13 -0
  17. data/lib/deepl/constants/document_status_code.rb +17 -0
  18. data/lib/deepl/constants/language_include.rb +17 -0
  19. data/lib/deepl/constants/language_resource_name.rb +21 -0
  20. data/lib/deepl/constants/tag_handling_version.rb +14 -0
  21. data/lib/deepl/deprecation.rb +42 -0
  22. data/lib/deepl/document_api.rb +45 -16
  23. data/lib/deepl/exceptions/bad_request.rb +1 -3
  24. data/lib/deepl/exceptions/connection_error.rb +37 -0
  25. data/lib/deepl/exceptions/document_not_ready_error.rb +17 -0
  26. data/lib/deepl/exceptions/document_translation_error.rb +17 -1
  27. data/lib/deepl/exceptions/error.rb +13 -1
  28. data/lib/deepl/exceptions/not_found.rb +8 -3
  29. data/lib/deepl/exceptions/not_supported.rb +6 -0
  30. data/lib/deepl/exceptions/request_error.rb +19 -2
  31. data/lib/deepl/exceptions/server_error.rb +7 -1
  32. data/lib/deepl/exceptions/validation_error.rb +14 -0
  33. data/lib/deepl/requests/admin/base.rb +94 -0
  34. data/lib/deepl/requests/admin/create_developer_key.rb +38 -0
  35. data/lib/deepl/requests/admin/custom_tag_usage_analytics.rb +38 -0
  36. data/lib/deepl/requests/admin/list_developer_keys.rb +28 -0
  37. data/lib/deepl/requests/admin/update_developer_key.rb +35 -0
  38. data/lib/deepl/requests/admin/usage_analytics.rb +36 -0
  39. data/lib/deepl/requests/base.rb +265 -43
  40. data/lib/deepl/requests/correct.rb +57 -0
  41. data/lib/deepl/requests/document/download.rb +11 -3
  42. data/lib/deepl/requests/document/get_status.rb +2 -2
  43. data/lib/deepl/requests/document/upload.rb +92 -10
  44. data/lib/deepl/requests/glossary/create.rb +6 -2
  45. data/lib/deepl/requests/glossary/language_pairs.rb +20 -4
  46. data/lib/deepl/requests/language_resources.rb +41 -0
  47. data/lib/deepl/requests/languages.rb +28 -4
  48. data/lib/deepl/requests/languages_for_resource.rb +54 -0
  49. data/lib/deepl/requests/multilingual_glossary/create.rb +37 -0
  50. data/lib/deepl/requests/multilingual_glossary/destroy.rb +36 -0
  51. data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +51 -0
  52. data/lib/deepl/requests/multilingual_glossary/entries.rb +66 -0
  53. data/lib/deepl/requests/multilingual_glossary/find.rb +35 -0
  54. data/lib/deepl/requests/multilingual_glossary/helpers.rb +63 -0
  55. data/lib/deepl/requests/multilingual_glossary/list.rb +39 -0
  56. data/lib/deepl/requests/multilingual_glossary/replace_dictionary.rb +41 -0
  57. data/lib/deepl/requests/multilingual_glossary/update.rb +40 -0
  58. data/lib/deepl/requests/rephrase.rb +5 -9
  59. data/lib/deepl/requests/spoken_terms/create.rb +37 -0
  60. data/lib/deepl/requests/spoken_terms/destroy.rb +36 -0
  61. data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +41 -0
  62. data/lib/deepl/requests/spoken_terms/entries.rb +46 -0
  63. data/lib/deepl/requests/spoken_terms/find.rb +35 -0
  64. data/lib/deepl/requests/spoken_terms/helpers.rb +67 -0
  65. data/lib/deepl/requests/spoken_terms/list.rb +35 -0
  66. data/lib/deepl/requests/spoken_terms/replace_term_list.rb +41 -0
  67. data/lib/deepl/requests/spoken_terms/update.rb +40 -0
  68. data/lib/deepl/requests/style_profile/base.rb +83 -0
  69. data/lib/deepl/requests/style_profile/create.rb +35 -0
  70. data/lib/deepl/requests/style_profile/destroy.rb +34 -0
  71. data/lib/deepl/requests/style_profile/find.rb +33 -0
  72. data/lib/deepl/requests/style_profile/list.rb +43 -0
  73. data/lib/deepl/requests/style_profile/update.rb +35 -0
  74. data/lib/deepl/requests/style_rule/create.rb +1 -1
  75. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +4 -2
  76. data/lib/deepl/requests/style_rule/destroy.rb +4 -2
  77. data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +5 -3
  78. data/lib/deepl/requests/style_rule/find.rb +4 -2
  79. data/lib/deepl/requests/style_rule/find_custom_instruction.rb +5 -3
  80. data/lib/deepl/requests/style_rule/helpers.rb +35 -0
  81. data/lib/deepl/requests/style_rule/list.rb +1 -1
  82. data/lib/deepl/requests/style_rule/update.rb +4 -2
  83. data/lib/deepl/requests/style_rule/update_configured_rules.rb +4 -2
  84. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +5 -3
  85. data/lib/deepl/requests/translate.rb +55 -20
  86. data/lib/deepl/requests/translation_memory/base.rb +44 -0
  87. data/lib/deepl/requests/translation_memory/create_export.rb +41 -0
  88. data/lib/deepl/requests/translation_memory/create_import.rb +53 -0
  89. data/lib/deepl/requests/translation_memory/destroy.rb +37 -0
  90. data/lib/deepl/requests/translation_memory/download_export.rb +39 -0
  91. data/lib/deepl/requests/translation_memory/find.rb +38 -0
  92. data/lib/deepl/requests/translation_memory/find_job.rb +38 -0
  93. data/lib/deepl/requests/translation_memory/list.rb +6 -18
  94. data/lib/deepl/requests/translation_memory/segments.rb +47 -0
  95. data/lib/deepl/requests/translation_memory/storage_base.rb +79 -0
  96. data/lib/deepl/requests/translation_memory/upload_file.rb +47 -0
  97. data/lib/deepl/requests/usage.rb +28 -1
  98. data/lib/deepl/requests/write_improvements.rb +35 -0
  99. data/lib/deepl/resources/admin_usage_report.rb +73 -0
  100. data/lib/deepl/resources/custom_tag_usage_report.rb +59 -0
  101. data/lib/deepl/resources/developer_key.rb +57 -0
  102. data/lib/deepl/resources/document_handle.rb +24 -25
  103. data/lib/deepl/resources/document_translation_status.rb +23 -4
  104. data/lib/deepl/resources/language.rb +10 -2
  105. data/lib/deepl/resources/language_resource.rb +57 -0
  106. data/lib/deepl/resources/language_support.rb +56 -0
  107. data/lib/deepl/resources/multilingual_glossary.rb +112 -0
  108. data/lib/deepl/resources/spoken_terms.rb +61 -0
  109. data/lib/deepl/resources/style_profile.rb +44 -0
  110. data/lib/deepl/resources/style_rule.rb +3 -0
  111. data/lib/deepl/resources/text.rb +18 -2
  112. data/lib/deepl/resources/translation_memory.rb +10 -1
  113. data/lib/deepl/resources/translation_memory_export.rb +37 -0
  114. data/lib/deepl/resources/translation_memory_import.rb +29 -0
  115. data/lib/deepl/resources/translation_memory_job.rb +162 -0
  116. data/lib/deepl/resources/translation_memory_segments.rb +80 -0
  117. data/lib/deepl/resources/usage.rb +93 -4
  118. data/lib/deepl/style_rule_api.rb +11 -0
  119. data/lib/deepl/translation_memory_api.rb +373 -1
  120. data/lib/deepl/utils/client_builder.rb +54 -0
  121. data/lib/deepl/utils/document_poller.rb +70 -0
  122. data/lib/deepl/utils/exception_builder.rb +3 -2
  123. data/lib/deepl/utils/http_client_builder.rb +79 -0
  124. data/lib/deepl/utils/number_parser.rb +34 -0
  125. data/lib/deepl/utils/term_validator.rb +33 -0
  126. data/lib/deepl/utils/time_parser.rb +40 -0
  127. data/lib/deepl.rb +190 -34
  128. data/lib/http_client_options.rb +21 -1
  129. data/lib/version.rb +1 -1
  130. data/spec/fixtures.rb +33 -0
  131. data/spec/offline/admin_client_spec.rb +551 -0
  132. data/spec/offline/client_spec.rb +273 -0
  133. data/spec/{api → offline}/configuration_spec.rb +45 -3
  134. data/spec/{constants → offline}/constants_spec.rb +23 -1
  135. data/spec/offline/correct_text_spec.rb +50 -0
  136. data/spec/offline/deprecation_spec.rb +32 -0
  137. data/spec/offline/errors_spec.rb +92 -0
  138. data/spec/offline/glossaries_spec.rb +98 -0
  139. data/spec/offline/http_client_spec.rb +319 -0
  140. data/spec/offline/languages_spec.rb +155 -0
  141. data/spec/offline/module_methods_spec.rb +13 -0
  142. data/spec/offline/multilingual_glossaries_spec.rb +358 -0
  143. data/spec/offline/offline_helper.rb +71 -0
  144. data/spec/offline/rephrase_text_spec.rb +73 -0
  145. data/spec/offline/spoken_terms_spec.rb +215 -0
  146. data/spec/offline/style_profiles_spec.rb +266 -0
  147. data/spec/offline/style_rules_spec.rb +318 -0
  148. data/spec/offline/translate_document_spec.rb +606 -0
  149. data/spec/offline/translate_text_spec.rb +286 -0
  150. data/spec/offline/translation_memories_client_spec.rb +256 -0
  151. data/spec/offline/translation_memories_spec.rb +463 -0
  152. data/spec/offline/usage_spec.rb +151 -0
  153. data/spec/real_api/admin_client_spec.rb +77 -0
  154. data/spec/real_api/correct_text_spec.rb +20 -0
  155. data/spec/real_api/glossaries_spec.rb +28 -0
  156. data/spec/real_api/languages_spec.rb +27 -0
  157. data/spec/real_api/multilingual_glossaries_spec.rb +53 -0
  158. data/spec/real_api/proxy_spec.rb +41 -0
  159. data/spec/real_api/real_api_helper.rb +65 -0
  160. data/spec/real_api/rephrase_text_spec.rb +16 -0
  161. data/spec/real_api/spoken_terms_spec.rb +56 -0
  162. data/spec/real_api/style_profiles_spec.rb +46 -0
  163. data/spec/real_api/style_rules_spec.rb +46 -0
  164. data/spec/real_api/translate_document_spec.rb +18 -0
  165. data/spec/real_api/translate_text_spec.rb +16 -0
  166. data/spec/real_api/translation_memories_spec.rb +56 -0
  167. data/spec/real_api/usage_spec.rb +20 -0
  168. data/spec/spec_helper.rb +1 -50
  169. metadata +134 -63
  170. data/.circleci/config.yml +0 -27
  171. data/.github/workflows/add_issues_to_kanban.yml +0 -16
  172. data/spec/api/api_spec.rb +0 -20
  173. data/spec/api/deepl_spec.rb +0 -262
  174. data/spec/integration_tests/document_api_spec.rb +0 -164
  175. data/spec/integration_tests/document_error_paths_spec.rb +0 -33
  176. data/spec/integration_tests/glossary_api_spec.rb +0 -114
  177. data/spec/integration_tests/glossary_error_paths_spec.rb +0 -107
  178. data/spec/integration_tests/integration_test_utils.rb +0 -170
  179. data/spec/integration_tests/languages_api_spec.rb +0 -54
  180. data/spec/integration_tests/languages_error_paths_spec.rb +0 -25
  181. data/spec/integration_tests/rephrase_api_spec.rb +0 -90
  182. data/spec/integration_tests/rephrase_error_paths_spec.rb +0 -53
  183. data/spec/integration_tests/smoke_test_spec.rb +0 -24
  184. data/spec/integration_tests/style_rule_api_spec.rb +0 -117
  185. data/spec/integration_tests/style_rule_error_paths_spec.rb +0 -45
  186. data/spec/integration_tests/translate_api_spec.rb +0 -98
  187. data/spec/integration_tests/translate_error_paths_spec.rb +0 -48
  188. data/spec/integration_tests/translation_memory_api_spec.rb +0 -54
  189. data/spec/integration_tests/translation_memory_error_paths_spec.rb +0 -19
  190. data/spec/integration_tests/usage_api_spec.rb +0 -29
  191. data/spec/integration_tests/usage_error_paths_spec.rb +0 -18
  192. data/spec/requests/extra_body_parameters_types_spec.rb +0 -82
  193. data/spec/requests/glossary/create_spec.rb +0 -44
  194. data/spec/requests/glossary/destroy_spec.rb +0 -27
  195. data/spec/requests/glossary/entries_spec.rb +0 -27
  196. data/spec/requests/glossary/find_spec.rb +0 -28
  197. data/spec/requests/glossary/language_pairs_spec.rb +0 -27
  198. data/spec/requests/glossary/list_spec.rb +0 -27
  199. data/spec/requests/languages_spec.rb +0 -27
  200. data/spec/requests/rephrase_spec.rb +0 -46
  201. data/spec/requests/style_rule/create_custom_instruction_spec.rb +0 -30
  202. data/spec/requests/style_rule/create_spec.rb +0 -29
  203. data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +0 -28
  204. data/spec/requests/style_rule/destroy_spec.rb +0 -27
  205. data/spec/requests/style_rule/find_custom_instruction_spec.rb +0 -29
  206. data/spec/requests/style_rule/find_spec.rb +0 -28
  207. data/spec/requests/style_rule/list_spec.rb +0 -27
  208. data/spec/requests/style_rule/update_configured_rules_spec.rb +0 -31
  209. data/spec/requests/style_rule/update_custom_instruction_spec.rb +0 -32
  210. data/spec/requests/style_rule/update_spec.rb +0 -29
  211. data/spec/requests/translate_spec.rb +0 -283
  212. data/spec/requests/translation_memory/list_spec.rb +0 -27
  213. data/spec/requests/usage_spec.rb +0 -27
  214. data/spec/resources/custom_instruction_spec.rb +0 -32
  215. data/spec/resources/glossary_spec.rb +0 -38
  216. data/spec/resources/language_pair_spec.rb +0 -23
  217. data/spec/resources/language_spec.rb +0 -45
  218. data/spec/resources/style_rule_spec.rb +0 -68
  219. data/spec/resources/text_spec.rb +0 -23
  220. data/spec/resources/translation_memory_spec.rb +0 -35
  221. data/spec/resources/usage_spec.rb +0 -35
  222. data/spec/support/live_mock_server.rb +0 -12
  223. data/spec/support/managed_glossary.rb +0 -17
  224. data/spec/support/managed_style_rule.rb +0 -17
  225. data/spec/support/managed_translation_memory.rb +0 -7
@@ -23,13 +23,24 @@ module DeepL
23
23
  "- Error message: #{error_message}"
24
24
  end
25
25
 
26
+ ##
27
+ # Checks if the translation is done, so the document can be downloaded.
28
+ #
29
+ # @return [Boolean] +true+ if the status is +done+.
30
+ def done?
31
+ status == Constants::DocumentStatusCode::DONE
32
+ end
33
+
26
34
  ##
27
35
  # Checks if the translation finished successfully
28
36
  #
37
+ # @deprecated Use #done? instead.
29
38
  # @return [true] if so
30
-
31
39
  def successful?
32
- status == 'done'
40
+ Deprecation.warn_once('DeepL::Resources::DocumentTranslationStatus#successful?',
41
+ '[DEPRECATION] DeepL::Resources::DocumentTranslationStatus#' \
42
+ 'successful? is deprecated, use #done? instead.')
43
+ done?
33
44
  end
34
45
 
35
46
  ##
@@ -38,7 +49,15 @@ module DeepL
38
49
  # @return [true] if so
39
50
 
40
51
  def error?
41
- status == 'error'
52
+ status == Constants::DocumentStatusCode::ERROR
53
+ end
54
+
55
+ ##
56
+ # Checks that the translation has not failed. Also true while it is queued or translating.
57
+ #
58
+ # @return [Boolean] +true+ unless the status is +error+.
59
+ def ok?
60
+ !error?
42
61
  end
43
62
 
44
63
  ##
@@ -47,7 +66,7 @@ module DeepL
47
66
  #
48
67
  # @return [true] if so
49
68
  def finished?
50
- successful? || error?
69
+ done? || error?
51
70
  end
52
71
  end
53
72
  end
@@ -8,20 +8,28 @@ module DeepL
8
8
  class Language < Base
9
9
  attr_reader :code, :name
10
10
 
11
- def initialize(code, name, supports_formality, *args)
11
+ # +client:+ is true for a language returned by DeepL::Client. @api private
12
+ def initialize(code, name, supports_formality, *args, client: false)
12
13
  super(*args)
13
14
 
14
15
  @code = code
15
16
  @name = name
16
17
  @supports_formality = supports_formality
18
+ @client = client
17
19
  end
18
20
 
19
21
  def to_s
20
22
  "#{code} - #{name}"
21
23
  end
22
24
 
25
+ # Whether the target language supports the +formality+ option.
26
+ #
27
+ # @return [Boolean, nil] +true+ or +false+ for a target language; for a source language
28
+ # +nil+ if it came from DeepL::Client.
29
+ # @raise [DeepL::Exceptions::NotSupported] For a source language from the module-level
30
+ # +DeepL.languages+, until the next major version.
23
31
  def supports_formality?
24
- return @supports_formality unless @supports_formality.nil?
32
+ return @supports_formality unless @supports_formality.nil? && !@client
25
33
 
26
34
  raise Exceptions::NotSupported, 'Support formality is only available on target languages'
27
35
  end
@@ -0,0 +1,57 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ # A feature supported by a DeepL API resource, as returned by `DeepL::Client#get_language_resources`.
9
+ class ResourceFeature
10
+ attr_reader :name
11
+
12
+ def initialize(feature)
13
+ @name = feature['name']
14
+ @needs_source_support = feature['needs_source_support'] ? true : false
15
+ @needs_target_support = feature['needs_target_support'] ? true : false
16
+ end
17
+
18
+ def to_s
19
+ name
20
+ end
21
+
22
+ # True if the source language must support this feature for it to be available.
23
+ def needs_source_support?
24
+ @needs_source_support
25
+ end
26
+
27
+ # True if the target language must support this feature for it to be available.
28
+ def needs_target_support?
29
+ @needs_target_support
30
+ end
31
+ end
32
+
33
+ # A DeepL API resource and the features it supports, as returned by `DeepL::Client#get_language_resources`.
34
+ class LanguageResource < Base
35
+ attr_reader :name, :features
36
+
37
+ def initialize(resource, *args)
38
+ super(*args)
39
+
40
+ @name = resource['name']
41
+ @features = (resource['features'] || []).to_h do |feature|
42
+ resource_feature = ResourceFeature.new(feature)
43
+ [resource_feature.name, resource_feature]
44
+ end
45
+ end
46
+
47
+ def to_s
48
+ name
49
+ end
50
+
51
+ # Returns true if this resource exposes the given feature (e.g. 'glossary').
52
+ def supports?(feature)
53
+ features.key?(feature.to_s)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,56 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ # Support level of a single feature of a language for a resource.
9
+ class LanguageFeature
10
+ attr_reader :status
11
+
12
+ def initialize(status)
13
+ @status = status
14
+ end
15
+
16
+ def to_s
17
+ status.to_s
18
+ end
19
+ end
20
+
21
+ # A language supported by a DeepL API resource, as returned by `DeepL::Client#get_languages_for_resource`.
22
+ class LanguageSupport < Base
23
+ attr_reader :code, :name, :status, :features
24
+
25
+ def initialize(language, *args)
26
+ super(*args)
27
+
28
+ @code = language['lang']
29
+ @name = language['name']
30
+ @status = language['status']
31
+ @usable_as_source = language['usable_as_source'] ? true : false
32
+ @usable_as_target = language['usable_as_target'] ? true : false
33
+ @features = (language['features'] || {}).to_h.transform_values do |feature|
34
+ LanguageFeature.new(feature.to_h['status'])
35
+ end
36
+ end
37
+
38
+ def to_s
39
+ "#{name} (#{code})"
40
+ end
41
+
42
+ def usable_as_source?
43
+ @usable_as_source
44
+ end
45
+
46
+ def usable_as_target?
47
+ @usable_as_target
48
+ end
49
+
50
+ # Returns true if this language supports the given feature (e.g. 'formality').
51
+ def supports?(feature)
52
+ features.key?(feature.to_s)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,112 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ class MultilingualGlossaryDictionary < Base
9
+ attr_reader :source_lang, :target_lang, :entry_count
10
+
11
+ def initialize(dictionary, *args)
12
+ super(*args)
13
+
14
+ @source_lang = dictionary['source_lang']
15
+ @target_lang = dictionary['target_lang']
16
+ @entry_count = dictionary['entry_count']
17
+ end
18
+
19
+ def to_s
20
+ "#{source_lang} -> #{target_lang} (#{entry_count})"
21
+ end
22
+ end
23
+
24
+ # The entries of one dictionary. Used as input for +create_multilingual_glossary+,
25
+ # +update_multilingual_glossary+, +update_multilingual_glossary_dictionary+ and
26
+ # +replace_multilingual_glossary_dictionary+, and returned by
27
+ # +get_multilingual_glossary_dictionary_entries+.
28
+ class MultilingualGlossaryDictionaryEntries < Base
29
+ # @return [Hash{String => String}] The target term of each source term.
30
+ attr_reader :entries
31
+ attr_reader :source_lang, :target_lang
32
+
33
+ ##
34
+ # @param [String] source_lang Source language code, for example +'en'+.
35
+ # @param [String] target_lang Target language code, for example +'de'+.
36
+ # @param [Hash{String => String}, Array<Array(String, String)>, String] entries The target
37
+ # term of each source term, as a hash, source/target pairs or a TSV string; terms are
38
+ # stripped and checked when sent.
39
+ # @raise [DeepL::Exceptions::ValidationError] If a pair or TSV line is not exactly two
40
+ # terms, or a source term appears twice.
41
+ def initialize(source_lang, target_lang, entries, request = nil, response = nil)
42
+ super(request, response)
43
+
44
+ @source_lang = source_lang
45
+ @target_lang = target_lang
46
+ pairs = entries.is_a?(String) ? self.class.tsv_to_pairs(entries) : entries.to_a
47
+ @entries = self.class.check_pairs!(pairs).to_h
48
+ end
49
+
50
+ # @api private
51
+ # Builds the entries from a dictionary of the API, whose entries are a TSV string.
52
+ def self.from_response(dictionary, request, response)
53
+ entries = (dictionary['entries'] || '').split(/\r?\n/).reject(&:empty?).to_h do |line|
54
+ source, target = line.split("\t", 2)
55
+ [source, target]
56
+ end
57
+ new(dictionary['source_lang'], dictionary['target_lang'], entries, request, response)
58
+ end
59
+
60
+ # @api private
61
+ # Raises ValidationError unless each pair is two strings and no source term, stripped as
62
+ # the term check strips it, appears twice. Returns +pairs+.
63
+ def self.check_pairs!(pairs)
64
+ seen = {}
65
+ pairs.each do |pair|
66
+ unless pair.is_a?(Array) && pair.size == 2 && pair.all?(String)
67
+ raise Exceptions::ValidationError, "Entry #{pair.inspect} must be two terms"
68
+ end
69
+
70
+ source = pair.first.strip
71
+ next seen[source] = true unless seen[source]
72
+
73
+ raise Exceptions::ValidationError, "Duplicate source term #{source.inspect}"
74
+ end
75
+ end
76
+
77
+ # @api private
78
+ # One source/target pair per non-blank line, separated by a single tab.
79
+ def self.tsv_to_pairs(tsv)
80
+ tsv.split(/\r?\n/).reject { |line| line.strip.empty? }.map do |line|
81
+ pair = line.split("\t", -1)
82
+ unless pair.size == 2
83
+ raise Exceptions::ValidationError,
84
+ "TSV entry #{line.inspect} must be a source and a target term separated by " \
85
+ 'one tab'
86
+ end
87
+
88
+ pair
89
+ end
90
+ end
91
+ end
92
+
93
+ class MultilingualGlossary < Base
94
+ attr_reader :id, :name, :creation_time, :dictionaries
95
+
96
+ def initialize(glossary, *args)
97
+ super(*args)
98
+
99
+ @id = glossary['glossary_id']
100
+ @name = glossary['name']
101
+ @creation_time = Utils::TimeParser.parse_optional_utc_time(glossary['creation_time'])
102
+ @dictionaries = (glossary['dictionaries'] || []).map do |dictionary|
103
+ MultilingualGlossaryDictionary.new(dictionary, *args)
104
+ end
105
+ end
106
+
107
+ def to_s
108
+ "#{id} - #{name}"
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,61 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ # The term count of one language in a spoken terms collection.
9
+ class SpokenTermsList < Base
10
+ attr_reader :lang, :entry_count
11
+
12
+ def initialize(term_list, *args)
13
+ super(*args)
14
+
15
+ @lang = term_list['lang']
16
+ @entry_count = term_list['entry_count']
17
+ end
18
+
19
+ def to_s
20
+ "#{lang} (#{entry_count})"
21
+ end
22
+ end
23
+
24
+ # The terms of one language. Used as input for +create_spoken_terms+,
25
+ # +update_spoken_terms+ and +replace_spoken_terms_list+, and returned by
26
+ # +get_spoken_terms_entries+.
27
+ class SpokenTermsEntries < Base
28
+ attr_reader :lang, :entries
29
+
30
+ ##
31
+ # @param [String] lang Language code, for example +'en'+.
32
+ # @param [Array<String>] entries The terms, one per element.
33
+ def initialize(lang, entries, request = nil, response = nil)
34
+ super(request, response)
35
+
36
+ @lang = lang
37
+ @entries = entries
38
+ end
39
+ end
40
+
41
+ # A spoken terms collection.
42
+ class SpokenTerms < Base
43
+ attr_reader :spoken_terms_id, :name, :creation_time, :term_lists
44
+
45
+ def initialize(spoken_terms, *args)
46
+ super(*args)
47
+
48
+ @spoken_terms_id = spoken_terms['spoken_terms_id']
49
+ @name = spoken_terms['name']
50
+ @creation_time = Utils::TimeParser.parse_optional_utc_time(spoken_terms['creation_time'])
51
+ @term_lists = (spoken_terms['term_lists'] || []).map do |term_list|
52
+ SpokenTermsList.new(term_list, *args)
53
+ end
54
+ end
55
+
56
+ def to_s
57
+ "#{spoken_terms_id} - #{name}"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,44 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ # A style profile: a named set of glossaries, style rule lists and translation memories.
9
+ class StyleProfile < Base
10
+ attr_reader :style_profile_id, :name, :description, :industry, :glossary_ids, :style_rule_ids,
11
+ :translation_memory_ids, :glossary_priority_order_enabled, :creation_time,
12
+ :updated_time
13
+
14
+ def initialize(profile, *args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
15
+ super(*args)
16
+
17
+ @style_profile_id = profile['style_profile_id']
18
+ @name = profile['name']
19
+ @description = optional_string(profile['description'])
20
+ @industry = optional_string(profile['industry'])
21
+ @glossary_ids = id_list(profile['glossary_ids'])
22
+ @style_rule_ids = id_list(profile['style_rule_ids'])
23
+ @translation_memory_ids = id_list(profile['translation_memory_ids'])
24
+ @glossary_priority_order_enabled = profile['glossary_priority_order_enabled'] == true
25
+ @creation_time = Utils::TimeParser.parse_optional_utc_time(profile['creation_time'])
26
+ @updated_time = Utils::TimeParser.parse_optional_utc_time(profile['updated_time'])
27
+ end
28
+
29
+ def to_s
30
+ "#{style_profile_id} - #{name}"
31
+ end
32
+
33
+ private
34
+
35
+ def optional_string(value)
36
+ value if value.is_a?(String)
37
+ end
38
+
39
+ def id_list(value)
40
+ value.is_a?(Array) ? value.grep(String) : []
41
+ end
42
+ end
43
+ end
44
+ end
@@ -80,6 +80,9 @@ module DeepL
80
80
  return nil unless time_string
81
81
 
82
82
  Time.parse(time_string)
83
+ rescue ArgumentError, TypeError
84
+ raise Exceptions::Error,
85
+ "Unexpected response from the DeepL API: invalid time #{time_string.inspect}"
83
86
  end
84
87
  end
85
88
  end
@@ -6,14 +6,30 @@
6
6
  module DeepL
7
7
  module Resources
8
8
  class Text < Base
9
+ # @return [Integer, nil] Number of characters billed for the text, +nil+ if the response
10
+ # does not contain it.
11
+ attr_reader :billed_characters
12
+ # @return [String, nil] Tag handling algorithm version used (see
13
+ # +DeepL::Constants::TagHandlingVersion+), +nil+ if the response does not contain it.
14
+ attr_reader :tag_handling_version
15
+ # @return [String, nil] Target language of a rephrase or correct result, +nil+ for text
16
+ # translation results and if the response does not contain it.
17
+ attr_reader :target_language
9
18
  attr_reader :text, :detected_source_language, :model_type_used
10
19
 
11
- def initialize(text, detected_source_language, model_type_used, *args)
12
- super(*args)
20
+ # Positional, not keyword, parameters: Ruby 2.7 would read the response (it has +to_hash+)
21
+ # as keywords.
22
+ def initialize(text, detected_source_language, model_type_used, request = nil, # rubocop:disable Metrics/ParameterLists
23
+ response = nil, billed_characters = nil, tag_handling_version = nil,
24
+ target_language = nil)
25
+ super(request, response)
13
26
 
14
27
  @text = text
15
28
  @detected_source_language = detected_source_language
16
29
  @model_type_used = model_type_used
30
+ @billed_characters = billed_characters
31
+ @tag_handling_version = tag_handling_version
32
+ @target_language = target_language
17
33
  end
18
34
 
19
35
  def to_s
@@ -6,7 +6,8 @@
6
6
  module DeepL
7
7
  module Resources
8
8
  class TranslationMemory < Base
9
- attr_reader :translation_memory_id, :name, :source_language, :target_languages, :segment_count
9
+ attr_reader :translation_memory_id, :name, :source_language, :target_languages,
10
+ :segment_count, :creation_time, :updated_time
10
11
 
11
12
  def initialize(translation_memory, *args)
12
13
  super(*args)
@@ -15,11 +16,19 @@ module DeepL
15
16
  @source_language = translation_memory['source_language']
16
17
  @target_languages = translation_memory['target_languages'] || []
17
18
  @segment_count = translation_memory['segment_count'] || 0
19
+ @creation_time = parse_time(translation_memory['creation_time'])
20
+ @updated_time = parse_time(translation_memory['updated_time'])
18
21
  end
19
22
 
20
23
  def to_s
21
24
  "#{translation_memory_id} - #{name}"
22
25
  end
26
+
27
+ private
28
+
29
+ def parse_time(value)
30
+ Utils::TimeParser.parse_optional_utc_time(value)
31
+ end
23
32
  end
24
33
  end
25
34
  end
@@ -0,0 +1,37 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ ##
9
+ # A translation memory export job. Poll the job until it is finished to obtain the download
10
+ # URL of the exported TMX file.
11
+
12
+ class TranslationMemoryExport < Base
13
+ attr_reader :job_id, :translation_memory_id
14
+
15
+ def initialize(translation_memory_export, reused_existing, *args)
16
+ super(*args)
17
+ parameters = translation_memory_export['parameters'] || {}
18
+ @job_id = translation_memory_export['job_id']
19
+ @translation_memory_id = parameters['translation_memory_id']
20
+ @reused_existing = reused_existing
21
+ end
22
+
23
+ ##
24
+ # Checks if the API reused a previously completed export instead of starting a new one.
25
+ #
26
+ # @return [true] if so
27
+
28
+ def reused_existing?
29
+ @reused_existing
30
+ end
31
+
32
+ def to_s
33
+ "TranslationMemoryExport: ID: #{job_id}"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Resources
8
+ ##
9
+ # A newly created translation memory import job. The TMX file must be uploaded to
10
+ # `upload_url` before `expires_at`, processing starts once the upload is detected.
11
+
12
+ class TranslationMemoryImport < Base
13
+ attr_reader :job_id, :upload_url, :expires_at
14
+
15
+ def initialize(translation_memory_import, *args)
16
+ super(*args)
17
+ @job_id = translation_memory_import['job_id']
18
+ @upload_url = translation_memory_import['upload_url']
19
+ @expires_at = Utils::TimeParser.parse_optional_utc_time(
20
+ translation_memory_import['expires_at']
21
+ )
22
+ end
23
+
24
+ def to_s
25
+ "TranslationMemoryImport: ID: #{job_id}"
26
+ end
27
+ end
28
+ end
29
+ end