In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
I. the method of language internationalization of Tornado
Tornado has been messing with internationalization for a while, and the official documentation for this part of Tornado is too poor. So write down how to use tornado combined with gettext to do internationalization.
The first step is to create a. / locales/zh_CN/LC_MESSAGES folder under the project path.
The second step is to use xgettext or poedit to create a po file under the folder in the first step, such as messages.po, which I create in poedit, which is a little more convenient than xgettext.
The third step is to edit the messages.po file. Of course, the po file has its own specific format and needs to be written in its own format.
Msgid "" msgstr "" Project-Id-Version:\ n "" POT-Creation-Date:\ n "" PO-Revision-Date:\ n "" Last-Translator:\ n "" Language-Team:\ n "" MIME-Version: 1.0\ n "" Content-Type: text/plain " Charset=UTF-8\ n "" Content-Transfer-Encoding: 8bit\ n "Language: zh_CN\ n" X-Generator: Poedit 1.8.4\ n "msgid" Sign in "msgstr" login "msgid" Sign out "msgstr" login "msgid" Username "msgstr" username "msgid" Password "msgstr" password "
Msgid is the original text content in the web page, and msgstr is the content to be replaced. The new content can be appended directly to the editor with msgid and msgstr.
The fourth step is to modify the HTML page template
{% include'. / header.html'%} {{_ ("Sign in")}} {{_ ("Username")}} {{_ ("Password")}} {% module xsrf_form_html ()%} {% include'. / footer.html'%}
The {{_ ("Sign in")}} in html is the content that needs to be found and replaced by gettext.
The fifth step is to add the method supported by gettext in the tornado main file.
Import osimport tornado.autoreloadimport tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.locale'''...'''if _ _ name__ = ='_ main__': tornado.locale.set_default_locale ('zh_CN') tornado.locale.load_gettext_translations ('. / locales', 'messages') server = tornado.httpserver.HTTPServer (application) server.listen (20000) loop = tornado.ioloop.IOLoop.instance () tornado.autoreload.start (loop) loop.start ()
Because of the ubuntu system I use, the server side is forced to use en_US encoding, so I force set_default_locale ('zh_CN') as debugging, and then use tornado.locale.load_gettext_translations ('. / locales', 'messages') to read the mo file of the messages project under the locales folder.
Step 6, add locale.translate to the Handler you wrote.
Class BaseHandler (tornado.web.RequestHandler): def get_current_user (self): _ = self.locale.translate user = self.get_secure_cookie ('username') return userclass SigninHandler (BaseHandler): def get (self): self.render (' User/sign_in.html') def post (self): username = self.get_argument ('username') password = self.get _ argument ('password') if username =' xianglei': self.set_secure_cookie ('username') 'xianglei') self.redirect (' /') class SignoutHandler (BaseHandler): def get (self, * args, * * kwargs): self.clear_all_cookies () self.redirect ('/')
_ = self.locale.translate,self.locale.translate is actually a method, so put this method in _ this object, and then the _ method is automatically substituted into the template to replace _ ("Sign in"), so the {{_ ("Sign in")}} actually written in the template actually asks Tornado to execute the tornado.locale.translate () method. In that case, if I remove the previous set_default_locale (), the page displays Sign in in English, plus login in Chinese.
Similarly, Tornado can also use a csv file as the base dictionary for translation, which is csv by default.
Second, Tornado executes the RESTful command as HTTP client.
The Tornado asynchronous client has been recorded before, and yesterday I debugged the RESTful client that uses Tornado to do HDFS and YARN. HDFS RESTful mode, can not use asynchronous, need to use Tornado synchronous client can. The RESTful management of HDFS and YARN requires the use of four query modes of HTTP, GET,POST,PUT,DELETE. PUT and DELETE are very similar to POST and GET.
such as
Class MakeDirectoryHandler (BaseHandler): @ tornado.web.authenticated def post (self): host = self.get_argument ('host') port = self.get_argument (' port') directory = self.get_argument ('directory') username = self.get_secure_cookie (' username') base_url = 'http://'+host+':'+port+'/webhdfs/v1'+directory+'?op=MKDIRS&user.name= '+ username put_body = dict () put_body [' op'] = 'MKDIRS' put_body [' user.name'] = username put_body = urllib.urlencode (put_body) try: http = tornado.httpclient.HTTPClient () response = http.fetch (tornado.httpclient.HTTPRequest (url=base_url) Method='PUT', body=put_body,) self.write (response.body) except tornado.httpclient.HTTPError, e: self.write ('{"errcode": "'+ str (e). Replace ('\ nbread,') +'"}')
The MKDIRS method of HDFS is placed in the PUT group, so the submitted parameters need to be encoded with urlencode and then PUT to the RESTful interface.
And DELETE is.
Class RemoveHandler (BaseHandler): @ tornado.web.authenticated def post (self): host = self.get_argument ('host') port = self.get_argument (' port') filename = self.get_argument ('filename')''If recursive = true, it use to remove whole directory If recursive = false, it use to remove a file or an empty directory The argument must be string. '' Recursive = self.get_argument ('recursive') username = self.get_secure_cookie (' username') base_url = 'http://'+host+':'+port+'/webhdfs/v1'+filename+'?op=DELETE&recursive='+recursive+'&user.name='+username try: http = tornado.httpclient.HTTPClient () response = http.fetch (tornado.httpclient.HTTPRequest ( Url=base_url Method='DELETE',) self.write (response.body) except tornado.httpclient.HTTPError, e: self.write ('{"errcode": "'+ str (e). Replace ('\ nbread,') +'"}')
Like GET, DELETE does not need to encapsulate passing parameters.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.