i18n.py 5.5 KB

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