web.py框架之i18n支持
2023-09-14 08:57:48 时间
问题:
在web.py的模板文件中, 如何得到i18n的支持?
Solution:
项目目录结构:
proj/ |- code.py |- i18n/ |- messages.po |- en_US/ |- LC_MESSAGES/ |- messages.po |- messages.mo |- templates/ |- hello.html
文件: proj/code.py
#!/usr/bin/env python # encoding: utf-8 import web import gettext urls = ( '/.*', 'hello', ) # File location directory. curdir = os.path.abspath(os.path.dirname(__file__)) # i18n directory. localedir = curdir + '/i18n' gettext.install('messages', localedir, unicode=True) gettext.translation('messages', localedir, languages=['en_US']).install(True) render = web.template.render(curdir + '/templates/', globals={'_': _}) class hello: def GET(self): return render.hello() # 使用内建的HTTP服务器来运行. app = web.application(urls, globals()) if __name__ == "__main__": app.run()
模板文件: proj/templates/hello.html.
$_("Message")
创建一个locale目录并使用python2.6内建的pygettext.py从python脚本和模板文件中导出翻译:
shell> cd /path/to/proj/ shell> mkdir -p i18n/en_US/LC_MESSAGES/ shell> python /path/to/pygettext.py -a -v -d messages -o i18n/messages.po *.py templates/*.html Working on code.py Working on templates/hello.html
你将会得到pot file: i18n/messages.po. 它的内容和下面的差不多 (‘msgstr’包含了翻译后的信息):
# 文件 code.py:40 msgid "Message" msgstr "This is translated message in file: code.py."
拷贝文件’i18n/messages.po’到目录’i18n/en_US/LC_MESSAGES/’下, 然后翻译它. 使用gettext包的msgfmt工具或者使用python2.6内建的’msgfmt.py’文件将一个pot文件编译称mo文件:
shell> msgfmt -o i18n/en_US/LC_MESSAGES/messages.mo i18n/en_US/LC_MESSAGES/messages.po
运行web.py的服务器:
shell> cd /path/to/proj/ shell> python code.py http://0.0.0.0:8000/
打开你的浏览器, 比如说firefox, 然后访问地址: http://192.168.0.3:8000/, 你将会看过翻译过的信息.
相关文章
- 推荐一款强大的轻量级模块化WEB前端快速开发框架--UIkit
- 使用eclipse和maven生成java web程序war包
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续
- OSGI开发web应用
- PHP做Web开发的MVC框架(Smarty使用说明 )
- 手机H5 web调试利器——WEINRE (WEb INspector REmote)
- Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面
- Web前端 -- Webpack
- [Whole Web, Nods.js, PM2] Passing environment variables to node.js using pm2
- ASP.NET Web API路由系统:Web Host下的URL路由
- 使用 ABAP 开发的一个基于 Web Socket 的小工具,能提高程序员日常工作效率
- [LINK]常用移动web开发框架研究分析
- 浅谈 Fiori Fundamentals 和 SAP UI5 Web Components 的关系
- 【异常】运行SpringBoot程序提示异常,Web server failed to start. Port XXX was already in use.
- 基于JAVA实现的WEB端UI自动化 - WebDriver框架篇 - ant使用 - ant调用email 自动发送邮件
- Go组件学习——Web框架Gin
- Web端server推送技术原理分析及dwr框架简单的使用
- Web测试中,各类web控件测试点总结
- MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。
- go语言web开发系列之九:gin框架中用bigcache做进程内缓存
- WEB框架之Tornado
- 从零实现Web框架Geo教程-错误恢复-07
- 从零实现Web框架Geo教程-分组控制-04
- 从零实现Web框架Geo教程-Http基础-01