📜 ⬆️ ⬇️

Unique content. Translation into English and back.

Personally, I know at least two ways to translate text. I'll start with the first one.

These are yahoo pipes. I dashed off the simplest pipe to translate text from Russian to English and back. And everything is good, but there are two problems. The first is that when translating, Yahu stupidly removes all html tags. And how to restore them is a separate question. I would even say a non-trivial task. Well, the second - the translation module is sometimes corny does not work. I found complaints in the support of this issue on the forum about yahoo pipes, and it seemed to fix everything. But I still have about 6 launches out of 10 pipe launches at different times of the day.

Summarizing, we can say that yahoo pipes are a good and fast tool, but I do not use it. Maybe you are more lucky, but for me the presence of tags and the stable operation of the service are very critical.
')
We approach the second method - through the AJAX API from Google. The method itself is not very usable, some automation is needed. Therefore, a small script was organized. I left set_time_limit (5) at the very beginning for debugging. This is to check everything first, and only then run for a day (well, or someone like that), removing set_time_limit (0) restrictions.

And there are two more subtleties, without which the script will not work. First, it is assumed that all input and output lines will be in unicode. So if you edit, make sure that your editor can save to UTF-8 (I use notepad ++). And the second - to work with the database you need the dbsimple library. Well, that is, I use it, and you can easily adapt all its challenges to your needs. Everything is simple and transparent.

The Google API has one significant limitation - the length of the string that you will translate. It seems like it is 700 characters, but sometimes errors fall out when requesting a line feed of 600 characters, and sometimes 690 characters each pass normally. The pattern of phenomena could not be established. It has been empirically established that everything works with a string of up to 545 characters. I stopped at it.

The main function in the script is string translate (string $ src, string $ langpair).
string $ src is the string to translate (full-length string)
string $ langpair is a translation language pair. For example, "ru% 7Cen". It is in this form, since this line is substituted without changes in the url request.

Well, here. The string is translated once there, and the second time - back. The script is small, elegant, open and even somehow commented on there. Any changes and additions are welcome.

In the merits of the script that I so need. This is the reliability and preservation of tags. But the execution time leaves much to be desired. Although I did not try to run it on a hosting - only at home on a local host, on a not very thick dedicated line.

PS For myself in the process of writing a script highlighted one thing. Unique content by turning it into something unreadable is not a trump. Much better figachit mashaps. =) But the tool remained, therefore I share.

This is a slightly modified article from my blog .

Source: https://habr.com/ru/post/30330/


All Articles