Skip to content

Commit 0ccded0

Browse files
Adding GTranslate project
Need GTranslate project added in order to make a single executable.
1 parent 3959268 commit 0ccded0

40 files changed

+4206
-6
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,6 @@ paket-files/
259259
# Python Tools for Visual Studio (PTVS)
260260
__pycache__/
261261
*.pyc
262-
GTranslate/
262+
263+
# Other files and folders
264+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace GTranslate;
2+
3+
/// <summary>
4+
/// Represents a Microsoft Authorization token.
5+
/// </summary>
6+
public sealed class MicrosoftAuthTokenInfo
7+
{
8+
internal MicrosoftAuthTokenInfo(string token, string region)
9+
{
10+
Token = token;
11+
Region = region;
12+
}
13+
14+
/// <summary>
15+
/// Gets the token.
16+
/// </summary>
17+
public string Token { get; }
18+
19+
/// <summary>
20+
/// Gets the region of this token.
21+
/// </summary>
22+
public string Region { get; }
23+
24+
/// <inheritdoc/>
25+
public override string ToString() => Token;
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Diagnostics;
2+
3+
namespace GTranslate;
4+
5+
/// <summary>
6+
/// Represents a TTS voice in Microsoft Translator.
7+
/// </summary>
8+
[DebuggerDisplay($"{{{nameof(DebuggerDisplay)},nq}}")]
9+
public class MicrosoftVoice
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="MicrosoftVoice"/> class.
13+
/// </summary>
14+
/// <param name="displayName">The display name of the voice.</param>
15+
/// <param name="shortName">The short name of the voice.</param>
16+
/// <param name="gender">The gender of the voice.</param>
17+
/// <param name="locale">The locale of the voice.</param>
18+
public MicrosoftVoice(string displayName, string shortName, string gender, string locale)
19+
{
20+
DisplayName = displayName;
21+
Gender = gender;
22+
ShortName = shortName;
23+
Locale = locale;
24+
}
25+
26+
/// <summary>
27+
/// Gets the display name of this voice.
28+
/// </summary>
29+
public string DisplayName { get; }
30+
31+
/// <summary>
32+
/// Gets the short name of this voice.
33+
/// </summary>
34+
public string ShortName { get; }
35+
36+
/// <summary>
37+
/// Gets the gender of this voice.
38+
/// </summary>
39+
public string Gender { get; }
40+
41+
/// <summary>
42+
/// Gets the locale of this voice.
43+
/// </summary>
44+
public string Locale { get; }
45+
46+
/// <inheritdoc/>
47+
public override string ToString() => $"{nameof(DisplayName)}: '{DisplayName}', {nameof(Locale)}: '{Locale}'";
48+
49+
private string DebuggerDisplay => ToString();
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Linq;
3+
using System.Text.Json;
4+
5+
namespace GTranslate.Extensions;
6+
7+
internal static class JsonElementExtensions
8+
{
9+
public static JsonElement FirstOrDefault(this JsonElement element)
10+
=> element.ValueKind == JsonValueKind.Array ? element.EnumerateArray().FirstOrDefault() : default;
11+
12+
public static JsonElement ElementAtOrDefault(this JsonElement element, int index)
13+
=> element.ValueKind == JsonValueKind.Array ? element.EnumerateArray().ElementAtOrDefault(index) : default;
14+
15+
public static JsonElement LastOrDefault(this JsonElement element)
16+
=> element.ValueKind == JsonValueKind.Array ? element.EnumerateArray().LastOrDefault() : default;
17+
18+
public static JsonElement GetPropertyOrDefault(this JsonElement element, string propertyName)
19+
=> element.ValueKind == JsonValueKind.Object && element.TryGetProperty(propertyName, out var value) ? value : default;
20+
21+
[return: NotNullIfNotNull("defaultValue")]
22+
public static string? GetStringOrDefault(this JsonElement element, string? defaultValue = null)
23+
=> element.ValueKind is JsonValueKind.String or JsonValueKind.Null ? element.GetString() ?? defaultValue : defaultValue;
24+
25+
public static int GetInt32OrDefault(this JsonElement element, int defaultValue = default)
26+
=> element.ValueKind == JsonValueKind.Number && element.TryGetInt32(out int value) ? value : defaultValue;
27+
28+
public static bool TryGetInt32(this JsonElement element, string propertyName, out int value)
29+
{
30+
value = 0;
31+
var prop = element.GetPropertyOrDefault(propertyName);
32+
return prop.ValueKind == JsonValueKind.Number && prop.TryGetInt32(out value);
33+
}
34+
35+
public static bool TryGetSingle(this JsonElement element, string propertyName, out float value)
36+
{
37+
value = 0;
38+
var prop = element.GetPropertyOrDefault(propertyName);
39+
return prop.ValueKind == JsonValueKind.Number && prop.TryGetSingle(out value);
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Buffers;
3+
using System.IO;
4+
5+
namespace GTranslate.Extensions;
6+
7+
internal static class ReadOnlySequenceExtensions
8+
{
9+
public static ReadOnlySequence<byte> AsReadOnlySequence(this ReadOnlyMemory<byte>[] chunks) => AsReadOnlySequence(chunks.AsSpan());
10+
11+
public static ReadOnlySequence<byte> AsReadOnlySequence(this ReadOnlySpan<ReadOnlyMemory<byte>> chunks)
12+
{
13+
if (chunks.Length == 0)
14+
{
15+
throw new ArgumentException("Byte array is empty.", nameof(chunks));
16+
}
17+
18+
if (chunks.Length == 1)
19+
{
20+
return new ReadOnlySequence<byte>(chunks[0]);
21+
}
22+
23+
var start = new MemorySegment<byte>(chunks[0]);
24+
var end = start.Append(chunks[1]);
25+
26+
for (int i = 2; i < chunks.Length; i++)
27+
{
28+
end = end.Append(chunks[i]);
29+
}
30+
31+
return new ReadOnlySequence<byte>(start, 0, end, end.Memory.Length);
32+
}
33+
34+
public static Stream AsStream(this ReadOnlySequence<byte> readOnlySequence) => new ReadOnlySequenceStream(readOnlySequence);
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GTranslate.Extensions;
5+
6+
internal static class StringExtensions
7+
{
8+
private static readonly char[] _separators = { '\t', '\r', '\n', ' ' };
9+
10+
// Splits a text into lines of max. 200 chars without breaking words (if possible)
11+
// This algorithm is not as accurate as the one Google uses, but it's good enough
12+
// Google prioritizes maintaining the structure of sentences rather than minimizing the number of requests
13+
public static IEnumerable<ReadOnlyMemory<char>> SplitWithoutWordBreaking(this string text, int maxLength = 200)
14+
{
15+
string[] split = text.Split(_separators, StringSplitOptions.RemoveEmptyEntries);
16+
var current = string.Join(" ", split).AsMemory();
17+
18+
while (!current.IsEmpty)
19+
{
20+
int index = -1;
21+
int length;
22+
23+
if (current.Length <= maxLength)
24+
{
25+
length = current.Length;
26+
}
27+
else
28+
{
29+
index = current.Slice(0, maxLength).Span.LastIndexOf(' ');
30+
length = index == -1 ? maxLength : index;
31+
}
32+
33+
var line = current.Slice(0, length);
34+
// skip a single space if there's one
35+
if (index != -1)
36+
{
37+
length++;
38+
}
39+
40+
current = current.Slice(length, current.Length - length);
41+
yield return line;
42+
}
43+
}
44+
}

GTranslate/GTranslate.csproj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Version>2.1</Version>
4+
<LangVersion>10</LangVersion>
5+
<TargetFrameworks>net7.0-windows</TargetFrameworks>
6+
<Authors>d4n3436</Authors>
7+
<Description>A collection of free translation APIs (Google Translate, Bing Translator, Microsoft Translator and Yandex.Translate). Currently supports translation, transliteration, language detection and text-to-speech.</Description>
8+
<NeutralLanguage>en</NeutralLanguage>
9+
<PackageTags>translator;translator-api;translation;translation-api;tts;text-to-speech</PackageTags>
10+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11+
<PackageProjectUrl>https://github.com/d4n3436/GTranslate</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/d4n3436/GTranslate</RepositoryUrl>
13+
<PackageReleaseNotes>https://github.com/d4n3436/GTranslate/releases</PackageReleaseNotes>
14+
<Copyright>Copyright ©2021 d4n3436</Copyright>
15+
<IncludeSymbols>true</IncludeSymbols>
16+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
17+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
18+
<Nullable>enable</Nullable>
19+
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
20+
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
21+
<EnableNETAnalyzers>True</EnableNETAnalyzers>
22+
<BaseOutputPath>$(SolutionDir)TranslateFileNames\bin\</BaseOutputPath>
23+
</PropertyGroup>
24+
<PropertyGroup>
25+
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
26+
</PropertyGroup>
27+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-windows|AnyCPU'">
28+
<NoWarn>1701;1702;</NoWarn>
29+
</PropertyGroup>
30+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-windows|AnyCPU'">
31+
<NoWarn>1701;1702;</NoWarn>
32+
</PropertyGroup>
33+
</Project>

GTranslate/ILanguage.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace GTranslate;
2+
3+
/// <summary>
4+
/// Represents a language.
5+
/// </summary>
6+
public interface ILanguage
7+
{
8+
/// <summary>
9+
/// Gets the name of this language.
10+
/// </summary>
11+
string Name { get; }
12+
13+
/// <summary>
14+
/// Gets the ISO 639-1 code of this language.
15+
/// </summary>
16+
string ISO6391 { get; }
17+
18+
/// <summary>
19+
/// Gets the ISO 639-3 code of this language.
20+
/// </summary>
21+
string ISO6393 { get; }
22+
}

GTranslate/ILanguageDictionary.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace GTranslate;
5+
6+
/// <summary>
7+
/// Represents a dictionary of languages.
8+
/// </summary>
9+
/// <typeparam name="TCode">The type of codes (or keys) associated with a language.</typeparam>
10+
/// <typeparam name="TLanguage">The type of values that implements <see cref="ILanguage"/>.</typeparam>
11+
public interface ILanguageDictionary<TCode, TLanguage> : IReadOnlyDictionary<TCode, TLanguage>
12+
where TLanguage : ILanguage
13+
{
14+
/// <summary>
15+
/// Gets a language from a language code.
16+
/// </summary>
17+
/// <param name="code">The language code.</param>
18+
/// <returns>The language, or default{TCode} if the language was not found.</returns>
19+
TLanguage GetLanguage(TCode code);
20+
21+
/// <summary>
22+
/// Tries to get a language from a language code, name or alias.
23+
/// </summary>
24+
/// <param name="code">The language code.</param>
25+
/// <param name="language">The language, if found.</param>
26+
/// <returns><see langword="true"/> if the language was found, otherwise <see langword="false"/>.</returns>
27+
bool TryGetLanguage(TCode code, [MaybeNullWhen(false)] out TLanguage language);
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace GTranslate;
4+
5+
internal readonly struct BingCredentials
6+
{
7+
public BingCredentials(string token, long key, Guid impressionGuid)
8+
{
9+
Token = token;
10+
Key = key;
11+
ImpressionGuid = impressionGuid;
12+
}
13+
14+
public string Token { get; }
15+
16+
public long Key { get; }
17+
18+
public Guid ImpressionGuid { get; }
19+
}

0 commit comments

Comments
 (0)