i18n.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # Copyright 2016 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import shutil
  12. from os import path
  13. from .. import coredata, mesonlib, build, mlog
  14. from ..mesonlib import MesonException
  15. from . import ModuleReturnValue
  16. from . import ExtensionModule
  17. from ..interpreterbase import permittedKwargs, FeatureNew, FeatureNewKwargs
  18. PRESET_ARGS = {
  19. 'glib': [
  20. '--from-code=UTF-8',
  21. '--add-comments',
  22. # https://developer.gnome.org/glib/stable/glib-I18N.html
  23. '--keyword=_',
  24. '--keyword=N_',
  25. '--keyword=C_:1c,2',
  26. '--keyword=NC_:1c,2',
  27. '--keyword=g_dcgettext:2',
  28. '--keyword=g_dngettext:2,3',
  29. '--keyword=g_dpgettext2:2c,3',
  30. '--flag=N_:1:pass-c-format',
  31. '--flag=C_:2:pass-c-format',
  32. '--flag=NC_:2:pass-c-format',
  33. '--flag=g_dngettext:2:pass-c-format',
  34. '--flag=g_strdup_printf:1:c-format',
  35. '--flag=g_string_printf:2:c-format',
  36. '--flag=g_string_append_printf:2:c-format',
  37. '--flag=g_error_new:3:c-format',
  38. '--flag=g_set_error:4:c-format',
  39. '--flag=g_markup_printf_escaped:1:c-format',
  40. '--flag=g_log:3:c-format',
  41. '--flag=g_print:1:c-format',
  42. '--flag=g_printerr:1:c-format',
  43. '--flag=g_printf:1:c-format',
  44. '--flag=g_fprintf:2:c-format',
  45. '--flag=g_sprintf:2:c-format',
  46. '--flag=g_snprintf:3:c-format',
  47. ]
  48. }
  49. class I18nModule(ExtensionModule):
  50. @staticmethod
  51. def nogettext_warning():
  52. mlog.warning('Gettext not found, all translation targets will be ignored.', once=True)
  53. return ModuleReturnValue(None, [])
  54. @staticmethod
  55. def _get_data_dirs(state, dirs):
  56. """Returns source directories of relative paths"""
  57. src_dir = path.join(state.environment.get_source_dir(), state.subdir)
  58. return [path.join(src_dir, d) for d in dirs]
  59. @FeatureNew('i18n.merge_file', '0.37.0')
  60. @FeatureNewKwargs('i18n.merge_file', '0.51.0', ['args'])
  61. @permittedKwargs(build.CustomTarget.known_kwargs | {'data_dirs', 'po_dir', 'type', 'args'})
  62. def merge_file(self, state, args, kwargs):
  63. if not shutil.which('xgettext'):
  64. return self.nogettext_warning()
  65. podir = kwargs.pop('po_dir', None)
  66. if not podir:
  67. raise MesonException('i18n: po_dir is a required kwarg')
  68. podir = path.join(state.build_to_src, state.subdir, podir)
  69. file_type = kwargs.pop('type', 'xml')
  70. VALID_TYPES = ('xml', 'desktop')
  71. if file_type not in VALID_TYPES:
  72. raise MesonException('i18n: "{}" is not a valid type {}'.format(file_type, VALID_TYPES))
  73. datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.pop('data_dirs', [])))
  74. datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
  75. command = state.environment.get_build_command() + [
  76. '--internal', 'msgfmthelper',
  77. '@INPUT@', '@OUTPUT@', file_type, podir
  78. ]
  79. if datadirs:
  80. command.append(datadirs)
  81. if 'args' in kwargs:
  82. command.append('--')
  83. command.append(mesonlib.stringlistify(kwargs.pop('args', [])))
  84. kwargs['command'] = command
  85. inputfile = kwargs['input']
  86. if hasattr(inputfile, 'held_object'):
  87. ct = build.CustomTarget(kwargs['output'] + '_merge', state.subdir, state.subproject, kwargs)
  88. else:
  89. if isinstance(inputfile, list):
  90. # We only use this input file to create a name of the custom target.
  91. # Thus we can ignore the other entries.
  92. inputfile = inputfile[0]
  93. if isinstance(inputfile, str):
  94. inputfile = mesonlib.File.from_source_file(state.environment.source_dir,
  95. state.subdir, inputfile)
  96. output = kwargs['output']
  97. ifile_abs = inputfile.absolute_path(state.environment.source_dir,
  98. state.environment.build_dir)
  99. values = mesonlib.get_filenames_templates_dict([ifile_abs], None)
  100. outputs = mesonlib.substitute_values([output], values)
  101. output = outputs[0]
  102. ct = build.CustomTarget(output + '_' + state.subdir.replace('/', '@').replace('\\', '@') + '_merge', state.subdir, state.subproject, kwargs)
  103. return ModuleReturnValue(ct, [ct])
  104. @FeatureNewKwargs('i18n.gettext', '0.37.0', ['preset'])
  105. @FeatureNewKwargs('i18n.gettext', '0.50.0', ['install_dir'])
  106. @permittedKwargs({'po_dir', 'data_dirs', 'type', 'languages', 'args', 'preset', 'install', 'install_dir'})
  107. def gettext(self, state, args, kwargs):
  108. if len(args) != 1:
  109. raise coredata.MesonException('Gettext requires one positional argument (package name).')
  110. if not shutil.which('xgettext'):
  111. return self.nogettext_warning()
  112. packagename = args[0]
  113. languages = mesonlib.stringlistify(kwargs.get('languages', []))
  114. datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.get('data_dirs', [])))
  115. extra_args = mesonlib.stringlistify(kwargs.get('args', []))
  116. preset = kwargs.pop('preset', None)
  117. if preset:
  118. preset_args = PRESET_ARGS.get(preset)
  119. if not preset_args:
  120. raise coredata.MesonException('i18n: Preset "{}" is not one of the valid options: {}'.format(
  121. preset, list(PRESET_ARGS.keys())))
  122. extra_args = set(preset_args + extra_args)
  123. pkg_arg = '--pkgname=' + packagename
  124. lang_arg = '--langs=' + '@@'.join(languages) if languages else None
  125. datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
  126. extra_args = '--extra-args=' + '@@'.join(extra_args) if extra_args else None
  127. potargs = state.environment.get_build_command() + ['--internal', 'gettext', 'pot', pkg_arg]
  128. if datadirs:
  129. potargs.append(datadirs)
  130. if extra_args:
  131. potargs.append(extra_args)
  132. pottarget = build.RunTarget(packagename + '-pot', potargs[0], potargs[1:], [], state.subdir, state.subproject)
  133. gmoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'gen_gmo']
  134. if lang_arg:
  135. gmoargs.append(lang_arg)
  136. gmotarget = build.RunTarget(packagename + '-gmo', gmoargs[0], gmoargs[1:], [], state.subdir, state.subproject)
  137. updatepoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'update_po', pkg_arg]
  138. if lang_arg:
  139. updatepoargs.append(lang_arg)
  140. if datadirs:
  141. updatepoargs.append(datadirs)
  142. if extra_args:
  143. updatepoargs.append(extra_args)
  144. updatepotarget = build.RunTarget(packagename + '-update-po', updatepoargs[0], updatepoargs[1:], [], state.subdir, state.subproject)
  145. targets = [pottarget, gmotarget, updatepotarget]
  146. install = kwargs.get('install', True)
  147. if install:
  148. install_dir = kwargs.get('install_dir', state.environment.coredata.get_builtin_option('localedir'))
  149. script = state.environment.get_build_command()
  150. args = ['--internal', 'gettext', 'install',
  151. '--subdir=' + state.subdir,
  152. '--localedir=' + install_dir,
  153. pkg_arg]
  154. if lang_arg:
  155. args.append(lang_arg)
  156. iscript = build.RunScript(script, args)
  157. targets.append(iscript)
  158. return ModuleReturnValue(None, targets)
  159. def initialize(*args, **kwargs):
  160. return I18nModule(*args, **kwargs)