You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Quickstart: Use the Translator Text API to detect text language using C#
16
16
17
-
In this quickstart, you'll learn how to detect the language of provided text using .NET Core and the Translator Text REST API.
17
+
In this quickstart, you'll learn how to detect the language of provided text using .NET Core, C# 7.1 or later, and the Translator Text REST API.
18
18
19
19
This quickstart requires an [Azure Cognitive Services account](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account) with a Translator Text resource. If you don't have an account, you can use the [free trial](https://azure.microsoft.com/try/cognitive-services/) to get a subscription key.
*[Visual Studio](https://visualstudio.microsoft.com/downloads/), [Visual Studio Code](https://code.visualstudio.com/download), or your favorite text editor
@@ -42,6 +43,18 @@ Next, you'll need to install Json.Net. From your project's directory, run:
This quickstart requires C# 7.1 or later. There are a few ways to change the C# version for your project. In this guide, we'll show you how to adjust the `detect-sample.csproj` file. For all available options, such as changing the language in Visual Studio, see [Select the C# language version](https://docs.microsoft.com/dotnet/csharp/language-reference/configure-language-version).
49
+
50
+
Open your project, then open `detect-sample.csproj`. Make sure that `LangVersion` is set to 7.1 or later. If there isn't a property group for the language version, add these lines:
51
+
52
+
```xml
53
+
<PropertyGroup>
54
+
<LangVersion>7.1</LangVersion>
55
+
</PropertyGroup>
56
+
```
57
+
45
58
## Add required namespaces to your project
46
59
47
60
The `dotnet new console` command that you ran earlier created a project, including `Program.cs`. This file is where you'll put your application code. Open `Program.cs`, and replace the existing using statements. These statements ensure that you have access to all the types required to build and run the sample app.
@@ -50,15 +63,42 @@ The `dotnet new console` command that you ran earlier created a project, includi
50
63
usingSystem;
51
64
usingSystem.Net.Http;
52
65
usingSystem.Text;
66
+
usingSystem.Threading.Tasks;
67
+
// Install Newtonsoft.Json with NuGet
53
68
usingNewtonsoft.Json;
54
69
```
55
70
71
+
## Create classes for the JSON response
72
+
73
+
Next, we're going to create a class that's used when deserializing the JSON response returned by the Translator Text API.
74
+
75
+
```csharp
76
+
/// <summary>
77
+
/// The C# classes that represents the JSON returned by the Translator Text API.
## Create a function to detect the source text's language
57
97
58
-
Within the `Program` class, create a function called `Detect`. This class encapsulates the code used to call the Detect resource and prints the result to console.
98
+
Within the `Program` class, create a function called `DetectTextRequest()`. This class encapsulates the code used to call the Detect resource and prints the result to console.
0 commit comments