Skip to content

Commit

Permalink
feat: add Formality options PreferLess and PreferMore
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jones-dev committed Sep 28, 2022
1 parent 962540b commit fbdd626
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


# [Unreleased]
## [Unreleased]
### Added
* Add new `Formality` options: `PreferLess` and `PreferMore`.
### Changed
* Requests resulting in `503 Service Unavailable` errors are now retried.
Attempting to download a document before translation is completed will now
Expand Down
8 changes: 7 additions & 1 deletion DeepL/Formality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public enum Formality {
Less,

/// <summary>Increased formality.</summary>
More
More,

/// <summary>Less formality, i.e. more informal, if available for the specified target language, otherwise default.</summary>
PreferLess,

/// <summary>Increased formality, if available for the specified target language, otherwise default.</summary>
PreferMore
}
}
19 changes: 17 additions & 2 deletions DeepL/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,23 @@ await _client.ApiGetAsync("/v2/languages", cancellationToken, queryParams)
bodyParams.Add(("glossary_id", options.GlossaryId));
}

if (options.Formality != Formality.Default) {
bodyParams.Add(("formality", options.Formality.ToString().ToLowerInvariant()));
switch (options.Formality) {
case Formality.Default:
break;
case Formality.Less:
bodyParams.Add(("formality", "less"));
break;
case Formality.More:
bodyParams.Add(("formality", "more"));
break;
case Formality.PreferLess:
bodyParams.Add(("formality", "prefer_less"));
break;
case Formality.PreferMore:
bodyParams.Add(("formality", "prefer_more"));
break;
default:
throw new ArgumentException($"{nameof(options.Formality)} value is out of range");
}

if (options.SentenceSplittingMode != SentenceSplittingMode.All) {
Expand Down
18 changes: 18 additions & 0 deletions DeepLTests/TranslateTextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ public async Task TestFormality() {
if (!IsMockServer) {
Assert.Equal("Wie geht es Ihnen?", result.Text);
}

result = await translator.TranslateTextAsync(
"How are you?",
null,
"DE",
new TextTranslateOptions { Formality = Formality.PreferMore });
if (!IsMockServer) {
Assert.Equal("Wie geht es Ihnen?", result.Text);
}

result = await translator.TranslateTextAsync(
"How are you?",
null,
"DE",
new TextTranslateOptions { Formality = Formality.PreferLess });
if (!IsMockServer) {
Assert.Equal("Wie geht es dir?", result.Text);
}
}

[Fact]
Expand Down

0 comments on commit fbdd626

Please sign in to comment.