Lusamine.GTranslate
1.3.1
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package Lusamine.GTranslate --version 1.3.1
NuGet\Install-Package Lusamine.GTranslate -Version 1.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lusamine.GTranslate" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lusamine.GTranslate" Version="1.3.1" />
<PackageReference Include="Lusamine.GTranslate" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lusamine.GTranslate --version 1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Lusamine.GTranslate, 1.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lusamine.GTranslate@1.3.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lusamine.GTranslate&version=1.3.1
#tool nuget:?package=Lusamine.GTranslate&version=1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Lusamine.GTranslate
A .NET client for the Google Translate API, a C# port of py-googletrans.
Installation
dotnet add package Lusamine.GTranslate
Quick start
using Lusamine.GTranslate;
using var translator = new Translator();
Translate a single string
Translated result = await translator.TranslateAsync("안녕하세요.", dest: "en");
Console.WriteLine(result.Text); // Hello.
Console.WriteLine(result.Src); // ko
Console.WriteLine(result.Pronunciation); // Annyeonghaseyo.
Translate a batch
IReadOnlyList<Translated> results = await translator.TranslateManyAsync(
["hello", "world", "goodbye"], dest: "ko");
foreach (var r in results)
Console.WriteLine($"{r.Origin} → {r.Text}");
// hello → 안녕하세요
// world → 세계
// goodbye → 잘 가요
Detect language
Detected detected = await translator.DetectAsync("これは日本語です");
Console.WriteLine(detected.Lang); // ja
Console.WriteLine(detected.Confidence); // 0.98...
Detect a batch
IReadOnlyList<Detected> results = await translator.DetectManyAsync(
["Hello", "Bonjour", "привет"]);
Language codes
Pass BCP-47 codes ("en", "zh-cn", "pt"), full language names ("english", "chinese (simplified)"), or locale strings ("en_US"). Use "auto" as the source to let Google detect it.
// By code
await translator.TranslateAsync("Hello", src: "en", dest: "ja");
// By name (case-insensitive)
await translator.TranslateAsync("Hello", src: "ENGLISH", dest: "iRiSh");
// Auto-detect source
await translator.TranslateAsync("Bonjour", dest: "en");
Browse all supported languages:
foreach (var (code, name) in Languages.All)
Console.WriteLine($"{code}: {name}");
Constructor options
| Parameter | Default | Description |
|---|---|---|
serviceUrls |
["translate.googleapis.com"] |
One or more Google Translate hosts. Use translate.google.com for the webapp endpoint (requires TKK token fetch). |
userAgent |
Chrome UA string | User-Agent header sent with every request. |
raiseException |
false |
When true, throws HttpRequestException on non-200 responses instead of returning an empty result. |
listOperationMaxConcurrency |
2 |
Max parallel HTTP calls for batch operations. |
httpClient |
null |
Bring your own HttpClient (e.g. from IHttpClientFactory). When null a new instance is created and disposed with the translator. |
// Use the webapp endpoint (slower, requires token fetch)
using var translator = new Translator(
serviceUrls: ["translate.google.com"],
raiseException: true);
// Provide your own HttpClient
using var translator = new Translator(httpClient: myHttpClient);
Disposal
Translator implements both IDisposable and IAsyncDisposable. When you supply your own HttpClient it is not disposed. Only the internally created one is.
// Synchronous
using var translator = new Translator();
// Asynchronous
await using var translator = new Translator();
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Text.Json (>= 8.0.5)
-
.NETStandard 2.1
- System.Text.Json (>= 8.0.5)
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.