3
# Thomas Nagy, 2006-2010 (ita)
6
Support for translation tools such as msgfmt and intltool
11
conf.load('gnu_dirs intltool')
14
# process the .po files into .gmo files, and install them in LOCALEDIR
15
bld(features='intltool_po', appname='myapp', podir='po', install_path="${LOCALEDIR}")
17
# process an input file, substituting the translations from the po dir
19
features = "intltool_in",
21
flags = ["-d", "-q", "-u", "-c"],
22
source = 'kupfer.desktop.in',
23
install_path = "${DATADIR}/applications",
26
Usage of the :py:mod:`waflib.Tools.gnu_dirs` is recommended, but not obligatory.
30
from waflib import Configure, TaskGen, Task, Utils, Runner, Options, Build, Logs
31
import waflib.Tools.ccroot
32
from waflib.TaskGen import feature, before_method
33
from waflib.Logs import error
35
@before_method('process_source')
36
@feature('intltool_in')
37
def apply_intltool_in_f(self):
39
Create tasks to translate files by intltool-merge::
43
features = "intltool_in",
45
flags = ["-d", "-q", "-u", "-c"],
46
source = 'kupfer.desktop.in',
47
install_path = "${DATADIR}/applications",
50
:param podir: location of the .po files
52
:param source: source files to process
53
:type source: list of string
54
:param flags: compilation flags ("-quc" by default)
55
:type flags: list of string
56
:param install_path: installation path
57
:type install_path: string
59
try: self.meths.remove('process_source')
60
except ValueError: pass
62
if not self.env.LOCALEDIR:
63
self.env.LOCALEDIR = self.env.PREFIX + '/share/locale'
65
for i in self.to_list(self.source):
66
node = self.path.find_resource(i)
68
podir = getattr(self, 'podir', 'po')
69
podirnode = self.path.find_dir(podir)
71
error("could not find the podir %r" % podir)
74
cache = getattr(self, 'intlcache', '.intlcache')
75
self.env['INTLCACHE'] = os.path.join(self.path.bldpath(), podir, cache)
76
self.env['INTLPODIR'] = podirnode.bldpath()
77
self.env['INTLFLAGS'] = getattr(self, 'flags', ['-q', '-u', '-c'])
79
task = self.create_task('intltool', node, node.change_ext(''))
80
inst = getattr(self, 'install_path', '${LOCALEDIR}')
82
self.bld.install_files(inst, task.outputs)
84
@feature('intltool_po')
85
def apply_intltool_po(self):
87
Create tasks to process po files::
90
bld(features='intltool_po', appname='myapp', podir='po', install_path="${LOCALEDIR}")
92
The relevant task generator arguments are:
94
:param podir: directory of the .po files
96
:param appname: name of the application
98
:param install_path: installation directory
99
:type install_path: string
101
The file LINGUAS must be present in the directory pointed by *podir* and list the translation files to process.
103
try: self.meths.remove('process_source')
104
except ValueError: pass
106
if not self.env.LOCALEDIR:
107
self.env.LOCALEDIR = self.env.PREFIX + '/share/locale'
109
appname = getattr(self, 'appname', 'set_your_app_name')
110
podir = getattr(self, 'podir', '')
111
inst = getattr(self, 'install_path', '${LOCALEDIR}')
113
linguas = self.path.find_node(os.path.join(podir, 'LINGUAS'))
115
# scan LINGUAS file for locales to process
116
file = open(linguas.abspath())
118
for line in file.readlines():
119
# ignore lines containing comments
120
if not line.startswith('#'):
121
langs += line.split()
123
re_linguas = re.compile('[-a-zA-Z_@.]+')
125
# Make sure that we only process lines which contain locales
126
if re_linguas.match(lang):
127
node = self.path.find_resource(os.path.join(podir, re_linguas.match(lang).group() + '.po'))
128
task = self.create_task('po', node, node.change_ext('.mo'))
131
filename = task.outputs[0].name
132
(langname, ext) = os.path.splitext(filename)
133
inst_file = inst + os.sep + langname + os.sep + 'LC_MESSAGES' + os.sep + appname + '.mo'
134
self.bld.install_as(inst_file, task.outputs[0], chmod=getattr(self, 'chmod', Utils.O644), env=task.env)
137
Logs.pprint('RED', "Error no LINGUAS file found in po directory")
141
Compile .po files into .gmo files
143
run_str = '${MSGFMT} -o ${TGT} ${SRC}'
146
class intltool(Task.Task):
148
Let intltool-merge translate an input file
150
run_str = '${INTLTOOL} ${INTLFLAGS} ${INTLCACHE} ${INTLPODIR} ${SRC} ${TGT}'
155
Detect the program *msgfmt* and set *conf.env.MSGFMT*.
156
Detect the program *intltool-merge* and set *conf.env.INTLTOOL*.
157
It is possible to set INTLTOOL in the environment, but it must not have spaces in it::
159
$ INTLTOOL="/path/to/the program/intltool" waf configure
161
If a C/C++ compiler is present, execute a compilation test to find the header *locale.h*.
163
conf.find_program('msgfmt', var='MSGFMT')
164
conf.find_perl_program('intltool-merge', var='INTLTOOL')
166
prefix = conf.env.PREFIX
167
datadir = conf.env.DATADIR
169
datadir = os.path.join(prefix,'share')
171
conf.define('LOCALEDIR', os.path.join(datadir, 'locale').replace('\\', '\\\\'))
172
conf.define('DATADIR', datadir.replace('\\', '\\\\'))
174
if conf.env.CC or conf.env.CXX:
175
conf.check(header_name='locale.h')