Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate Cookbook with Google Translate or OpenAI ChatGPT
#2
The following code uses the Edge translation engine, without the need for an API, and the results are pretty good. Can it be added to the above code?
These utilize common C# code, but if we incorporate some functions from the LA library, the code would appear more elegant
 
Code:
Copy      Help
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        string authCode = await GetAuthCode();
        if (authCode != null)
        {
            string[] param = { "{\"Text\": \"你好\"}" };
            string language = "en";
            string url = $"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to={language}&includeSentenceLength=true";

            try
            {
                var client = new HttpClient();
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {authCode}");
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

                var body = $"[{param[0]}]";
                var content = new StringContent(body, Encoding.UTF8, "application/json");

                var response = await client.PostAsync(url, content);

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(result);
                }
                else
                {
                    Console.WriteLine($"Translation request failed. Status code: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Translation request failed. Error: {ex.Message}");
            }
        }
    }

    static async Task<string> GetAuthCode()
    {
        try
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

                var response = await client.GetAsync("https://edge.microsoft.com/translate/auth");

                if (response.IsSuccessStatusCode)
                {
                    return (await response.Content.ReadAsStringAsync()).Trim();
                }
                else
                {
                    Console.WriteLine($"Failed to access authentication endpoint. Status code: {response.StatusCode}");
                    return null;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
            return null;
        }
    }
}


Messages In This Thread
RE: Translate Cookbook with Google Translate or OpenAI ChatGPT - by Davider - 03-02-2024, 09:32 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)