diff --git a/translate/api/Translate.Samples.Test/TextTests.cs b/translate/api/Translate.Samples.Test/TextTests.cs new file mode 100644 index 00000000000..d94e09c00a8 --- /dev/null +++ b/translate/api/Translate.Samples.Test/TextTests.cs @@ -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); + } +} diff --git a/translate/api/Translate.Samples.Test/Translate.Samples.Test.csproj b/translate/api/Translate.Samples.Test/Translate.Samples.Test.csproj new file mode 100644 index 00000000000..45ad9415599 --- /dev/null +++ b/translate/api/Translate.Samples.Test/Translate.Samples.Test.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/translate/api/Translate.Samples.Test/TranslateFixture.cs b/translate/api/Translate.Samples.Test/TranslateFixture.cs new file mode 100644 index 00000000000..74690cbc505 --- /dev/null +++ b/translate/api/Translate.Samples.Test/TranslateFixture.cs @@ -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 +{ + + public TranslateFixture() { } + + public void Dispose() { } +} diff --git a/translate/api/Translate.Samples/Text.cs b/translate/api/Translate.Samples/Text.cs new file mode 100644 index 00000000000..6dae38a7ae8 --- /dev/null +++ b/translate/api/Translate.Samples/Text.cs @@ -0,0 +1,53 @@ +/* + * 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 +{ + /// + /// Translate the given text into the specified targetLanguage. + /// + /// The ISO 639 language code to translate the text into. + /// + /// Optional. The ISO 639 language code of the input text. If null, the API will attempt + /// to detect the source language automatically. + /// + /// The text to translate. + /// + /// An Execute object representing the completed workflow execution. + /// + public async Task TranslateText( + string targetLanguage, + string? sourceLanguage, + string text) + { + // Create TranslationClient. + TranslationClient client = await TranslationClient.CreateAsync(); + + // Translate text. + TranslationResult translationResult = client.TranslateText(text, targetLanguage, sourceLanguage); + + // Print results. + Console.WriteLine($"Input text: {translationResult.OriginalText}"); + Console.WriteLine($"Translated text: {translationResult.TranslatedText}"); + + return translationResult; + } +} +// [END translate_translate_text] diff --git a/translate/api/Translate.Samples/Translate.Samples.csproj b/translate/api/Translate.Samples/Translate.Samples.csproj new file mode 100644 index 00000000000..6d56a6d8705 --- /dev/null +++ b/translate/api/Translate.Samples/Translate.Samples.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/translate/api/Translate.sln b/translate/api/Translate.sln new file mode 100644 index 00000000000..3a84c4041c2 --- /dev/null +++ b/translate/api/Translate.sln @@ -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