i18n.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 sys
  12. import shutil
  13. from os import path
  14. from .. import coredata, mesonlib, build
  15. from ..mesonlib import MesonException
  16. from . import ModuleReturnValue
  17. from . import ExtensionModule
  18. from . import permittedKwargs
  19. PRESET_ARGS = {
  20. 'glib': [
  21. '--from-code=UTF-8',
  22. '--add-comments',
  23. # https://developer.gnome.org/glib/stable/glib-I18N.html
  24. '--keyword=_',
  25. '--keyword=N_',
  26. '--keyword=C_:1c,2',
  27. '--keyword=NC_:1c,2',
  28. '--keyword=g_dcgettext:2',
  29. '--keyword=g_dngettext:2,3',
  30. '--keyword=g_dpgettext2:2c,3',
  31. '--flag=N_:1:pass-c-format',
  32. '--flag=C_:2:pass-c-format',
  33. '--flag=NC_:2:pass-c-format',
  34. '--flag=g_dngettext:2:pass-c-format',
  35. '--flag=g_strdup_printf:1:c-format',
  36. '--flag=g_string_printf:2:c-format',
  37. '--flag=g_string_append_printf:2:c-format',
  38. '--flag=g_error_new:3:c-format',
  39. '--flag=g_set_error:4:c-format',
  40. ]
  41. }
  42. class I18nModule(ExtensionModule):
  43. @staticmethod
  44. def _get_data_dirs(state, dirs):
  45. """Returns source directories of relative paths"""
  46. src_dir = path.join(state.environment.get_source_dir(), state.subdir)
  47. return [path.join(src_dir, d) for d in dirs]
  48. @permittedKwargs({'languages', 'data_dirs', 'preset', 'args', 'po_dir', 'type',
  49. 'input', 'output', 'install', 'install_dir'})
  50. def merge_file(self, state, args, kwargs):
  51. podir = kwargs.pop('po_dir', None)
  52. if not podir:
  53. raise MesonException('i18n: po_dir is a required kwarg')
  54. podir = path.join(state.build_to_src, state.subdir, podir)
  55. file_type = kwargs.pop('type', 'xml')
  56. VALID_TYPES = ('xml', 'desktop')
  57. if file_type not in VALID_TYPES:
  58. raise MesonException('i18n: "{}" is not a valid type {}'.format(file_type, VALID_TYPES))
  59. datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.pop('data_dirs', [])))
  60. datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
  61. command = [state.environment.get_build_command(), '--internal', 'msgfmthelper',
  62. '@INPUT@', '@OUTPUT@', file_type, podir]
  63. if datadirs:
  64. command.append(datadirs)
  65. kwargs['command'] = command
  66. ct = build.CustomTarget(kwargs['output'] + '_merge', state.subdir, kwargs)
  67. return ModuleReturnValue(ct, [ct])
  68. @permittedKwargs({'po_dir', 'data_dirs', 'type', 'languages', 'args', 'preset'})
  69. def gettext(self, state, args, kwargs):
  70. if len(args) != 1:
  71. raise coredata.MesonException('Gettext requires one positional argument (package name).')
  72. if not shutil.which('xgettext'):
  73. raise coredata.MesonException('Can not do gettext because xgettext is not installed.')
  74. packagename = args[0]
  75. languages = mesonlib.stringlistify(kwargs.get('languages', []))
  76. datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.get('data_dirs', [])))
  77. extra_args = mesonlib.stringlistify(kwargs.get('args', []))
  78. preset = kwargs.pop('preset', None)
  79. if preset:
  80. preset_args = PRESET_ARGS.get(preset)
  81. if not preset_args:
  82. raise coredata.MesonException('i18n: Preset "{}" is not one of the valid options: {}'.format(
  83. preset, list(PRESET_ARGS.keys())))
  84. extra_args = set(preset_args + extra_args)
  85. pkg_arg = '--pkgname=' + packagename
  86. lang_arg = '--langs=' + '@@'.join(languages) if languages else None
  87. datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
  88. extra_args = '--extra-args=' + '@@'.join(extra_args) if extra_args else None
  89. potargs = [state.environment.get_build_command(), '--internal', 'gettext', 'pot', pkg_arg]
  90. if datadirs:
  91. potargs.append(datadirs)
  92. if extra_args:
  93. potargs.append(extra_args)
  94. pottarget = build.RunTarget(packagename + '-pot', sys.executable, potargs, [], state.subdir)
  95. gmoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'gen_gmo']
  96. if lang_arg:
  97. gmoargs.append(lang_arg)
  98. gmotarget = build.RunTarget(packagename + '-gmo', sys.executable, gmoargs, [], state.subdir)
  99. updatepoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'update_po', pkg_arg]
  100. if lang_arg:
  101. updatepoargs.append(lang_arg)
  102. if datadirs:
  103. updatepoargs.append(datadirs)
  104. if extra_args:
  105. updatepoargs.append(extra_args)
  106. updatepotarget = build.RunTarget(packagename + '-update-po', sys.executable, updatepoargs, [], state.subdir)
  107. script = [sys.executable, state.environment.get_build_command()]
  108. args = ['--internal', 'gettext', 'install',
  109. '--subdir=' + state.subdir,
  110. '--localedir=' + state.environment.coredata.get_builtin_option('localedir'),
  111. pkg_arg]
  112. if lang_arg:
  113. args.append(lang_arg)
  114. iscript = build.RunScript(script, args)
  115. return ModuleReturnValue(None, [pottarget, gmotarget, iscript, updatepotarget])
  116. def initialize():
  117. return I18nModule()