Skip to content
Newer
Older
100644 97 lines (74 sloc) 2.23 KB
1708052 @ohsc edit
ohsc authored
1 ---
2 layout: default
3 title: i18n support in template file
4 ---
5
6 # i18n support in template file
7
5217a95 edit
David Xie authored
8 ## 模板文件中的i18n支持
1708052 @ohsc edit
ohsc authored
9
5217a95 edit
David Xie authored
10 ### 问题:
1708052 @ohsc edit
ohsc authored
11
5217a95 edit
David Xie authored
12 在web.py的模板文件中, 如何得到i18n的支持?
1708052 @ohsc edit
ohsc authored
13
5217a95 edit
David Xie authored
14 ### Solution:
15
16 项目目录结构:
1708052 @ohsc edit
ohsc authored
17
18 <pre>
19 proj/
20 |- code.py
21 |- i18n/
22 |- messages.po
23 |- en_US/
24 |- LC_MESSAGES/
25 |- messages.po
26 |- messages.mo
27 |- templates/
28 |- hello.html
29
30 </pre>
31
5217a95 edit
David Xie authored
32 文件: proj/code.py
1708052 @ohsc edit
ohsc authored
33
34 <pre>
35 #!/usr/bin/env python
36 # encoding: utf-8
37
38 import web
39 import gettext
40
41 urls = (
42 '/.*', 'hello',
43 )
44
45 # File location directory.
46 curdir = os.path.abspath(os.path.dirname(__file__))
47
48 # i18n directory.
49 localedir = curdir + '/i18n'
50
51 gettext.install('messages', localedir, unicode=True)
52 gettext.translation('messages', localedir, languages=['en_US']).install(True)
53 render = web.template.render(curdir + '/templates/', globals={'_': _})
54
55 class hello:
56 def GET(self):
57 return render.hello()
58
5217a95 edit
David Xie authored
59 # 使用内建的HTTP服务器来运行.
1708052 @ohsc edit
ohsc authored
60 app = web.application(urls, globals())
61 if __name__ == "__main__":
62 app.run()
63 </pre>
64
5217a95 edit
David Xie authored
65 模板文件: proj/templates/hello.html.
1708052 @ohsc edit
ohsc authored
66 <pre>$_("Message")</pre>
67
5217a95 edit
David Xie authored
68 创建一个locale目录并使用python2.6内建的pygettext.py从python脚本和模板文件中导出翻译:
1708052 @ohsc edit
ohsc authored
69 <pre>
70 shell> cd /path/to/proj/
71 shell> mkdir -p i18n/en_US/LC_MESSAGES/
72 shell> python /path/to/pygettext.py -a -v -d messages -o i18n/messages.po *.py templates/*.html
73 Working on code.py
74 Working on templates/hello.html
75 </pre>
76
5217a95 edit
David Xie authored
77 你将会得到pot file: i18n/messages.po. 它的内容和下面的差不多
78 ('msgstr'包含了翻译后的信息):
1708052 @ohsc edit
ohsc authored
79 <pre>
5217a95 edit
David Xie authored
80 # 文件 code.py:40
1708052 @ohsc edit
ohsc authored
81 msgid "Message"
82 msgstr "This is translated message in file: code.py."
83 </pre>
84
5217a95 edit
David Xie authored
85 拷贝文件'i18n/messages.po'到目录'i18n/en_US/LC_MESSAGES/'下, 然后翻译它. 使用gettext包的msgfmt工具或者使用python2.6内建的'msgfmt.py'文件将一个pot文件编译称mo文件:
1708052 @ohsc edit
ohsc authored
86 <pre>
87 shell> msgfmt -o i18n/en_US/LC_MESSAGES/messages.mo i18n/en_US/LC_MESSAGES/messages.po
88 </pre>
5217a95 edit
David Xie authored
89
90 运行web.py的服务器:
91
1708052 @ohsc edit
ohsc authored
92 <pre>
93 shell> cd /path/to/proj/
94 shell> python code.py
95 http://0.0.0.0:8000/
96 </pre>
5217a95 edit
David Xie authored
97 打开你的浏览器, 比如说firefox, 然后访问地址: http://192.168.0.3:8000/, 你将会看过翻译过的信息.
Something went wrong with that request. Please try again.