Amazon Translate - Formality Setting in .Net?

50 views Asked by At

When trying to use the Formality setting (https://aws.amazon.com/about-aws/whats-new/2022/10/amazon-translate-formality-customization-support-dutch-korean-mexican-spanish/ ) in code behind (.Net), I keep getting System.NullReferenceException: 'Object reference not set to an instance of an object.'

Here's the code I'm running - everything works as expected until I add in request.Settings.Formality = "FORMAL";`

using (var client = new AmazonTranslateClient(awsCredentials, selectedRegion))
                    {
                        var request = new Amazon.Translate.Model.TranslateTextRequest();
                        request.Text = toTranslate;
                        request.SourceLanguageCode = sourceLanguage; 
                        request.TargetLanguageCode = translateLanguage; 
                        request.Settings.Formality = "FORMAL";

`Looking at the limited examples in other languages from the AWS documentation doesn't indicate anything else that's needed. I also tried the Profanity setting and the same results - System.NullReferenceException.

I also tried making the call later via the using statement that looks like this with the same error:`

var response = client.TranslateTextAsync(request).GetAwaiter().GetResult();
                        response.AppliedSettings.Formality = translationFormality;

`

1

There are 1 answers

0
Thor2020 On

Updated code with solution that worked for me:

using (var client = new AmazonTranslateClient(awsCredentials, selectedRegion))
                {
                    var request = new Amazon.Translate.Model.TranslateTextRequest();
                    request.Text = toTranslate;
                    request.SourceLanguageCode = sourceLanguage; // SourceLanguageItem.LanguageCode;
                    request.TargetLanguageCode = translateLanguage; // TranslateLanguageItem.LanguageCode;
                    
                    TranslationSettings settings = new TranslationSettings();
                    settings.Formality = "FORMAL";
                    request.Settings = settings;