簡體   English   中英

Tornado的模板翻譯函數'_',它來自哪里?

[英]Tornado's templates translate function '_', where does it come from?

我需要修飾默認的tornado.locale.Locale.translate來添加一些額外的邏輯。

在模板中,translate方法是下划線(因為gettext約定要求),但我找不到定義或傳遞的位置(我希望它被指定為模板環境變量別名上述translate方法)。

我的另一個選擇是用我自己的方法替換所有出現的'_'方法,但我更喜歡堅持使用標准符號,所以我不必在之后調整字符串提取。

沒關系,我剛發現它:

在龍卷風的web.py中:

   def render_string(self, template_name, **kwargs):
        """Generate the given template with the given arguments.

        We return the generated string. To generate and write a template
        as a response, use render() above.
        """
        # If no template_path is specified, use the path of the calling file
        template_path = self.get_template_path()
        if not template_path:
            frame = sys._getframe(0)
            web_file = frame.f_code.co_filename
            while frame.f_code.co_filename == web_file:
                frame = frame.f_back
            template_path = os.path.dirname(frame.f_code.co_filename)
        with RequestHandler._template_loader_lock:
            if template_path not in RequestHandler._template_loaders:
                loader = self.create_template_loader(template_path)
                RequestHandler._template_loaders[template_path] = loader
            else:
                loader = RequestHandler._template_loaders[template_path]
        t = loader.load(template_name)
        args = dict(
            handler=self,
            request=self.request,
            current_user=self.current_user,
            locale=self.locale,
            _=self.locale.translate,
            static_url=self.static_url,
            xsrf_form_html=self.xsrf_form_html,
            reverse_url=self.application.reverse_url
        )
        args.update(self.ui)
        args.update(kwargs)
        return t.generate(**args)

args字典有_=self.locale.translate ; 所以我可以在我的處理程序中裝飾該對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM