i18n.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  14. from ..mesonlib import MesonException
  15. from . import ModuleReturnValue
  16. from . import ExtensionModule
  17. from ..interpreterbase import permittedKwargs
  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. ]
  40. }
  41. class I18nModule(ExtensionModule):
  42. @staticmethod
  43. def _get_data_dirs(state, dirs):
  44. """Returns source directories of relative paths"""
  45. src_dir = path.join(state.environment.get_source_dir(), state.subdir)
  46. return [path.join(src_dir, d) for d in dirs]
  47. @permittedKwargs({'languages', 'data_dirs', 'preset', 'args', 'po_dir', 'type',
  48. 'input', 'output', 'install', 'install_dir'})
  49. def merge_file(self, state, args, kwargs):
  50. podir = kwargs.pop('po_dir', None)
  51. if not podir:
  52. raise MesonException('i18n: po_dir is a required kwarg')
  53. podir = path.join(state.build_to_src, state.subdir, podir)
  54. file_type = kwargs.pop('type', 'xml')
  55. VALID_TYPES = ('xml', 'desktop')
  56. if file_type not in VALID_TYPES:
  57. raise MesonException('i18n: "{}" is not a valid type {}'.format(file_type, VALID_TYPES))
  58. datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.pop('data_dirs', [])))
  59. datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
  60. command = state.environment.get_build_command() + [
  61. '--internal', 'msgfmthelper',
  62. '@INPUT@', '@OUTPUT@', file_type, podir
  63. ]
  64. if datadirs:
  65. command.append(datadirs)
  66. kwargs['command'] = command
  67. ct = build.CustomTarget(kwargs['output'] + '_merge', state.subdir, state.subproject, kwargs)
  68. return ModuleReturnValue(ct, [ct])
  69. @permittedKwargs({'po_dir', 'data_dirs', 'type', 'languages', 'args', 'preset', 'install'})
  70. def gettext(self, state, args, kwargs):
  71. if len(args) != 1:
  72. raise coredata.MesonException('Gettext requires one positional argument (package name).')
  73. if not shutil.which('xgettext'):
  74. raise coredata.MesonException('Can not do gettext because xgettext is not installed.')
  75. packagename = args[0]
  76. languages = mesonlib.stringlistify(kwargs.get('languages', []))
  77. datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.get('data_dirs', [])))
  78. extra_args = mesonlib.stringlistify(kwargs.get('args', []))
  79. preset = kwargs.pop('preset', None)
  80. if preset:
  81. preset_args = PRESET_ARGS.get(preset)
  82. if not preset_args:
  83. raise coredata.MesonException('i18n: Preset "{}" is not one of the valid options: {}'.format(
  84. preset, list(PRESET_ARGS.keys())))
  85. extra_args = set(preset_args + extra_args)
  86. pkg_arg = '--pkgname=' + packagename
  87. lang_arg = '--langs=' + '@@'.join(languages) if languages else None
  88. datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
  89. extra_args = '--extra-args=' + '@@'.join(extra_args) if extra_args else None
  90. potargs = state.environment.get_build_command() + ['--internal', 'gettext', 'pot', pkg_arg]
  91. if datadirs:
  92. potargs.append(datadirs)
  93. if extra_args:
  94. potargs.append(extra_args)
  95. pottarget = build.RunTarget(packagename + '-pot', potargs[0], potargs[1:], [], state.subdir, state.subproject)
  96. gmoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'gen_gmo']
  97. if lang_arg:
  98. gmoargs.append(lang_arg)
  99. gmotarget = build.RunTarget(packagename + '-gmo', gmoargs[0], gmoargs[1:], [], state.subdir, state.subproject)
  100. updatepoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'update_po', pkg_arg]
  101. if lang_arg:
  102. updatepoargs.append(lang_arg)
  103. if datadirs:
  104. updatepoargs.append(datadirs)
  105. if extra_args:
  106. updatepoargs.append(extra_args)
  107. updatepotarget = build.RunTarget(packagename + '-update-po', updatepoargs[0], updatepoargs[1:], [], state.subdir, state.subproject)
  108. targets = [pottarget, gmotarget, updatepotarget]
  109. install = kwargs.get('install', True)
  110. if install:
  111. script = state.environment.get_build_command()
  112. args = ['--internal', 'gettext', 'install',
  113. '--subdir=' + state.subdir,
  114. '--localedir=' + state.environment.coredata.get_builtin_option('localedir'),
  115. pkg_arg]
  116. if lang_arg:
  117. args.append(lang_arg)
  118. iscript = build.RunScript(script, args)
  119. targets.append(iscript)
  120. return ModuleReturnValue(None, targets)
  121. def initialize():
  122. return I18nModule()