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
Copy file name to clipboardExpand all lines: articles/cognitive-services/Translator/quickstart-csharp-translate.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ public class SentenceLength
118
118
119
119
## Create a function to translate text
120
120
121
-
Within the `Program` class, create a asynchronous function called `TranslateTextRequest`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
121
+
Within the `Program` class, create an asynchronous function called `TranslateTextRequest`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
122
122
123
123
```csharp
124
124
// This sample requires C# 7.1 or later for async/await.
Copy file name to clipboardExpand all lines: articles/cognitive-services/Translator/quickstart-csharp-transliterate.md
+73-31Lines changed: 73 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,13 @@ ms.author: erhopf
14
14
15
15
# Quickstart: Use the Translator Text API to transliterate text using C#
16
16
17
-
In this quickstart, you'll learn how to transliterate (convert) text from one script to another using .NET Core (C#) and the Translator Text REST API. In the sample provided, Japanese is transliterated to use the Latin alphabet.
17
+
In this quickstart, you'll learn how to transliterate (convert) text from one script to another using .NET Core (C#), C# 7.1 or later, and the Translator Text REST API. In the sample provided, Japanese is transliterated to use the Latin alphabet.
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 `tts-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 `tts-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,32 @@ 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.
78
+
/// </summary>
79
+
publicclassTransliterationResult
80
+
{
81
+
publicstringText { get; set; }
82
+
publicstringScript { get; set; }
83
+
}
84
+
```
85
+
56
86
## Create a function to transliterate text
57
87
58
-
Within the `Program` class, create a function called `TransliterateText`. This class encapsulates the code used to call the Transliterate resource and prints the result to console.
88
+
Within the `Program` class, create an asynchronous function called `TransliterateTextRequest`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
Add these lines to the `TransliterateText` function. You'll notice that along with the `api-version`, two additional parameters have been appended to the `route`. These parameters are used to set the input language, and the scripts for transliteration. In this sample, it's set to Japanese (`jpan`) and Latin (`latn`). Make sure you update the subscription key value.
102
+
Next, we need to create and serialize the JSON object that includes the text you want to translate. Keep in mind, you can pass more than one object in the `body`.
Next, we need to create and serialize the JSON object that includes the text you want to transliterate. Keep in mind, you can pass more than one object in the `body` array.
81
-
82
-
```csharp
83
-
System.Object[] body=newSystem.Object[] { new { Text=@"こんにちは" } };
105
+
object[] body=newobject[] { new { Text=inputText } };
84
106
varrequestBody=JsonConvert.SerializeObject(body);
85
107
```
86
108
@@ -110,36 +132,48 @@ Inside the `HttpRequestMessage` you'll:
Console.WriteLine("Transliterated to {0} script: {1}", o.Script, o.Text);
153
+
}
132
154
```
133
155
134
156
## Put it all together
135
157
136
-
The last step is to call `TransliterateText()` in the `Main` function. Locate `static void Main(string[] args)` and add these lines:
158
+
The last step is to call `TransliterateTextRequest()` in the `Main` function. In this sample, we're transliterating from Japanese to latin script. Locate `static void Main(string[] args)` and add replace it with this code:
137
159
138
160
```csharp
139
-
TransliterateText();
140
-
Console.ReadLine();
161
+
staticasyncTaskMain(string[] args)
162
+
{
163
+
// This is our main function.
164
+
// Output languages are defined in the route.
165
+
// For a complete list of options, see API reference.
0 commit comments