Update: the method we described in 2013 in A free alternative to the Google Translate API, which asked for the translations with file_get_contents() against an undocumented access point, has not worked for years.

Google closed that door by adding a signed tk parameter to the request that only its own JavaScript knows how to calculate, and besides, that access went against the service’s terms, so recommending it today would be bad practice.

That entry is still in its place, because it explains how we ended up there, and this one tells you what options there are now and leaves you a replacement function that you can use.

Background

Some time ago a client asked us to translate his catalogue’s descriptions on the fly and completely automatically from Spanish into other languages such as English, German, Russian or Chinese, and the official API had just changed its terms and was charging twenty dollars for every million characters translated, so we did not feel like sending him to the till and we set about looking for a free alternative.

After combing the Internet we found a lead that put us on the trail and, thanks to Rupesh Patel, we put together that small piece of PHP code which queried the same address the translator uses from its web interface and kept the already translated text.

It worked for years, until it stopped working.

Why the Google Translate API trick no longer works

The address we were hitting was never a public API but the internal access point the translator uses from the browser, and Google protected it with a tk parameter that is a signature calculated over the text itself and over a seed that changes on its own.

That signature is generated by the page’s JavaScript, so reproducing it from PHP means chasing somebody else’s code that can change any Tuesday morning and leave your catalogue half translated.

And even if you managed it, the service’s terms forbid automated access by that route, so the risk is not only a technical one.

What alternatives there are now

The landscape has changed quite a bit since 2013, and today there are at least three reasonable ways of translating automatically without building a shortcut, each with its own small print:

  • LibreTranslate: it is the real heir to the original idea, an open source translation engine under the AGPLv3 licence that you can install on your own server. It is worth not confusing the two things that go by the same name: what is free is the software, not the service. Self-hosting it has no fee and no invoice and does not send anybody your clients’ texts, although you pay for the server yourself in memory and in processor and the quality falls below that of the commercial engines, above all in less common languages. Its hosted API, on the other hand, is paid for and has no free plan: as of today it advertises $29 a month for the Pro plan and $58 a month for the Business plan, both with a limit of 2,000 characters per request.
  • Google Cloud Translation: the official API is the legitimate route for what that entry was trying to do through the back door. Its published rate includes 500,000 characters a month at no cost, in the form of a $10 credit that does not carry over from one month to the next, and from there on it charges $20 per million characters. It requires a project, a key and an active billing account, so going over the quota does not return an error but a charge.
  • DeepL: the quality is noticeably higher in the European languages, although its catalogue of languages is shorter than Google’s. Its free tier for developers, DeepL API Free, used to translate 500,000 characters a month, but DeepL has reorganised its API plans and that tier is no longer taken out as it used to be, so check its pricing page before counting on it. In any case it does not spare you the registration or the key either.

The quota and price figures change every so often, so check them in the provider’s documentation before building anything on top. The ones we give here are those published in September 2026.

And if what you want is to translate your whole WordPress

When the aim is not to translate a single field but the complete site, the job stops being calling an engine and becomes managing the translated versions of each piece of content, and a plugin solves that much better.

Here we use WPML, which for years has brought automatic translation by credits and lets you review each text before publishing it, which is exactly what the 2013 contraption was missing, and whose quirks we already talked about in Adding WPML compatibility to a theme.

The free alternative is Polylang, which manages the languages at no cost but leaves the automatic translation in the hands of its paid add-on or of your own code.

Down to work

For the specific case of that entry, translating descriptions on the fly from PHP, the direct replacement for that code is a call to LibreTranslate with wp_remote_post(), the service’s address and the key in two constants and the result saved in a transient so as not to translate the same thing twice.

If you self-host the engine, the address is that of your own server and the key may be unnecessary; if you go with its hosted API, you will have to register, pay for the plan and put the key they give you there.

Tested: this code has been checked with PHP 8.4 and run against a self-hosted LibreTranslate, which returns the translation whether or not the api_key field is sent to it. Its error paths have also been tested: faced with a 403, with a response that is not JSON and with a server that does not answer, it returns the original text. Even so, test it in your own environment before putting it into production.

Specifically you have to add this small piece of PHP code to the functions.php file of your child theme:

Copy to clipboard

Thanks to it each text is translated only once and the following calls come out of the transient, which is what really saves you quota, response time and trouble with the provider.

The function is deliberately distrustful, it checks is_wp_error(), it checks the response code and it checks that the JSON brings what it expects, and at the slightest doubt it returns the original text, because an untranslated description is a small problem and an empty description is a big one.

The service’s address and the key live in constants, better defined in wp-config.php, so that they do not end up travelling in the theme or in the repository.

Conclusion

Thanks to an engine you can host yourself and a transient cache, today a whole catalogue can be translated without paying per character and without depending on a shortcut that can fall over any day and that, besides, we should never have recommended.

We hope you find this little review useful and we look forward to your comments and contributions.

Related posts

Comments

Suscríbete
Notificar de
0 Comments
Más antiguos
Más recientes Más votados

In this post

Products and services

Latest posts

Latest comments

Subscribe!

Lista de correos de APG

DATA PROTECTION: In accordance with data protection regulations, we provide you with the following processing information: (+)

  • Responsible: Art Project Group.
  • Treatment purposes: to respond to queries or any type of request made by the user through any of the forms of contact that are made available on the website. Sending communications about products or services (with your consent).
  • Your rights: access, rectification, portability, deletion, limitation and opposition. More information about the treatment in the Privacy Policy.

What we post on !

Se ha publicado #WooCommerce 11.2.2: actualización de seguridad que refuerza las API REST y Store, mejora la validación de CSV y corrige problemas en pagos y pedidos. Incluye cambios en la base de datos.

¡Actualiza cuanto antes!

13 hours ago