Skip to content

Commit e79f522

Browse files
feat: add billed characters to translate text response
1 parent c2f0bd5 commit e79f522

File tree

7 files changed

+26
-5
lines changed

7 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [Unreleased]
9+
### Added
10+
* Added `getBilledCharacters()` to text translation response.
11+
12+
813
## [1.5.1] - 2024-09-05
914
### Fixed
1015
* Fixed parsing for usage count and limit for large values.
@@ -110,6 +115,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
110115
Initial version.
111116

112117

118+
[Unreleased]: https://github.com/DeepLcom/deepl-java/compare/v1.5.1...HEAD
113119
[1.5.1]: https://github.com/DeepLcom/deepl-java/compare/v1.5.0...v1.5.1
114120
[1.5.0]: https://github.com/DeepLcom/deepl-java/compare/v1.4.0...v1.5.0
115121
[1.4.0]: https://github.com/DeepLcom/deepl-java/compare/v1.3.0...v1.4.0

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ There are additional optional arguments to control translation, see
9696
[Text translation options](#text-translation-options) below.
9797

9898
`translateText()` returns a `TextResult`, or a List of `TextResult`s
99-
corresponding to your input text(s). `TextResult` has two accessors: `getText()`
100-
returns the translated text, and `getDetectedSourceLanguage()` returns the
101-
detected source language code.
99+
corresponding to your input text(s). `TextResult` has the following accessors:
100+
- `getText()` returns the translated text,
101+
- `getDetectedSourceLanguage()` returns the detected source language code, and
102+
- `getBilledCharacters()` returns the number of characters billed for the text.
102103

103104
```java
104105
class Example { // Continuing class Example from above
@@ -115,8 +116,10 @@ class Example { // Continuing class Example from above
115116
"en-GB");
116117
System.out.println(results.get(0).getText()); // "How are you?"
117118
System.out.println(results.get(0).getDetectedSourceLanguage()); // "ja" the language code for Japanese
119+
System.out.println(results.get(0).getBilledCharacters()); // 7 - the number of characters in the source text "お元気ですか?"
118120
System.out.println(results.get(1).getText()); // "How are you?"
119121
System.out.println(results.get(1).getDetectedSourceLanguage()); // "es" the language code for Spanish
122+
System.out.println(results.get(1).getBilledCharacters()); // 12 - the number of characters in the source text "¿Cómo estás?"
120123

121124
// Translate into German with less and more Formality:
122125
System.out.println(translator.translateText("How are you?",

deepl-java/src/main/java/com/deepl/api/TextResult.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
public class TextResult {
88
private final String text;
99
private final String detectedSourceLanguage;
10+
private final int billedCharacters;
1011

1112
/** Constructs a new instance. */
12-
public TextResult(String text, String detectedSourceLanguage) {
13+
public TextResult(String text, String detectedSourceLanguage, int billedCharacters) {
1314
this.text = text;
1415
this.detectedSourceLanguage = LanguageCode.standardize(detectedSourceLanguage);
16+
this.billedCharacters = billedCharacters;
1517
}
1618

1719
/** The translated text. */
@@ -23,4 +25,9 @@ public String getText() {
2325
public String getDetectedSourceLanguage() {
2426
return detectedSourceLanguage;
2527
}
28+
29+
/** Number of characters billed for this text. */
30+
public int getBilledCharacters() {
31+
return billedCharacters;
32+
}
2633
}

deepl-java/src/main/java/com/deepl/api/Translator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,8 @@ private static ArrayList<KeyValuePair<String, String>> createHttpParams(
782782
if (text.isEmpty()) throw new IllegalArgumentException("text must not be empty");
783783
params.add(new KeyValuePair<>("text", text));
784784
});
785+
// Always send show_billed_characters=1, remove when the API default is changed to true
786+
params.add(new KeyValuePair<>("show_billed_characters", "1"));
785787

786788
if (options != null) {
787789
// Note: formality and glossaryId are added above

deepl-java/src/main/java/com/deepl/api/parsing/TextResultDeserializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public TextResult deserialize(JsonElement json, Type typeOfT, JsonDeserializatio
1818
JsonObject jsonObject = json.getAsJsonObject();
1919
return new TextResult(
2020
jsonObject.get("text").getAsString(),
21-
jsonObject.get("detected_source_language").getAsString());
21+
jsonObject.get("detected_source_language").getAsString(),
22+
jsonObject.get("billed_characters").getAsInt());
2223
}
2324
}

deepl-java/src/test/java/com/deepl/api/GeneralTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void testExampleTranslation() throws DeepLException, InterruptedException {
4545
String sourceLang = LanguageCode.removeRegionalVariant(entry.getKey());
4646
TextResult result = translator.translateText(inputText, sourceLang, "en-US");
4747
Assertions.assertTrue(result.getText().toLowerCase(Locale.ENGLISH).contains("proton"));
48+
Assertions.assertEquals(inputText.length(), result.getBilledCharacters());
4849
}
4950
}
5051

deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void testSingleText() throws DeepLException, InterruptedException {
1717
TextResult result = translator.translateText(exampleText.get("en"), null, LanguageCode.German);
1818
Assertions.assertEquals(exampleText.get("de"), result.getText());
1919
Assertions.assertEquals("en", result.getDetectedSourceLanguage());
20+
Assertions.assertEquals(exampleText.get("en").length(), result.getBilledCharacters());
2021
}
2122

2223
@Test

0 commit comments

Comments
 (0)