intltool.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2006-2018 (ita)
  4. """
  5. Support for translation tools such as msgfmt and intltool
  6. Usage::
  7. def configure(conf):
  8. conf.load('gnu_dirs intltool')
  9. def build(bld):
  10. # process the .po files into .gmo files, and install them in LOCALEDIR
  11. bld(features='intltool_po', appname='myapp', podir='po', install_path="${LOCALEDIR}")
  12. # process an input file, substituting the translations from the po dir
  13. bld(
  14. features = "intltool_in",
  15. podir = "../po",
  16. style = "desktop",
  17. flags = ["-u"],
  18. source = 'kupfer.desktop.in',
  19. install_path = "${DATADIR}/applications",
  20. )
  21. Usage of the :py:mod:`waflib.Tools.gnu_dirs` is recommended, but not obligatory.
  22. """
  23. import os, re
  24. from waflib import Context, Task, Utils, Logs
  25. import waflib.Tools.ccroot
  26. from waflib.TaskGen import feature, before_method, taskgen_method
  27. from waflib.Logs import error
  28. from waflib.Configure import conf
  29. _style_flags = {
  30. 'ba': '-b',
  31. 'desktop': '-d',
  32. 'keys': '-k',
  33. 'quoted': '--quoted-style',
  34. 'quotedxml': '--quotedxml-style',
  35. 'rfc822deb': '-r',
  36. 'schemas': '-s',
  37. 'xml': '-x',
  38. }
  39. @taskgen_method
  40. def ensure_localedir(self):
  41. """
  42. Expands LOCALEDIR from DATAROOTDIR/locale if possible, or falls back to PREFIX/share/locale
  43. """
  44. # use the tool gnu_dirs to provide options to define this
  45. if not self.env.LOCALEDIR:
  46. if self.env.DATAROOTDIR:
  47. self.env.LOCALEDIR = os.path.join(self.env.DATAROOTDIR, 'locale')
  48. else:
  49. self.env.LOCALEDIR = os.path.join(self.env.PREFIX, 'share', 'locale')
  50. @before_method('process_source')
  51. @feature('intltool_in')
  52. def apply_intltool_in_f(self):
  53. """
  54. Creates tasks to translate files by intltool-merge::
  55. def build(bld):
  56. bld(
  57. features = "intltool_in",
  58. podir = "../po",
  59. style = "desktop",
  60. flags = ["-u"],
  61. source = 'kupfer.desktop.in',
  62. install_path = "${DATADIR}/applications",
  63. )
  64. :param podir: location of the .po files
  65. :type podir: string
  66. :param source: source files to process
  67. :type source: list of string
  68. :param style: the intltool-merge mode of operation, can be one of the following values:
  69. ``ba``, ``desktop``, ``keys``, ``quoted``, ``quotedxml``, ``rfc822deb``, ``schemas`` and ``xml``.
  70. See the ``intltool-merge`` man page for more information about supported modes of operation.
  71. :type style: string
  72. :param flags: compilation flags ("-quc" by default)
  73. :type flags: list of string
  74. :param install_path: installation path
  75. :type install_path: string
  76. """
  77. try:
  78. self.meths.remove('process_source')
  79. except ValueError:
  80. pass
  81. self.ensure_localedir()
  82. podir = getattr(self, 'podir', '.')
  83. podirnode = self.path.find_dir(podir)
  84. if not podirnode:
  85. error("could not find the podir %r" % podir)
  86. return
  87. cache = getattr(self, 'intlcache', '.intlcache')
  88. self.env.INTLCACHE = [os.path.join(str(self.path.get_bld()), podir, cache)]
  89. self.env.INTLPODIR = podirnode.bldpath()
  90. self.env.append_value('INTLFLAGS', getattr(self, 'flags', self.env.INTLFLAGS_DEFAULT))
  91. if '-c' in self.env.INTLFLAGS:
  92. self.bld.fatal('Redundant -c flag in intltool task %r' % self)
  93. style = getattr(self, 'style', None)
  94. if style:
  95. try:
  96. style_flag = _style_flags[style]
  97. except KeyError:
  98. self.bld.fatal('intltool_in style "%s" is not valid' % style)
  99. self.env.append_unique('INTLFLAGS', [style_flag])
  100. for i in self.to_list(self.source):
  101. node = self.path.find_resource(i)
  102. task = self.create_task('intltool', node, node.change_ext(''))
  103. inst = getattr(self, 'install_path', None)
  104. if inst:
  105. self.add_install_files(install_to=inst, install_from=task.outputs)
  106. @feature('intltool_po')
  107. def apply_intltool_po(self):
  108. """
  109. Creates tasks to process po files::
  110. def build(bld):
  111. bld(features='intltool_po', appname='myapp', podir='po', install_path="${LOCALEDIR}")
  112. The relevant task generator arguments are:
  113. :param podir: directory of the .po files
  114. :type podir: string
  115. :param appname: name of the application
  116. :type appname: string
  117. :param install_path: installation directory
  118. :type install_path: string
  119. The file LINGUAS must be present in the directory pointed by *podir* and list the translation files to process.
  120. """
  121. try:
  122. self.meths.remove('process_source')
  123. except ValueError:
  124. pass
  125. self.ensure_localedir()
  126. appname = getattr(self, 'appname', getattr(Context.g_module, Context.APPNAME, 'set_your_app_name'))
  127. podir = getattr(self, 'podir', '.')
  128. inst = getattr(self, 'install_path', '${LOCALEDIR}')
  129. linguas = self.path.find_node(os.path.join(podir, 'LINGUAS'))
  130. if linguas:
  131. # scan LINGUAS file for locales to process
  132. with open(linguas.abspath()) as f:
  133. langs = []
  134. for line in f.readlines():
  135. # ignore lines containing comments
  136. if not line.startswith('#'):
  137. langs += line.split()
  138. re_linguas = re.compile('[-a-zA-Z_@.]+')
  139. for lang in langs:
  140. # Make sure that we only process lines which contain locales
  141. if re_linguas.match(lang):
  142. node = self.path.find_resource(os.path.join(podir, re_linguas.match(lang).group() + '.po'))
  143. task = self.create_task('po', node, node.change_ext('.mo'))
  144. if inst:
  145. filename = task.outputs[0].name
  146. (langname, ext) = os.path.splitext(filename)
  147. inst_file = inst + os.sep + langname + os.sep + 'LC_MESSAGES' + os.sep + appname + '.mo'
  148. self.add_install_as(install_to=inst_file, install_from=task.outputs[0],
  149. chmod=getattr(self, 'chmod', Utils.O644))
  150. else:
  151. Logs.pprint('RED', "Error no LINGUAS file found in po directory")
  152. class po(Task.Task):
  153. """
  154. Compiles .po files into .gmo files
  155. """
  156. run_str = '${MSGFMT} -o ${TGT} ${SRC}'
  157. color = 'BLUE'
  158. class intltool(Task.Task):
  159. """
  160. Calls intltool-merge to update translation files
  161. """
  162. run_str = '${INTLTOOL} ${INTLFLAGS} ${INTLCACHE_ST:INTLCACHE} ${INTLPODIR} ${SRC} ${TGT}'
  163. color = 'BLUE'
  164. @conf
  165. def find_msgfmt(conf):
  166. """
  167. Detects msgfmt and sets the ``MSGFMT`` variable
  168. """
  169. conf.find_program('msgfmt', var='MSGFMT')
  170. @conf
  171. def find_intltool_merge(conf):
  172. """
  173. Detects intltool-merge
  174. """
  175. if not conf.env.PERL:
  176. conf.find_program('perl', var='PERL')
  177. conf.env.INTLCACHE_ST = '--cache=%s'
  178. conf.env.INTLFLAGS_DEFAULT = ['-q', '-u']
  179. conf.find_program('intltool-merge', interpreter='PERL', var='INTLTOOL')
  180. def configure(conf):
  181. """
  182. Detects the program *msgfmt* and set *conf.env.MSGFMT*.
  183. Detects the program *intltool-merge* and set *conf.env.INTLTOOL*.
  184. It is possible to set INTLTOOL in the environment, but it must not have spaces in it::
  185. $ INTLTOOL="/path/to/the program/intltool" waf configure
  186. If a C/C++ compiler is present, execute a compilation test to find the header *locale.h*.
  187. """
  188. conf.find_msgfmt()
  189. conf.find_intltool_merge()
  190. if conf.env.CC or conf.env.CXX:
  191. conf.check(header_name='locale.h')