【问题标题】:Tornado's templates translate function '_', where does it come from?Tornado的模板翻译函数'_',它从何而来?
【发布时间】:2012-09-24 15:17:56
【问题描述】:

我需要装饰默认的tornado.locale.Locale.translate 以添加一些额外的逻辑。

在模板中,翻译方法是一个下划线(正如 gettext 约定所要求的那样),但我找不到定义或传递的地方(我希望它被指定为模板环境变量别名上述翻译方法) .

我的另一个选择是用我自己的方法替换所有出现的“_”方法,但我更愿意坚持使用标准符号,这样我就不必在之后调整字符串提取。

【问题讨论】:

    标签: python templates internationalization tornado


    【解决方案1】:

    没关系,我刚刚找到它:

    在 tornado 的 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;所以我可以在我的处理程序中装饰那个对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 2012-10-01
      • 1970-01-01
      相关资源
      最近更新 更多