Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions translate/api/Translate.Samples.Test/TextTests.cs
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);
}

Copy link
Copy Markdown
Contributor

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

}
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>
22 changes: 22 additions & 0 deletions translate/api/Translate.Samples.Test/TranslateFixture.cs
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() { }
}
53 changes: 53 additions & 0 deletions translate/api/Translate.Samples/Text.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant

TranslationResult translationResult = client.TranslateText(text, targetLanguage, sourceLanguage);

// Print results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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]
13 changes: 13 additions & 0 deletions translate/api/Translate.Samples/Translate.Samples.csproj
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>
28 changes: 28 additions & 0 deletions translate/api/Translate.sln
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