d4n3436 / Fergun

A utility Discord bot written in C# using Discord.Net
MIT License
30 stars 9 forks source link

BingTranslator: error 400 with non-google languajes #7

Closed sanslash332 closed 3 years ago

sanslash332 commented 3 years ago

Hello guys, again:

I'm using on my project your bingTranslator classes, and works awesome, specially with the auto-detect language.

But, with the languages that not share their codes with the google translator, fails when you use it directly, with a error 400.

Specifically, the languages are:

Again, Thanks for your awesome work.

d4n3436 commented 3 years ago

Hello, what is the problem exactly? BingTranslator already converts the Google Translate languages to their Bing Translator equivalent:

// Convert Google Translate language codes to Bing Translator equivalent.
toLanguage = toLanguage switch
{                
    "no" => "nb",
    "pt" => "pt-pt",
    "zh-CN" => "zh-Hans",
    "zh-TW" => "zh-Hant",
    _ => toLanguage
};

Maybe you're getting the language codes in lower-case. If that's the case then you'll have to edit the previous snippet a bit:

toLanguage = toLanguage.ToLowerInvariant();

// Convert Google Translate language codes to Bing Translator equivalent.
toLanguage = toLanguage switch
{                
    "no" => "nb",
    "pt" => "pt-pt",
    "zh-cn" => "zh-Hans",
    "zh-tw" => "zh-Hant",
    _ => toLanguage
};

If you want to convert Bing Translator languages to Google Translate then you simply have to invert the names:

// Convert Bing Translator language codes to Google Translate equivalent.
toLanguage = toLanguage switch
{
    "nb" => "no",
    "pt-pt" => "pt",
     "zh-Hans" => "zh-CN",
    "zh-Hant" => "zh-TW",
    _ => toLanguage
};
sanslash332 commented 3 years ago

Hello @d4n3436

Thanks for your answer :)

After more debug process, I found the error.

My problem was only the toLanguage variable pass through the conversion process, but not the fromLanguage.

I was changing to chinese instead of auto the fromLanguage, but of course, that language didn't have the convert, so it fails.

I added manually the same switch that the toLanguage has to the fromLanguage to have this feature on bot languages and it works like a charm.

How to I can create a pull request to add that small change to the class?

I have first fork all the repository, and make the change to create the pull request?

Again, thanks for your valued support.

d4n3436 commented 3 years ago

I actually added the toLanguage converter on TranslateAsync() just for convenience. Ideally, the person using the API should handle the language code conversions, since BingTranslator should only support Bing-only languages.

With this in mind, I think it will be better to remove the converter from BingTranslator and do the conversions before using the translator.

sanslash332 commented 3 years ago

@d4n3436 maybe. If your think from the lazy side, and your project uses google and bing, have the converrsion directly on the translateAsync option will be OK.

but, for sake of correctness, Bing have to use only bing languages, and the supportedLanguages dictionary on Bing translator, must be complete with codes and named languages, like the google class has.

So, both approaches are interesting :3

thanks

d4n3436 commented 3 years ago

Some time ago I considered adding a new class called SimpleTranslator. This class would encapsulate GoogleTranslateFreeApi (now GTranslate), BingTranslator, their supported languages (codes and names) and it would handle the language conversions for you.

At that time GoogleTranslateFreeApi was completely broken and I was using BingTranslator as a replacement while I was searching for a free Google Translate API that worked. I didn't add SimpleTranslator because it wouldn't make sense to combine a working and a non-working translator.

Now with GTranslate the process will be simple.

sanslash332 commented 3 years ago

Interesting; on the past, I too used GoogleTranslateFreeApi on my project. But currently the main repository of the project is broken.

I found that one Chinese fork works... relative good, using Chinese google's endpoints, but is subject a rate limmit bans. With a significative small amount of request in a minute, you can be baned for a few hours. And is slower than your GTranslate class.

But well. That idea of a simpleTranslator class sounds good; specially if you want add on the future other translator services like yandex, azure, etc.

:)

O, if you want take a look, this is the Chinese fork that I've used.

https://github.com/wadereye/GoogleTranslateFreeApi

d4n3436 commented 3 years ago

@sanslash332 Hey, I was thinking of the idea of having multiple translation APIs inside a simple wrapper, but still allowing to use the translators separately, so I decided to work these days in my new library, now called GTranslate. You can install it from NuGet with the name GTranslate and view the source code here.

GTranslate is a collection of translation APIs. It includes the Google Translate and Bing Translator API that Fergun uses, a new Yandex Translate API (Fergun had one but was very limited) and a new "Translator" class that groups all 3 translators. I rewrote most of the code from scratch and added a proper Language class where you can get all the supported language of all the translators and know whether a translator supports a language (there's even an "emoji" language lol).

Now the translators handles of the translator-specific language code conversions for you, so you no longer need to convert the language codes manually. Feel free to use GTranslate in your project, and report any bugs and suggest stuff in GTranslate's repository :)

sanslash332 commented 3 years ago

Thanks @d4n3436 for these awesome news!

I'll grab it, and check how it works.

Any issues or ideas, will be reported :)

again, thanks!