-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(translate): add translate text sample and test #3093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6ffb99f
1dd9cd5
ee65b04
c39bba6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * 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 | ||
| * | ||
| * https: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 Google.Cloud.Translation.V2; | ||
|
|
||
| [Collection(nameof(TranslateFixture))] | ||
| public class TextTests | ||
| { | ||
| private readonly TranslateTextSample _sample; | ||
| private readonly TranslateFixture _fixture; | ||
|
|
||
| public TextTests(TranslateFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new TranslateTextSample(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TranslateTest() | ||
| { | ||
| TranslationResult translationResult = await _sample.TranslateText("en", "es", "¡Hola amigos y amigas!"); | ||
|
|
||
| Assert.Contains("Hello friends!", translationResult.TranslatedText); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
| <PackageReference Include="xunit" Version="2.5.3" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Translate.Samples\Translate.Samples.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="Xunit" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright(c) 2025 Google LLC | ||
| // | ||
| // 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. | ||
|
|
||
| [CollectionDefinition(nameof(TranslateFixture))] | ||
| public class TranslateFixture : IDisposable, ICollectionFixture<TranslateFixture> | ||
| { | ||
|
|
||
| public TranslateFixture() { } | ||
|
|
||
| public void Dispose() { } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. file name should be TranslateTextSample.cs |
||
| * Copyright 2025 Google LLC | ||
| * | ||
| * 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 | ||
| * | ||
| * https: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. | ||
| */ | ||
|
|
||
| // [START translate_translate_text] | ||
|
|
||
| using Google.Cloud.Translation.V2; | ||
|
|
||
| public class TranslateTextSample | ||
| { | ||
| /// <summary> | ||
| /// Translate the given text into the specified targetLanguage. | ||
| /// </summary> | ||
| /// <param name="targetLanguage">The ISO 639 language code to translate the text into.</param> | ||
| /// <param name="sourceLanguage"> | ||
| /// Optional. The ISO 639 language code of the input text. If null, the API will attempt | ||
| /// to detect the source language automatically. | ||
| /// </param> | ||
| /// <param name="text">The text to translate.</param> | ||
| /// <returns> | ||
| /// An Execute object representing the completed workflow execution. | ||
| /// </returns> | ||
|
Comment on lines
+23
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a more readable format you can follow for this documentation? |
||
| public async Task<TranslationResult> TranslateText( | ||
| string targetLanguage, | ||
| string? sourceLanguage, | ||
| string text) | ||
| { | ||
| // Create TranslationClient. | ||
| TranslationClient client = await TranslationClient.CreateAsync(); | ||
|
|
||
| // Translate text. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant |
||
| TranslationResult translationResult = client.TranslateText(text, targetLanguage, sourceLanguage); | ||
|
|
||
| // Print results. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant |
||
| Console.WriteLine($"Input text: {translationResult.OriginalText}"); | ||
| Console.WriteLine($"Translated text: {translationResult.TranslatedText}"); | ||
|
|
||
| return translationResult; | ||
| } | ||
| } | ||
| // [END translate_translate_text] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Google.Cloud.Translation.V2" Version="3.4.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.0.31903.59 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Translate.Samples", "Translate.Samples\Translate.Samples.csproj", "{6C7D7BC5-7EB4-4AD1-BFEA-9E4E38747D80}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Translate.Samples.Test", "Translate.Samples.Test\Translate.Samples.Test.csproj", "{E84081F0-8372-444A-89AA-D6373DDA4B81}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {6C7D7BC5-7EB4-4AD1-BFEA-9E4E38747D80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {6C7D7BC5-7EB4-4AD1-BFEA-9E4E38747D80}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {6C7D7BC5-7EB4-4AD1-BFEA-9E4E38747D80}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {6C7D7BC5-7EB4-4AD1-BFEA-9E4E38747D80}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {E84081F0-8372-444A-89AA-D6373DDA4B81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {E84081F0-8372-444A-89AA-D6373DDA4B81}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {E84081F0-8372-444A-89AA-D6373DDA4B81}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {E84081F0-8372-444A-89AA-D6373DDA4B81}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a test where source language is null