This article provides you with a free translation interface, when you have time to try or very show ha!

preface

The necessary data in a project needs to be written in language packages, such as data that has not changed for hundreds of years, but there is a certain type of data that changes over the course of the project.

It is obviously not appropriate to write such data as a language package, so we need to use the translation interface to achieve our requirements.

A brief introduction of translation interface

Take a look at a picture first! Take a look at how this translation interface is used in our project.

Insert a picture description here

This interface has four parameters: A, f, t, and w.

What these four parameters mean is that a is a fixed value which is fy.

F refers to the translated language.

T stands for need to be translated into the language we need.

W refers to the data that needs to be translated.

Ii. Actual combat of the project

The project requirement is the box on the left that follows the language change after switching languages.First of all, the text above is not written manually, and the HTML file does not exist. It is configured in the database comments.

Here is our database creation.Why and what are the benefits? I won’t talk about it, but each team has its own ideas!

Then we came to the point

The code used is this, the interface address is the address in the upper postman, after only need to transmit a need to translate the data can be.

The data implemented in this article is the text on the left of the image above, that is, we read the comments out of the database and use the name as a column to add or modify according to certain rules.

You just need to append this comment directly to the translation interface.Curl curl curl curl curl curl curl curl curl curl

public function translateRequest($url, $data=array(a)){



        $ch = curl_init();/ / initialization

        //curl_setopt(); / / set

        / / set

        curl_setopt($ch,CURLOPT_URL,$url);   // The URL to be obtained

        curl_setopt($ch,CURLOPT_HEADER,0);          // Header information will be output as a data stream when this function is enabled

        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  // The obtained information is returned as a string rather than printed directly

        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); // Connection timeout time

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');



        // Avoid HTTPS SSL authentication

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_SSLVERSION, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);



        if($data){

            curl_setopt($ch, CURLOPT_POST, 1);          / / post request

            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);/ / post parameters

        }



        / / execution

        $data = curl_exec($ch);// Execution does not output content back to it

        // Determine whether the request was successful



        if(curl_errno($ch)){/ / error code

            echo 'curl error: '.curl_error($ch);// Error message

        }



        $response = curl_getinfo($ch);



        switch($response['http_code']) {

            case 200:

                return $data;

                break;

            default:

                exit('Program exception');

        }



        curl_close($ch);/ / close

    }

Copy the code

After the test to see the effect, the effect is achieved, but you can privately try ha! It’s ok to translate one set of data, but a bit slow to translate several data.

Add page open time about 3-5s, this is definitely not possible, so we need to think of a way to solve this problem.

3. Optimize translation speed

In the second step, kaka finally found that the speed of translation is a little slow, encountered many fields is ridiculously slow. So Kaka thought of a way.

These data were translated one by one before, so can we also translate them at one time, and then we will assemble the data by ourselves.

And with that in mind, we started doing it.

What these lines end up doing is putting all the field comments together, separated by commasYou can take a look at the printed data, and the out field is the translated data. In fact, you don’t have to think about it. One translation is faster than six.

Then take this group of data in the array and reassemble into the original data is OK.Here’s a little bit of a problem that we can focus on. Chinese printed out is the original data inside, English is the data after processing.

It is obvious that the returned English results do not match the original data.The processing here is also relatively simple, if you have a better solution see the comments section

Save the corresponding index to $needkey before retrieving the Chinese annotation of the database field

Then redefine a variable fanal so that the index of fanal is equal to needkey’s v and the corresponding value is result[Needkey’s K].


Four,

The use of such a translation interface is completed, the project screenshots will not show you ha!

To change multiple translation into one translation and improve the translation speed, after all, is to request other people’s things, certainly not as fast as our own data restructuring.

Finally, I’m going to give you a comparison table of all the languages

Adhering to learning, blogging and sharing is the belief that Kakha has been upholding since she started her career. I hope you can get a little help from my articles on the Internet.