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

Improved fix for Google Lite translator #678

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
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
42 changes: 36 additions & 6 deletions src/ResXManager.Translators/GoogleTranslatorLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ protected override async Task Translate(ITranslationSession translationSession)
var parameters = new List<string?>(30);
// ReSharper disable once PossibleNullReferenceException
parameters.AddRange(new[]
{
"client", "dict-chrome-ex",
{
"client", "gtx",
"dt", "t",
"sl", GoogleLangCode(translationSession.SourceLanguage),
"tl", GoogleLangCode(targetCulture),
"q", RemoveKeyboardShortcutIndicators(sourceItems[0].Source)
});
});
tkefauver marked this conversation as resolved.
Show resolved Hide resolved

// ReSharper disable once AssignNullToNotNullAttribute
var response = await GetHttpResponse(
"https://clients5.google.com/translate_a/t",
"https://translate.googleapis.com/translate_a/single",
parameters,
translationSession.CancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -101,8 +102,37 @@ private static async Task<string> GetHttpResponse(string baseUrl, ICollection<st

#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods => not available in .NET Framework
var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
result = result.Substring(2, result.Length - 4);
return Regex.Unescape(result);
var sb = new StringBuilder();
tom-englert marked this conversation as resolved.
Show resolved Hide resolved
var str_count = 0;
var array_start_count = 0;
using var reader = new JsonTextReader(new StringReader(result));
while (reader.Read())
{
if (reader.Value != null)
{
if (reader.TokenType == JsonToken.String && array_start_count == 3)
{
if (str_count % 2 == 0)
{
sb.Append(reader.Value.ToString());
}
str_count++;
}
}
else
{
if (reader.TokenType == JsonToken.StartArray)
{
array_start_count++;
}
else if (reader.TokenType == JsonToken.EndArray)
{
array_start_count--;

}
}
}
return Regex.Unescape(sb.ToString());
}

[DataContract]
Expand Down
Loading