sienori / simple-translate

WebExtensions for translating text on web pages
http://simple-translate.sienori.com/
Mozilla Public License 2.0
1.12k stars 126 forks source link

Changes in Google Translator API (Maybe) #506

Closed pouyacodes closed 1 year ago

pouyacodes commented 1 year ago

Recently I tried to use the extension and It doesn't work. I wonder about the cause of this problem. If you open the Console tab of the browser there is an error for the extension:

TypeError: e.terms is undefined (content.js)

Looks like the extension can not read terms property from e object. With a bit of search and exploration in the source code I realized that e object and terms come from Google Translate API and more specifically sendRequestToGoogle function in the source code.

The extension uses this Google Translator API endpoint and populate the required field and then fetchs data. Then function tries to read some field like sentences which contains the translation(s) and dict for "word vocabulary" or something.

Then I tried to send some random requests to this endpoint and check the result with code to make sure that the function read proper values from the data. For example:

$ curl "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=fa&dt=t&dt=bd&dj=1&q=Asked"

and the response is:

{
  "sentences": [
    {
      "trans": "پرسیده شد",
      "orig": "Asked",
      "backend": 3,
      "model_specification": [
        {}
      ],
      "translation_engine_debug_info": [
        {
          "model_tracking": {
            "checkpoint_md5": "982c75c78c6c8e6005ec3a4021a7f785",
            "launch_doc": "tea_GrecoIndoEuropeA_en2elfahykakumksq_2021q3.md"
          }
        }
      ]
    }
  ],
  "dict": [
    {
      "pos": "adjective",
      "entry": [
        {
          "word": "خواسته",
          "reverse_translation": [
            "asked",
            "demanded",
            "requested",
            "wanted",
            "required",
            "desired"
          ],
          "score": 0.10215643
        }
      ],
      "base_form": "asked",
      "pos_enum": 3
    }
  ],
  "src": "en",
  "spell": {}
}

But there is no terms field in the result. So I think perhaps Google changes something in the API.

Also, my browser is Mozilla Firefox and the extension version that I use is 2.8.1.

Thanks!

ttrasn commented 1 year ago

firefox-temp.zip chrome-temp.zip I raised a PR for fixing this issue, but if you want to use it and it blocks you, you can manually run the temp version. Load on the browsers: https://github.com/sienori/simple-translate#load-the-extension-in-chrome

pouyacodes commented 1 year ago

Thanks! :heart: Looks like works and the issue is resolved. :+1:

Do I have to close the issue right now or wait until the next official release and your PR merged?

ttrasn commented 1 year ago

that file was a temporary fix, but the issue still exists until the changes are fixed. so it is better to wait.

pouyacodes commented 1 year ago

Yes. Thanks.

sienori commented 1 year ago

This seems to have been fixed by a change in Google's API. Simple Translate also fixed with https://github.com/sienori/simple-translate/pull/507.