推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
aragakiiyui
V2EX  ›  Python

关于用 flask 进行网页语言切换的问题

  •  
  •   aragakiiyui · Oct 27, 2015 · 4797 views
    This topic created in 3878 days ago, the information mentioned may be changed or developed.

    问题是这样的:
    我给网页做了两套前端页面,简体版的和繁体版的。我目前是想在登录页面给了一个语言选择的选项。然后将选则的值传到后端,后端根据这个值判断返回繁体或简体版视图,那么请问要如何传递这个值?我把这个存在 session 中合理吗?!

    17 replies    2015-10-28 11:43:55 +08:00
    onlyxuyang
        1
    onlyxuyang  
       Oct 27, 2015 via Android
    用 post 或者 get 传啊 存 session 里我觉得 ok
    tonic
        2
    tonic  
       Oct 27, 2015
    合理啊
    fy
        3
    fy  
       Oct 27, 2015
    合理 + 1 ,但为啥不用 i18n 之类的办法呢?
    aragakiiyui
        4
    aragakiiyui  
    OP
       Oct 27, 2015
    @fy 我稍微看了一下,貌似转繁体没这么容易啊。。繁体好坑爹啊。。。还不如做两套。。
    strahe
        5
    strahe  
       Oct 27, 2015
    @strahe i18n
    aragakiiyui
        6
    aragakiiyui  
    OP
       Oct 27, 2015
    @onlyxuyang
    @tonic 下一步的话就是返回视图了,现在有两个方案:
    第一是在每个 route 函数中进行语系判断,这样好麻烦,每个 route 都要改。
    还有一个想法是通过装饰器来实现动态切换模版。现在我简体版的视图都是以 xxx.html 结尾返回,然后繁体版的视图都是以 xxx_tw.html 结尾。这个不知道有什么办法实现不?
    junnplus
        7
    junnplus  
       Oct 27, 2015
    @aragakiiyui 装饰器+1
    aragakiiyui
        8
    aragakiiyui  
    OP
       Oct 27, 2015
    @junnplus 我想知道要怎么实现。。。。
    junnplus
        9
    junnplus  
       Oct 27, 2015
    判 session 里面的语系,改 template 不就可以了么
    tonic
        10
    tonic  
       Oct 27, 2015
    @aragakiiyui 或者直接搞个新的 `render_template` 啊, 里面判断是什么语系就给什么模板加什么后缀, 然后调用普通的 `render_template`
    wizardforcel
        11
    wizardforcel  
       Oct 27, 2015
    第一种实现方法是做两个 template 根据参数来渲染。

    第二种是做两个字典 渲染的时候传其中一个进去 然后 template 里面不要硬编码 而是从字典里取
    onlyxuyang
        12
    onlyxuyang  
       Oct 27, 2015 via Android
    @aragakiiyui 能渲染好以后调用 google 翻译简转繁么 …… just kidding 2333333333
    ericls
        13
    ericls  
       Oct 28, 2015
    多语言问题 还是用 i18n + i10n 做吧!

    flask-babel
    v2gba
        14
    v2gba  
       Oct 28, 2015
    gaotongfei
        15
    gaotongfei  
       Oct 28, 2015 via iPad
    楼上+1
    aragakiiyui
        16
    aragakiiyui  
    OP
       Oct 28, 2015
    @junnplus
    @tonic 我有想过,但是并不是所有的 route 都要返回 template , 对于不同的请求,或者是 ajax 请求,返回的东西都是不一样的,同时可能一个 route 会返回不同的 template ,这样装饰器貌似有点难写,而且可能需要写多个不同的装饰器了。。。所以感觉很头疼。。。昨天要交货,目前是写了一个函数,在所有返回 templete 的地方都通过这个函数来判断 session ,然后返回 template....几乎所有视图要手动改啊。。。。心塞
    junnplus
        17
    junnplus  
       Oct 28, 2015 via Android
    改 template 而不是返回 template
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 19:53 · PVG 03:53 · LAX 12:53 · JFK 15:53
    ♥ Do have faith in what you're doing.