Skip to content
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

Extended language support and integration of HTML format #10

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
Binary file modified .vs/LibreTranslate.Net/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
5 changes: 3 additions & 2 deletions LibreTranslate.Net.Cmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ class Program
static void Main(string[] args)
{
var libreTranslate = new LibreTranslate();
var englishText = "Hello World!";
var englishText = "<p>Hello world!</p>";
var getSupportedLanguagesAsyncTask = libreTranslate.GetSupportedLanguagesAsync();
System.Threading.Tasks.Task.Run(() => getSupportedLanguagesAsyncTask).Wait();
var supportedLanguages = getSupportedLanguagesAsyncTask.Result;
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(supportedLanguages, Newtonsoft.Json.Formatting.Indented));
var TranslateAsyncTask = libreTranslate.TranslateAsync(new Translate()
{
ApiKey = "MySecretApiKey",
ApiKey = "YourSecretKey",
Source = LanguageCode.English,
Target = LanguageCode.Spanish,
Format = Format.HTML, //Format is optional with 'Format.Text' as default value.
Text = englishText
});
System.Threading.Tasks.Task.Run(() => TranslateAsyncTask).Wait();
Expand Down
40 changes: 40 additions & 0 deletions LibreTranslate.Net/Constants/Format.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text;

namespace LibreTranslate.Net
{
public class Format
{
private static readonly Dictionary<string, Format> Instance = new Dictionary<string, Format>();
private readonly string Name;
private Format(string name)
{
Name = name;
Instance[Name] = this;
}
public static implicit operator Format(string str)
{
return $"{FromString(str)}";
}
public static Format FromString(string str)
{
if(Instance.TryGetValue(str, out Format format))
{
return format;
}
else
{
throw new ArgumentException($"{nameof(Net.Format)} must be 'text' or 'html'");
}
}
public override string ToString()
{
return $"{Name}";
}

public static readonly Format HTML = new Format("html");
public static readonly Format Text = new Format("text");
}
}
17 changes: 17 additions & 0 deletions LibreTranslate.Net/Constants/LanguageCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,34 @@ public override string ToString()
}
public static readonly LanguageCode English = new LanguageCode("en");
public static readonly LanguageCode Arabic = new LanguageCode("ar");
public static readonly LanguageCode Azerbaijani = new LanguageCode("az");
public static readonly LanguageCode Catalan = new LanguageCode("ca");
public static readonly LanguageCode Chinese = new LanguageCode("zh");
public static readonly LanguageCode Czech = new LanguageCode("cs");
public static readonly LanguageCode Danish = new LanguageCode("da");
public static readonly LanguageCode Dutch = new LanguageCode("nl");
public static readonly LanguageCode Esperanto = new LanguageCode("eo");
public static readonly LanguageCode Finnish = new LanguageCode("fi");
public static readonly LanguageCode French = new LanguageCode("fr");
public static readonly LanguageCode German = new LanguageCode("de");
public static readonly LanguageCode Greek = new LanguageCode("el");
public static readonly LanguageCode Hebrew = new LanguageCode("he");
public static readonly LanguageCode Hindi = new LanguageCode("hi");
public static readonly LanguageCode Hungarian = new LanguageCode("hu");
public static readonly LanguageCode Indonesian = new LanguageCode("id");
public static readonly LanguageCode Irish = new LanguageCode("ga");
public static readonly LanguageCode Italian = new LanguageCode("it");
public static readonly LanguageCode Japanese = new LanguageCode("ja");
public static readonly LanguageCode Korean = new LanguageCode("ko");
public static readonly LanguageCode Persian = new LanguageCode("fa");
public static readonly LanguageCode Polish = new LanguageCode("pl");
public static readonly LanguageCode Portuguese = new LanguageCode("pt");
public static readonly LanguageCode Russian = new LanguageCode("ru");
public static readonly LanguageCode Slovak = new LanguageCode("sk");
public static readonly LanguageCode Spanish = new LanguageCode("es");
public static readonly LanguageCode Swedish = new LanguageCode("sv");
public static readonly LanguageCode Turkish = new LanguageCode("tr");
public static readonly LanguageCode Ukranian = new LanguageCode("uk");
public static readonly LanguageCode AutoDetect = new LanguageCode("auto");
}
}
3 changes: 2 additions & 1 deletion LibreTranslate.Net/LibreTranslate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public async Task<string> TranslateAsync(Translate translate)
{ "q", translate.Text },
{ "source", translate.Source.ToString() },
{ "target", translate.Target.ToString() },
{ "api_key", translate.ApiKey }
{ "format", translate.Format?.ToString() ?? Format.Text.ToString() },
{ "api_key", translate.ApiKey },
});
var response = await HttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post, "/translate")
{
Expand Down
6 changes: 6 additions & 0 deletions LibreTranslate.Net/Models/Translate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class Translate
[JsonProperty("target")]
public LanguageCode Target { get; set; }
/// <summary>
/// [optional]
/// The current format of the text to be translated
/// </summary>
[JsonProperty("format")]
public Format Format { get; set; }
/// <summary>
/// The libre translate api key
/// </summary>
[JsonProperty("api_key")]
Expand Down
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,38 @@ using LibreTranslate.Net;
```
### Usage
```csharp
var LibreTranslate = new LibreTranslate();
System.Collections.Generic.IEnumerable<SupportedLanguages> SupportedLanguages = await LibreTranslate.GetSupportedLanguagesAsync();
var libreTranslate = new LibreTranslate();

//Get supported languages
System.Collections.Generic.IEnumerable<SupportedLanguages> SupportedLanguages = await libreTranslate.GetSupportedLanguagesAsync();
System.Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(supportedLanguages, Newtonsoft.Json.Formatting.Indented));

//text translation
var englishText = "Hello World!";
string spanishText = await LibreTranslate.TranslateAsync(new Translate() {
string spanishText = await libreTranslate.TranslateAsync(new Translate() {
ApiKey = "MySecretApiKey",
Source = LanguageCode.English,
Target = LanguageCode.Spanish,
Text = englishText
});
System.Console.WriteLine(spanishText);

//html translation
var englishHtml = "<p>Hello World!</p>";
string spanishHtml = await libreTranslate.TranslateAsync(new Translate() {
ApiKey = "MySecretApiKey",
Source = LanguageCode.English,
Target = LanguageCode.Spanish,
Format = Format.HTML,
Text = englishHtml
});
System.Console.WriteLine(spanishHtml);
```
### Output:
```
Hello World!
¡Hola Mundo!
<p>¡Hola Mundo!</p>
```
### Custom LibreTranslate URL (style: `http[s]://url` with no trailing `/`):
```csharp
Expand All @@ -45,15 +61,32 @@ Language|Code
-|-
English|`LanguageCode.English`
Arabic|`LanguageCode.Arabic`
Azerbaijani|`LanguageCode.Azerbaijani`
Catalan|`LanguageCode.Catalan`
Chinese|`LanguageCode.Chinese`
Czech|`LanguageCode.Czech`
Danish|`LanguageCode.Danish`
Dutch|`LanguageCode.Dutch`
Esperanto|`LanguageCode.Esperanto`
Finnish|`LanguageCode.Finnish`
French|`LanguageCode.French`
German|`LanguageCode.German`
Greek|`LanguageCode.Greek`
Hebrew|`LanguageCode.Hebrew`
Hindi|`LanguageCode.Hindi`
Hungarian|`LanguageCode.Hungarian`
Indonesian|`LanguageCode.Indonesian`
Irish|`LanguageCode.Irish`
Italian|`LanguageCode.Italian`
Japanese|`LanguageCode.Japanese`
Korean|`LanguageCode.Korean`
Persian|`LanguageCode.Persian`
Polish|`LanguageCode.Polish`
Portuguese|`LanguageCode.Portuguese`
Russian|`LanguageCode.Russian`
Slovak|`LanguageCode.Slovak`
Spanish|`LanguageCode.Spanish`
Swedish|`LanguageCode.Swedish`
Turkish|`LanguageCode.Turkish`
Ukranian|`LanguageCode.Ukranian`
AutoDetect|`LanguageCode.AutoDetect //This feature is experimental`