Use Translate ToolKit 2.5.0 API to build Flask web app

Translate ToolKit 2.5.0 API is a translation of the document conversion tools for industry. Converting json e.g. html or the like to use PO file for translation. As used herein Flask web framework to achieve the basic functions Translate ToolKit api's.

It was a few days ago written request within three days of use api to achieve a webApp, I was half asleep, "Lay Dying" (which is really out of sorts for a while) completed the task, (although there is no written follow-up, still did not work ~ well, do not nonsense, that is entered.

In my original open-source Chinese blog: https://my.oschina.net/finchxu/blog/3217430
project easily download to view in my github https://github.com/finch-xu/f-tt

0. environment:

pip install flask translate-toolkit environment uwsgi the like Python3

1. functions to achieve:

This api is primarily a function of the converter, and comes with some tools, here simply to achieve a txt file transfer po file, json file transfer file po, po comprehensive data file statistics these three functions.

1.a. txt / json file transfer po respectively call txt2po / json2po, take a look at how the API documentation is written:

Official Documents

Here playing open file, and then call txt2po, his internal principle contains readlines there are other methods of judgment way split.

from translate.convert import txt2po, json2po, html2po

@app.route('/convertfile/<path:filename>')
def convert_file(filename):
    #给输出的文件设定po拓展名
    filenameOut = os.path.splitext(filename)[0] + '.po'
    #判断拓展名,来进行不同的转换操作
    key = os.path.splitext(filename)[-1][1:]
    if key == 'txt':
        aa = open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb+')
        bb = open(os.path.join(app.config['CONVERT_FOLDER'], filenameOut), 'wb+')
        txt2po.run_converter(aa,bb,template_file=None,duplicatestyle='msgctxt',encoding='utf-8',flavour='plain',no_segmentation=False)
        aa.close()
        bb.close()
        return redirect(url_for('manage_file'))
    if key == 'json':
        aa = open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb+')
        bb = open(os.path.join(app.config['CONVERT_FOLDER'], filenameOut), 'wb+')
        json2po.convertjson(aa, bb, template_file=None, pot=False, duplicatestyle='msgctxt',dialect='default', filter=None)
        aa.close()
        bb.close()
        return redirect(url_for('manage_file'))
    return redirect(url_for('manage_file'))

1.b. Tools in here to achieve a count function, statistical information on the number of paragraphs of the document, the number of characters, such as the degree of completion of the translation.

pocount achieve

Here called pocount.calcstats_old (), to view the source code

#查看源码
def calcstats_old(filename):
    ...
    ...
    ...
    sourcewords = lambda elementlist: sum(wordcounts[id(unit)][0] for unit in elementlist)
    targetwords = lambda elementlist: sum(wordcounts[id(unit)][1] for unit in elementlist)
    stats = {}
    ...
    ...
    ...
    # words
    stats["translatedsourcewords"] = sourcewords(translated)
    stats["translatedtargetwords"] = targetwords(translated)
    stats["fuzzysourcewords"] = sourcewords(fuzzy)
    stats["untranslatedsourcewords"] = sourcewords(untranslated)
    stats["reviewsourcewords"] = sourcewords(review)
    stats["totalsourcewords"] = (stats["translatedsourcewords"] +
                                 stats["fuzzysourcewords"] +
                                 stats["untranslatedsourcewords"])
    return stats

So we called directly, then returned to the front end of a dictionary

from translate.tools import pocount
#统计功能
@app.route('/count/<path:filename>')
def count_file(filename):
    countfilename = os.path.join(app.config['CONVERT_FOLDER'],filename)
    state = pocount.calcstats_old(countfilename)
    return render_template("countinfo.html", state=state, filename=filename)

The front end to get the dictionary, extracted data needs just fine

<h2>PO文件内容统计信息</h2>
    <table border = 1>
            <tr>
                <th>{{filename}}</th>
                <th> Strings </th>
                <th>Words (source)</th>
                <th>Words (translation)</th>
            </tr>
        <tr>
            <td>Translated:  </td>
            <td> {{state["translated"]}}( {{state["translatedsourcewords"] * 100 / state["total"]}}%)</td>
                    <td>{{state["translatedsourcewords"]}}( {{state["translatedsourcewords"] * 100 / state["totalsourcewords"]}}%)</td>
                    <td>{{state["translatedtargetwords"]}}</td>
        </tr>
        <tr>
            <td>Fuzzy:</td>
            <td>{{state["fuzzy"]}}( {{state["fuzzy"] * 100 / state["total"]}}%)</td>
                    <td> {{ state["fuzzysourcewords"] }}( {{state["fuzzy"] * 100 / state["totalsourcewords"]}}%) </td>
            <td>n/a</td>
        </tr>
    ...
    ...
    ...
    </table>

2. Here again PYCharm start debugging on the overall project uploaded to my github https://github.com/finch-xu/f-tt

It should be noted that the project being written only to upload utf8 file, if the file upload other coding gbk other projects will encounter an error. If there is Chinese, convert the file to utf8 in Chinese. (Notepad ++ tap can be converted, self-Baidu)

#Windows10下调试,注入环境变量
#这里用hello.py这个文件作为app启动文件
set FLASK_APP=hello.py
#设置debug网页显示
set FLASK_DEBUG=1
#启动
Flask run

This time to open a web browser you will find

3. deployed in a production environment

Use Nginx listening uWSGI to run python web app. First, write uwsgi.ini profile. Upload the project to install ubuntu after uwsgi and nginx, here to note that if in the same time there python2.7 and ubuntu python3, then the configuration file to be written into python3, if only python3, then the default written in python just fine, after all, the project is Python3.7 written.

Here set the local port 622 for listening using nginx

[uwsgi]
socket = 127.0.0.1:622
chdir = /home/ubuntu/f-tt/flaskr/
wsgi-file = /home/ubuntu/f-tt/flaskr/hello.py
callable = app
processes = 1
threads = 1
logto = /home/ubuntu/uwsgilogs/%n.log
#这里我的服务器上有python2和3,所以这里要写python3,如果你只有一个,那么就不需要写3或者2
plugins = python3

Nginx modify configuration files

sudo vim /etc/nginx/sites-available/default

server {
	listen 5000;

	root /var/www/html;

	server_name ip或者你的域名;

	location / {
		include /etc/nginx/uwsgi_params;
		uwsgi_pass 127.0.0.1:622;
	}

Start the project, if you encounter error 500 series, you can view the log in the following directory, usually prompted to insufficient permissions (sudo solutions) or repeatedly start and the like, or Python project some of the error itself, in short, very clearly written log

sudo uwsgi /home/ubuntu/f-tt/flaskr/uwsgi.ini -d /home/ubuntu/f-tt/flaskr/logs/log.log

Other file upload and download functions and other content can refer to my GitHub.

We're done!

Guess you like

Origin www.cnblogs.com/finch-xu/p/12654797.html