X Tutup
Skip to content

Commit c7ab1dd

Browse files
committed
i18n: build_translation, i18n/->po/
".po" files moved to directory "po/", renamed using the following template: "lng_LNG.po". File "setup.py" properly configured to build .mo files, with "build_translation" option. bPython's main() functions (cli.py, gtk_.py, urwid.py) properly modified in order to use gettext.
1 parent 428bcba commit c7ab1dd

File tree

7 files changed

+85
-13
lines changed

7 files changed

+85
-13
lines changed

bpython/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,9 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
17241724
def main(args=None, locals_=None, banner=None):
17251725
global stdscr
17261726

1727+
import gettext
1728+
gettext.install('bpython')
1729+
17271730
setlocale(LC_ALL, '')
17281731

17291732
config, options, exec_args = bpython.args.parse(args)

bpython/gtk_.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ def init_import_completion():
763763

764764

765765
def main(args=None):
766+
import gettext
767+
gettext.install('bpython')
766768

767769
setlocale(LC_ALL, '')
768770

bpython/urwid.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,9 @@ def tab(self, back=False):
842842
self._completion_update_suppressed = False
843843

844844
def main(args=None, locals_=None, banner=None):
845+
import gettext
846+
gettext.install('bpython')
847+
845848
# Err, somewhat redundant. There is a call to this buried in urwid.util.
846849
# That seems unfortunate though, so assume that's going away...
847850
locale.setlocale(locale.LC_ALL, '')

data/bpython

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#!/usr/bin/env python
22

33
import sys
4-
import gettext
5-
6-
gettext.install('bpython', 'i18n/po/')
7-
84
from bpython.cli import main
5+
96
sys.exit(main())

data/bpython-gtk

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#!/usr/bin/env python
22

33
import sys
4-
import gettext
5-
6-
gettext.install('bpython', 'i18n/po/')
7-
84
from bpython.gtk_ import main
5+
96
sys.exit(main())

setup.py

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44

55
import glob
66
import os
7+
import os.path
78
import platform
89
import re
910
import sys
11+
12+
from distutils import cmd
13+
from distutils.command.install_data import install_data as _install_data
14+
from distutils.command.build import build
15+
16+
from bpython import __version__
17+
1018
try:
1119
from setuptools import setup
1220
using_setuptools = True
@@ -19,14 +27,71 @@
1927
except ImportError:
2028
from distutils.command.build_py import build_py
2129

22-
from bpython import __version__
23-
30+
try:
31+
import msgfmt
32+
except ImportError:
33+
pass
2434

2535
if platform.system() == 'FreeBSD':
2636
man_dir = 'man'
2737
else:
2838
man_dir = 'share/man'
2939

40+
41+
42+
class build_translation(cmd.Command):
43+
"""Internationalization suport for bpython.
44+
Compile .po files into .mo files"""
45+
46+
description = __doc__
47+
user_options = []
48+
49+
def initialize_options(self):
50+
pass
51+
52+
def finalize_options(self):
53+
pass
54+
55+
def run(self):
56+
src_path = os.path.join(os.path.realpath(''), 'po')
57+
for filename in os.listdir(src_path):
58+
if (not os.path.isfile(os.path.join(src_path, filename)) or
59+
not filename.endswith('.po')):
60+
continue
61+
62+
lang = filename[:-3]
63+
dest_path = os.path.join('build', 'locale', lang, 'LC_MESSAGES')
64+
65+
src = os.path.join(src_path, filename)
66+
dest = os.path.join(dest_path, 'bpython.mo')
67+
68+
if not os.path.exists(dest_path):
69+
os.makedirs(dest_path)
70+
if (not os.path.exists(dest) or
71+
os.stat(src)[8] > os.stat(dest)[8]):
72+
print ('Adding translation: %s' % lang)
73+
msgfmt.make(src, dest)
74+
75+
build.sub_commands.append(('build_translation', None))
76+
77+
78+
class install_data(_install_data):
79+
"""Append to data_files l10n .mo files. Then continue with normal install."""
80+
81+
def run(self):
82+
for lang in os.listdir('po'):
83+
if not lang.endswith('.mo'):
84+
continue
85+
86+
lang_dir = os.path.join('share', 'locale', lang[:-3], 'LC_MESSAGES')
87+
lang_file = os.path.join('po', lang)
88+
self.data_files.append((lang_dir, [lang_file]))
89+
90+
_install_data.run(self)
91+
92+
build.sub_commands.append(('install_data', None))
93+
94+
3095
setup(
3196
name="bpython",
3297
version = __version__,
@@ -42,9 +107,11 @@
42107
],
43108
packages = ["bpython"],
44109
data_files = [
110+
# man pages
45111
(os.path.join(man_dir, 'man1'), ['doc/bpython.1']),
46112
(os.path.join(man_dir, 'man5'), ['doc/bpython-config.5']),
47-
('share/applications', ['data/bpython.desktop'])
113+
# desktop shorcut
114+
(os.path.join('share', 'applications'), ['data/bpython.desktop'])
48115
],
49116
package_data = {'bpython': ['logo.png']},
50117
entry_points = {
@@ -53,9 +120,12 @@
53120
'bpython-gtk = bpython.gtk_:main',
54121
],
55122
},
56-
scripts = ([] if using_setuptools else ['data/bpython',
123+
scripts = ([] if using_setuptools else ['data/bpython',
57124
'data/bpython-gtk']),
58-
cmdclass=dict(build_py=build_py)
125+
cmdclass=dict(build_py=build_py,
126+
build = build,
127+
build_translation = build_translation,
128+
install_data = install_data)
59129
)
60130

61131
# vim: encoding=utf-8 sw=4 ts=4 sts=4 ai et sta

0 commit comments

Comments
 (0)
X Tutup