-
Notifications
You must be signed in to change notification settings - Fork 362
/
TranslationClientSnippets.cs
188 lines (166 loc) · 8 KB
/
TranslationClientSnippets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Google.Cloud.Translation.V2.Snippets
{
public class TranslationClientSnippets
{
public void TranslateTextBaseDefaultModel()
{
// Sample: TranslateTextBaseDefaultModel
TranslationClient client = TranslationClient.Create(model: TranslationModel.Base);
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
// End sample
}
public void TranslateTextBaseOverrideModel()
{
// Sample: TranslateTextBaseOverrideModel
TranslationClient client = TranslationClient.Create();
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French,
model: TranslationModel.Base);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
// End sample
}
[Fact]
public void TranslateText()
{
// Sample: TranslateText
// Additional: TranslateText(string, string, string, *)
TranslationClient client = TranslationClient.Create();
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
// End sample
Assert.Equal("Il pleut.", result.TranslatedText);
Assert.Equal("en", result.DetectedSourceLanguage);
}
[Fact]
public void TranslateHtml()
{
// Sample: TranslateHtml
// Additional: TranslateHtml(string, string, string, *)
TranslationClient client = TranslationClient.Create();
TranslationResult result = client.TranslateHtml("<p><strong>It is raining.</strong></p>", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
// End sample
Assert.Equal("<p> <strong>Il pleut.</strong> </p>", result.TranslatedText);
Assert.Equal("en", result.DetectedSourceLanguage);
}
[Fact]
public void TranslateText_Multiple()
{
// Sample: TranslateTextMultiple
// Additional: TranslateText(IEnumerable<string>, string, string, *)
TranslationClient client = TranslationClient.Create();
IList<TranslationResult> results = client.TranslateText(
new[] { "It is raining.", "It is sunny." },
LanguageCodes.French);
foreach (TranslationResult result in results)
{
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
}
// End sample
Assert.Equal("Il pleut.", results[0].TranslatedText);
Assert.Equal("en", results[0].DetectedSourceLanguage);
Assert.Equal("C'est ensoleillé.", results[1].TranslatedText);
Assert.Equal("en", results[1].DetectedSourceLanguage);
}
[Fact]
public void TranslateHtml_Multiple()
{
// Sample: TranslateHtmlMultiple
// Additional: TranslateHtml(IEnumerable<string>, string, string, *)
TranslationClient client = TranslationClient.Create();
IList<TranslationResult> results = client.TranslateHtml(
new[] { "<p><strong>It is raining.</strong></p>", "<p><strong>It is sunny.</strong></p>" },
LanguageCodes.French);
foreach (TranslationResult result in results)
{
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
}
// End sample
Assert.Equal("<p> <strong>Il pleut.</strong> </p>", results[0].TranslatedText);
Assert.Equal("en", results[0].DetectedSourceLanguage);
Assert.Equal("<p> <strong>C'est ensoleillé.</strong> </p>", results[1].TranslatedText);
Assert.Equal("en", results[1].DetectedSourceLanguage);
}
// See-also: TranslateText(string, string, string, *)
// Member: TranslateTextAsync(string, string, string, *, CancellationToken)
// Member: TranslateTextAsync(IEnumerable<string>, string, string, *, CancellationToken)
// See [TranslateText](ref) for a synchronous example.
// End see-also
// See-also: TranslateHtml(string, string, string, *)
// Member: TranslateHtmlAsync(string, string, string, *, CancellationToken)
// Member: TranslateHtmlAsync(IEnumerable<string>, string, string, *, CancellationToken)
// See [TranslateHtml](ref) for a synchronous example.
// End see-also
[Fact]
public void DetectLanguage()
{
// Sample: DetectLanguage
// Additional: DetectLanguage(string)
TranslationClient client = TranslationClient.Create();
Detection result = client.DetectLanguage("It is raining.");
Console.WriteLine($"Language: {result.Language}; confidence {result.Confidence}");
// End sample
Assert.Equal("en", result.Language);
Assert.Equal("It is raining.", result.Text);
}
// See-also: DetectLanguage(string)
// Member: DetectLanguageAsync(string, CancellationToken)
// See [DetectLanguage](ref) for a synchronous example.
// End see-also
[Fact]
public void DetectLanguages()
{
// Sample: DetectLanguages
// Additional: DetectLanguages(IEnumerable<string>)
TranslationClient client = TranslationClient.Create();
IList<Detection> results = client.DetectLanguages(new[] { "It is raining.", "Il pleut." });
foreach (var result in results)
{
Console.WriteLine($"Text: {result.Text}; language: {result.Language}; confidence {result.Confidence}");
}
// End sample
Assert.Equal("en", results[0].Language);
Assert.Equal("It is raining.", results[0].Text);
Assert.Equal("fr", results[1].Language);
Assert.Equal("Il pleut.", results[1].Text);
}
// See-also: DetectLanguages(IEnumerable<string>)
// Member: DetectLanguagesAsync(IEnumerable<string>, CancellationToken)
// See [DetectLanguages](ref) for a synchronous example.
// End see-also
[Fact]
public void ListLanguages()
{
// Snippet: ListLanguages(string)
TranslationClient client = TranslationClient.Create();
IList<Language> languages = client.ListLanguages(LanguageCodes.English);
// Display just the first 10 languages for brevity
foreach (Language language in languages.Take(10))
{
Console.WriteLine($"Language: {language.Name}; code {language.Code}");
}
// End snippet
}
// See-also: ListLanguages(string)
// Member: ListLanguagesAsync(string, CancellationToken)
// See [ListLanguages](ref) for a synchronous example.
// End see-also
}
}