Home  >  Article  >  PHP Framework  >  The use and optimization of Thinkphp translation interface

The use and optimization of Thinkphp translation interface

咔咔
咔咔Original
2020-08-19 16:53:442704browse

This article provides you with a free translation interface, you can try it when you have time, it is still very good!

Preface

The necessary data in the project needs to be written in a language package, just like the kind that remains unchanged for hundreds of years data, but there is a type of data that is constantly changing during the running of the project.

It is obviously inappropriate for us to write a language package for such data, so we need to use the translation interface to achieve our needs.

1. Brief introduction to the translation interface

Let’s look at a picture first! Let’s take a look at how this translation interface is used in our project.

The use and optimization of Thinkphp translation interface
Insert image description here

This interface has a total of 4 parameters, namely a, f, t, and w.

The meaning of these four parameters is that a is a fixed value, which is fy.

f refers to the language of translation.

t refers to the need to be translated into the language we need.

w refers to the data that needs to be translated.

2. Project actual combat

#The project requirement is to put the box on the left and follow this language after switching languages change. First of all, let me explain that the above text was not written manually, and the html file does not exist. It is configured in the database annotation.

The picture below shows our database creation. Why it is created like this and what are the benefits of creating it this way, I won’t mention it. Every team has its own ideas!

Then we come to the topic

This is the code used. The interface address is the address in postman above. Later, we only need to transmit a piece of data that needs to be translated.

The data implemented in this article is the text on the left side of the above picture, that is, we read the comments from the database and then use the name directly as a column when adding or modifying according to certain rules.

Then you only need to append this comment directly to the back of the translation interface. Provide you with a code for php to initiate a curl request

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #272822; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #272822; border-radius: 5px;"><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">function</span> <span class="hljs-title" style="color: #a6e22e; font-weight: bold; line-height: 26px;">translateRequest</span><span class="hljs-params" style="line-height: 26px;">($url, $data=array<span class="hljs-params" style="line-height: 26px;">()</span>)</span></span>{<br/><br/>        $ch = curl_init();<span class="hljs-comment" style="color: #75715e; line-height: 26px;">//初始化</span><br/>        <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//curl_setopt();//设置</span><br/>        <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//设置</span><br/>        curl_setopt($ch,CURLOPT_URL,$url);   <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//需要获取的 URL 地址</span><br/>        curl_setopt($ch,CURLOPT_HEADER,<span class="hljs-number" style="line-height: 26px;">0</span>);          <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//启用时会将头文件的信息作为数据流输出, 此处禁止输出头信息</span><br/>        curl_setopt($ch,CURLOPT_RETURNTRANSFER,<span class="hljs-number" style="line-height: 26px;">1</span>);  <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//获取的信息以字符串返回,而不是直接输出</span><br/>        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,<span class="hljs-number" style="line-height: 26px;">30</span>); <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//连接超时时间</span><br/>        curl_setopt($ch, CURLOPT_ENCODING, <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">&#39;gzip&#39;</span>);<br/><br/>        <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//避免https 的ssl验证</span><br/>        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">false</span>);<br/>        curl_setopt($ch, CURLOPT_SSLVERSION, <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">false</span>);<br/>        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">false</span>);<br/><br/>        <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">if</span>($data){<br/>            curl_setopt($ch, CURLOPT_POST, <span class="hljs-number" style="line-height: 26px;">1</span>);          <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//post请求</span><br/>            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<span class="hljs-comment" style="color: #75715e; line-height: 26px;">//post参数</span><br/>        }<br/><br/>        <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//执行</span><br/>        $data = curl_exec($ch);<span class="hljs-comment" style="color: #75715e; line-height: 26px;">//执行   不输出  内容返回给它</span><br/>        <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//判断是否请求成功</span><br/><br/>        <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">if</span>(curl_errno($ch)){<span class="hljs-comment" style="color: #75715e; line-height: 26px;">//错误码</span><br/>            <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">echo</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">&#39;curl error: &#39;</span>.curl_error($ch);<span class="hljs-comment" style="color: #75715e; line-height: 26px;">//错误信息</span><br/>        }<br/><br/>        $response = curl_getinfo($ch);<br/><br/>        <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">switch</span>($response[<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">&#39;http_code&#39;</span>]){<br/>            <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">case</span> <span class="hljs-number" style="line-height: 26px;">200</span>:<br/>                <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">return</span> $data;<br/>                <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">break</span>;<br/>            <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">default</span>:<br/>                <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">exit</span>(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">&#39;程序异常&#39;</span>);<br/>        }<br/><br/>        curl_close($ch);<span class="hljs-comment" style="color: #75715e; line-height: 26px;">//关闭</span><br/>    }<br/></code>

After testing, see the effect. The effect is achieved, but you can try it privately! The speed is okay when translating a group of data, but a bit slow when translating several data.

It takes about 3-5 seconds to open the added page. This is definitely not possible, so we need to think of a way to solve this problem. The use and optimization of Thinkphp translation interface

3. Optimize translation speed

In the second step, Kaka finally found that the translation speed was a bit It's slow, especially when there are many fields. So Kaka thought of a way.

These data were translated one by one before, so can we translate them all at once, and then assemble the data ourselves. The use and optimization of Thinkphp translation interface

With this idea in mind, we started to implement it.

What these lines of code ultimately achieve is to put all the field annotations together and separate them with ","The use and optimization of Thinkphp translation interfaceYou can take a look at the printed data. The out field is the translated data. In fact, Don’t even think about it. One translation must be faster than 6 translations.

Then take this set of data, convert it into an array, and reassemble it into the original data. The use and optimization of Thinkphp translation interfaceThere is a small problem here, you can pay attention to it together. What is printed in Chinese is the original data, and what is printed in English is the processed data.

It is obvious that the returned English results do not match the original data. The use and optimization of Thinkphp translation interfaceThe processing here is also relatively simple. If you have a better solution, please see it in the comment area.

The processing method is to save the corresponding index to $needkey before obtaining the Chinese annotation of the database field

and then Redefine a variable fanal so that the index of fanal is equal to v of needkey, and the corresponding value is result[k of needkey]

4. Summary

#The use of such a translation interface is completed, and the project screenshots will not be shown to everyone!

Change multiple translations to one translation to improve the translation speed. After all, it is requesting something from others, and it is definitely not as fast as reorganizing the data ourselves.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always adhered to since his career. I hope that Kaka’s articles in Nuoda Internet can bring you Any help.

The above is the detailed content of The use and optimization of Thinkphp translation interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn