在flask appbuild中获取当前语言

2024-09-29 19:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Flask Appbuilder编写了一个简单的应用程序,view.py如下所示。它是http://flask-appbuilder.readthedocs.io/en/latest/views.html中exmaple的一部分,在method1中我用我想找到的函数替换了{}。在

我们可以在app(en,fr,ru,…)中更改语言并进行翻译。是否有一个获取当前语言的函数?(当前的_Language())。在

from flask_appbuilder import AppBuilder, BaseView, expose, has_access, ModelView
from app import appbuilder, db
from flask import render_template, g
from flask_babel import Babel
from flask_babel import lazy_gettext as _
from flask_appbuilder.models.sqla.interface import SQLAInterface

class MyView(BaseView):    
  default_view = 'method1'    
  @expose('/method1/')
  @has_access
  def method1(self):
    return Current_Language()

appbuilder.add_view(MyView, "Method1", category='My View')

Tags: 函数fromimportview语言appflaskaccess
2条回答

你的问题有点模棱两可。你指的是当前的服务器端语言还是客户端语言。在

前者:

import locale
locale.getlocale()

后者:

^{pr2}$

您感兴趣的标题是Accept-Language。但是当涉及到以这种方式推断客户机语言时,也有一些警告。见https://www.w3.org/International/questions/qa-accept-lang-locales

appbuilder实例有一个bm属性,它是BabelManager类的一个实例。在

这个类有一个get_locale方法,它返回应用程序使用的当前语言。在

class MyView(BaseView):    
    default_view = 'method1'    
    @expose('/method1/')
    @has_access
    def method1(self):
        return appbuilder.bm.get_locale()

您可以检查project repository.上的BabelManager类的代码

相关问题 更多 >

    热门问题